go.uber.org/yarpc@v1.72.1/encoding/protobuf/outbound_test.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package protobuf_test 22 23 import ( 24 "context" 25 "io/ioutil" 26 "testing" 27 28 "github.com/gogo/protobuf/jsonpb" 29 "github.com/gogo/protobuf/proto" 30 "github.com/gogo/protobuf/types" 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 "go.uber.org/yarpc/api/transport" 34 "go.uber.org/yarpc/encoding/protobuf" 35 "go.uber.org/yarpc/encoding/protobuf/internal/testpb" 36 "go.uber.org/yarpc/yarpctest" 37 ) 38 39 var _ jsonpb.AnyResolver = (*testResolver)(nil) 40 41 func TestOutboundAnyResolver(t *testing.T) { 42 const testValue = "foo-bar-baz" 43 newReq := func() proto.Message { return &testpb.TestMessage{} } 44 customResolver := &testResolver{NewMessage: newReq} 45 46 tests := []struct { 47 name string 48 anyURL string 49 resolver jsonpb.AnyResolver 50 wantErr bool 51 }{ 52 { 53 name: "nothing custom", 54 anyURL: "uber.yarpc.encoding.protobuf.TestMessage", 55 }, 56 { 57 name: "custom resolver", 58 anyURL: "uber.yarpc.encoding.protobuf.TestMessage", 59 resolver: customResolver, 60 }, 61 { 62 name: "custom resolver, custom URL", 63 anyURL: "foo.bar.baz", 64 resolver: customResolver, 65 }, 66 { 67 name: "custom URL, no resolver", 68 anyURL: "foo.bar.baz", 69 wantErr: true, 70 }, 71 } 72 73 for _, tt := range tests { 74 t.Run(tt.name, func(t *testing.T) { 75 trans := yarpctest.NewFakeTransport() 76 // outbound that echos the body back 77 out := trans.NewOutbound(nil, yarpctest.OutboundCallOverride( 78 yarpctest.OutboundCallable(func(ctx context.Context, req *transport.Request) (*transport.Response, error) { 79 return &transport.Response{Body: ioutil.NopCloser(req.Body)}, nil 80 }), 81 )) 82 83 client := protobuf.NewClient(protobuf.ClientParams{ 84 ClientConfig: &transport.OutboundConfig{ 85 Outbounds: transport.Outbounds{ 86 Unary: out, 87 }, 88 }, 89 AnyResolver: tt.resolver, 90 Options: []protobuf.ClientOption{protobuf.UseJSON}, 91 }) 92 93 testMessage := &testpb.TestMessage{Value: testValue} 94 95 // convert to an Any so that the marshaller will use the custom resolver 96 any, err := types.MarshalAny(testMessage) 97 require.NoError(t, err) 98 any.TypeUrl = tt.anyURL // update to custom URL 99 100 gotMessage, err := client.Call(context.Background(), "", any, newReq) 101 if tt.wantErr { 102 require.Error(t, err) 103 } else { 104 require.NoError(t, err) 105 assert.Equal(t, testMessage, gotMessage) // we expect the actual type behind the Any 106 } 107 }) 108 } 109 }