github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/codec/codec_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/codec/codec_test.go 14 15 package codec_test 16 17 import ( 18 "io" 19 "testing" 20 21 "github.com/tickoalcantara12/micro/v3/util/codec" 22 "github.com/tickoalcantara12/micro/v3/util/codec/bytes" 23 "github.com/tickoalcantara12/micro/v3/util/codec/grpc" 24 "github.com/tickoalcantara12/micro/v3/util/codec/json" 25 "github.com/tickoalcantara12/micro/v3/util/codec/jsonrpc" 26 "github.com/tickoalcantara12/micro/v3/util/codec/proto" 27 "github.com/tickoalcantara12/micro/v3/util/codec/protorpc" 28 "github.com/tickoalcantara12/micro/v3/util/codec/text" 29 ) 30 31 type testRWC struct{} 32 33 func (rwc *testRWC) Read(p []byte) (n int, err error) { 34 return 0, nil 35 } 36 37 func (rwc *testRWC) Write(p []byte) (n int, err error) { 38 return 0, nil 39 } 40 41 func (rwc *testRWC) Close() error { 42 return nil 43 } 44 45 func getCodecs(c io.ReadWriteCloser) map[string]codec.Codec { 46 return map[string]codec.Codec{ 47 "bytes": bytes.NewCodec(c), 48 "grpc": grpc.NewCodec(c), 49 "json": json.NewCodec(c), 50 "jsonrpc": jsonrpc.NewCodec(c), 51 "proto": proto.NewCodec(c), 52 "protorpc": protorpc.NewCodec(c), 53 "text": text.NewCodec(c), 54 } 55 } 56 57 func Test_WriteEmptyBody(t *testing.T) { 58 for name, c := range getCodecs(&testRWC{}) { 59 err := c.Write(&codec.Message{ 60 Type: codec.Error, 61 Header: map[string]string{}, 62 }, nil) 63 if err != nil { 64 t.Fatalf("codec %s - expected no error when writing empty/nil body: %s", name, err) 65 } 66 } 67 }