github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/tools/configtxlator/rest/protolator_handlers_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package rest 18 19 import ( 20 "bytes" 21 "fmt" 22 "net/http" 23 "net/http/httptest" 24 "strings" 25 "testing" 26 27 cb "github.com/hyperledger/fabric/protos/common" 28 "github.com/hyperledger/fabric/protos/utils" 29 30 "github.com/golang/protobuf/proto" 31 "github.com/stretchr/testify/assert" 32 ) 33 34 var ( 35 testProto = &cb.Block{ 36 Header: &cb.BlockHeader{ 37 PreviousHash: []byte("foo"), 38 }, 39 Data: &cb.BlockData{ 40 Data: [][]byte{ 41 utils.MarshalOrPanic(&cb.Envelope{ 42 Signature: []byte("bar"), 43 }), 44 }, 45 }, 46 } 47 48 testOutput = `{"data":{"data":[{"signature":"YmFy"}]},"header":{"previous_hash":"Zm9v"}}` 49 ) 50 51 func TestProtolatorDecode(t *testing.T) { 52 data, err := proto.Marshal(testProto) 53 assert.NoError(t, err) 54 55 url := fmt.Sprintf("/protolator/decode/%s", proto.MessageName(testProto)) 56 57 req, _ := http.NewRequest("POST", url, bytes.NewReader(data)) 58 rec := httptest.NewRecorder() 59 r := NewRouter() 60 r.ServeHTTP(rec, req) 61 62 assert.Equal(t, http.StatusOK, rec.Code) 63 64 // Remove all the whitespace 65 compactJSON := strings.Replace(strings.Replace(strings.Replace(rec.Body.String(), "\n", "", -1), "\t", "", -1), " ", "", -1) 66 67 assert.Equal(t, testOutput, compactJSON) 68 } 69 70 func TestProtolatorEncode(t *testing.T) { 71 72 url := fmt.Sprintf("/protolator/encode/%s", proto.MessageName(testProto)) 73 74 req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte(testOutput))) 75 rec := httptest.NewRecorder() 76 r := NewRouter() 77 r.ServeHTTP(rec, req) 78 79 assert.Equal(t, http.StatusOK, rec.Code) 80 81 outputMsg := &cb.Block{} 82 83 err := proto.Unmarshal(rec.Body.Bytes(), outputMsg) 84 assert.NoError(t, err) 85 assert.Equal(t, testProto, outputMsg) 86 } 87 88 func TestProtolatorDecodeNonExistantProto(t *testing.T) { 89 req, _ := http.NewRequest("POST", "/protolator/decode/NonExistantMsg", bytes.NewReader([]byte{})) 90 rec := httptest.NewRecorder() 91 r := NewRouter() 92 r.ServeHTTP(rec, req) 93 94 assert.Equal(t, http.StatusNotFound, rec.Code) 95 } 96 97 func TestProtolatorEncodeNonExistantProto(t *testing.T) { 98 req, _ := http.NewRequest("POST", "/protolator/encode/NonExistantMsg", bytes.NewReader([]byte{})) 99 rec := httptest.NewRecorder() 100 r := NewRouter() 101 r.ServeHTTP(rec, req) 102 103 assert.Equal(t, http.StatusNotFound, rec.Code) 104 } 105 106 func TestProtolatorDecodeBadData(t *testing.T) { 107 url := fmt.Sprintf("/protolator/decode/%s", proto.MessageName(testProto)) 108 109 req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte("Garbage"))) 110 111 rec := httptest.NewRecorder() 112 r := NewRouter() 113 r.ServeHTTP(rec, req) 114 115 assert.Equal(t, http.StatusBadRequest, rec.Code) 116 } 117 118 func TestProtolatorEncodeBadData(t *testing.T) { 119 url := fmt.Sprintf("/protolator/encode/%s", proto.MessageName(testProto)) 120 121 req, _ := http.NewRequest("POST", url, bytes.NewReader([]byte("Garbage"))) 122 123 rec := httptest.NewRecorder() 124 r := NewRouter() 125 r.ServeHTTP(rec, req) 126 127 assert.Equal(t, http.StatusBadRequest, rec.Code) 128 }