github.com/kaituanwang/hyperledger@v2.0.1+incompatible/internal/configtxlator/rest/protolator_handlers_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package rest
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"net/http"
    13  	"net/http/httptest"
    14  	"strings"
    15  	"testing"
    16  
    17  	"github.com/golang/protobuf/proto"
    18  	cb "github.com/hyperledger/fabric-protos-go/common"
    19  	"github.com/hyperledger/fabric/protoutil"
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  var (
    24  	testProto = &cb.Block{
    25  		Header: &cb.BlockHeader{
    26  			PreviousHash: []byte("foo"),
    27  		},
    28  		Data: &cb.BlockData{
    29  			Data: [][]byte{
    30  				protoutil.MarshalOrPanic(&cb.Envelope{
    31  					Payload: protoutil.MarshalOrPanic(&cb.Payload{
    32  						Header: &cb.Header{
    33  							ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{
    34  								Type: int32(cb.HeaderType_CONFIG),
    35  							}),
    36  						},
    37  					}),
    38  					Signature: []byte("bar"),
    39  				}),
    40  			},
    41  		},
    42  	}
    43  
    44  	testOutput = `{"data":{"data":[{"payload":{"data":null,"header":{"channel_header":{"channel_id":"","epoch":"0","extension":null,"timestamp":null,"tls_cert_hash":null,"tx_id":"","type":1,"version":0},"signature_header":null}},"signature":"YmFy"}]},"header":{"data_hash":null,"number":"0","previous_hash":"Zm9v"},"metadata":null}`
    45  )
    46  
    47  func TestProtolatorDecode(t *testing.T) {
    48  	data, err := proto.Marshal(testProto)
    49  	assert.NoError(t, err)
    50  
    51  	url := fmt.Sprintf("/protolator/decode/%s", proto.MessageName(testProto))
    52  
    53  	req, _ := http.NewRequest("POST", url, bytes.NewReader(data))
    54  	rec := httptest.NewRecorder()
    55  	r := NewRouter()
    56  	r.ServeHTTP(rec, req)
    57  
    58  	assert.Equal(t, http.StatusOK, rec.Code)
    59  
    60  	// Remove all the whitespace
    61  	compactJSON := strings.Replace(strings.Replace(strings.Replace(rec.Body.String(), "\n", "", -1), "\t", "", -1), " ", "", -1)
    62  
    63  	assert.Equal(t, testOutput, compactJSON)
    64  }
    65  
    66  func TestProtolatorEncode(t *testing.T) {
    67  
    68  	url := fmt.Sprintf("/protolator/encode/%s", proto.MessageName(testProto))
    69  
    70  	req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte(testOutput)))
    71  	rec := httptest.NewRecorder()
    72  	r := NewRouter()
    73  	r.ServeHTTP(rec, req)
    74  
    75  	assert.Equal(t, http.StatusOK, rec.Code)
    76  
    77  	outputMsg := &cb.Block{}
    78  
    79  	err := proto.Unmarshal(rec.Body.Bytes(), outputMsg)
    80  	assert.NoError(t, err)
    81  	assert.True(t, proto.Equal(testProto, outputMsg))
    82  }
    83  
    84  func TestProtolatorDecodeNonExistantProto(t *testing.T) {
    85  	req, _ := http.NewRequest("POST", "/protolator/decode/NonExistantMsg", bytes.NewReader([]byte{}))
    86  	rec := httptest.NewRecorder()
    87  	r := NewRouter()
    88  	r.ServeHTTP(rec, req)
    89  
    90  	assert.Equal(t, http.StatusNotFound, rec.Code)
    91  }
    92  
    93  func TestProtolatorEncodeNonExistantProto(t *testing.T) {
    94  	req, _ := http.NewRequest("POST", "/protolator/encode/NonExistantMsg", bytes.NewReader([]byte{}))
    95  	rec := httptest.NewRecorder()
    96  	r := NewRouter()
    97  	r.ServeHTTP(rec, req)
    98  
    99  	assert.Equal(t, http.StatusNotFound, rec.Code)
   100  }
   101  
   102  func TestProtolatorDecodeBadData(t *testing.T) {
   103  	url := fmt.Sprintf("/protolator/decode/%s", proto.MessageName(testProto))
   104  
   105  	req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte("Garbage")))
   106  
   107  	rec := httptest.NewRecorder()
   108  	r := NewRouter()
   109  	r.ServeHTTP(rec, req)
   110  
   111  	assert.Equal(t, http.StatusBadRequest, rec.Code)
   112  }
   113  
   114  func TestProtolatorEncodeBadData(t *testing.T) {
   115  	url := fmt.Sprintf("/protolator/encode/%s", proto.MessageName(testProto))
   116  
   117  	req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte("Garbage")))
   118  
   119  	rec := httptest.NewRecorder()
   120  	r := NewRouter()
   121  	r.ServeHTTP(rec, req)
   122  
   123  	assert.Equal(t, http.StatusBadRequest, rec.Code)
   124  }