go.uber.org/yarpc@v1.72.1/encoding/protobuf/inbound_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 "bytes" 25 "context" 26 "errors" 27 "testing" 28 29 "github.com/gogo/protobuf/jsonpb" 30 "github.com/gogo/protobuf/proto" 31 "github.com/gogo/protobuf/types" 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34 "go.uber.org/yarpc/api/transport" 35 "go.uber.org/yarpc/api/transport/transporttest" 36 "go.uber.org/yarpc/encoding/protobuf" 37 "go.uber.org/yarpc/encoding/protobuf/internal/testpb" 38 ) 39 40 var _ jsonpb.AnyResolver = (*testResolver)(nil) 41 42 type testResolver struct { 43 NewMessage func() proto.Message 44 } 45 46 func (t *testResolver) Resolve(url string) (proto.Message, error) { 47 if t.NewMessage == nil { 48 return nil, errors.New("test resolver is not initialized") 49 } 50 return t.NewMessage(), nil 51 } 52 53 func TestInboundAnyResolver(t *testing.T) { 54 newReq := func() proto.Message { return &testpb.TestMessage{} } 55 customResolver := &testResolver{NewMessage: newReq} 56 57 tests := []struct { 58 name string 59 anyURL string 60 resolver jsonpb.AnyResolver 61 wantErr bool 62 }{ 63 { 64 name: "nothing custom", 65 anyURL: "uber.yarpc.encoding.protobuf.TestMessage", 66 }, 67 { 68 name: "custom resolver", 69 anyURL: "uber.yarpc.encoding.protobuf.TestMessage", 70 resolver: customResolver, 71 }, 72 { 73 name: "custom resolver, custom URL", 74 anyURL: "foo.bar.baz", 75 resolver: customResolver, 76 }, 77 { 78 name: "custom URL, no resolver", 79 anyURL: "foo.bar.baz", 80 wantErr: true, 81 }, 82 } 83 84 for _, tt := range tests { 85 t.Run(tt.name, func(t *testing.T) { 86 handler := protobuf.NewUnaryHandler(protobuf.UnaryHandlerParams{ 87 Handle: func(context.Context, proto.Message) (proto.Message, error) { 88 testMessage := &testpb.TestMessage{Value: "foo-bar-baz"} 89 90 any, err := types.MarshalAny(testMessage) 91 assert.NoError(t, err) 92 any.TypeUrl = tt.anyURL // update to custom URL 93 return any, nil 94 }, 95 NewRequest: newReq, 96 AnyResolver: tt.resolver, 97 }) 98 99 req := &transport.Request{ 100 Encoding: protobuf.JSONEncoding, 101 Body: bytes.NewReader(nil), 102 } 103 104 var resw transporttest.FakeResponseWriter 105 err := handler.Handle(context.Background(), req, &resw) 106 107 if tt.wantErr { 108 require.Error(t, err) 109 } else { 110 require.NoError(t, err) 111 } 112 }) 113 } 114 }