github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/configtxlator/rest/protolator_handlers_test.go (about)

     1  /*
     2  Copyright hechain. 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  	"github.com/hechain20/hechain/protoutil"
    19  	cb "github.com/hyperledger/fabric-protos-go/common"
    20  	"github.com/stretchr/testify/require"
    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  	require.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  	require.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  	require.Equal(t, testOutput, compactJSON)
    64  }
    65  
    66  func TestProtolatorEncode(t *testing.T) {
    67  	url := fmt.Sprintf("/protolator/encode/%s", proto.MessageName(testProto))
    68  
    69  	req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte(testOutput)))
    70  	rec := httptest.NewRecorder()
    71  	r := NewRouter()
    72  	r.ServeHTTP(rec, req)
    73  
    74  	require.Equal(t, http.StatusOK, rec.Code)
    75  
    76  	outputMsg := &cb.Block{}
    77  
    78  	err := proto.Unmarshal(rec.Body.Bytes(), outputMsg)
    79  	require.NoError(t, err)
    80  	require.True(t, proto.Equal(testProto, outputMsg))
    81  }
    82  
    83  func TestProtolatorDecodeNonExistantProto(t *testing.T) {
    84  	req, _ := http.NewRequest("POST", "/protolator/decode/NonExistantMsg", bytes.NewReader([]byte{}))
    85  	rec := httptest.NewRecorder()
    86  	r := NewRouter()
    87  	r.ServeHTTP(rec, req)
    88  
    89  	require.Equal(t, http.StatusNotFound, rec.Code)
    90  }
    91  
    92  func TestProtolatorEncodeNonExistantProto(t *testing.T) {
    93  	req, _ := http.NewRequest("POST", "/protolator/encode/NonExistantMsg", bytes.NewReader([]byte{}))
    94  	rec := httptest.NewRecorder()
    95  	r := NewRouter()
    96  	r.ServeHTTP(rec, req)
    97  
    98  	require.Equal(t, http.StatusNotFound, rec.Code)
    99  }
   100  
   101  func TestProtolatorDecodeBadData(t *testing.T) {
   102  	url := fmt.Sprintf("/protolator/decode/%s", proto.MessageName(testProto))
   103  
   104  	req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte("Garbage")))
   105  
   106  	rec := httptest.NewRecorder()
   107  	r := NewRouter()
   108  	r.ServeHTTP(rec, req)
   109  
   110  	require.Equal(t, http.StatusBadRequest, rec.Code)
   111  }
   112  
   113  func TestProtolatorEncodeBadData(t *testing.T) {
   114  	url := fmt.Sprintf("/protolator/encode/%s", proto.MessageName(testProto))
   115  
   116  	req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte("Garbage")))
   117  
   118  	rec := httptest.NewRecorder()
   119  	r := NewRouter()
   120  	r.ServeHTTP(rec, req)
   121  
   122  	require.Equal(t, http.StatusBadRequest, rec.Code)
   123  }