go.uber.org/yarpc@v1.72.1/encoding/protobuf/v2/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 v2_test
    22  
    23  import (
    24  	"bytes"
    25  	"context"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  	"go.uber.org/yarpc/api/transport"
    30  	"go.uber.org/yarpc/api/transport/transporttest"
    31  	"go.uber.org/yarpc/encoding/protobuf/internal/testpb/v2"
    32  	"go.uber.org/yarpc/encoding/protobuf/v2"
    33  	"google.golang.org/protobuf/proto"
    34  	"google.golang.org/protobuf/types/known/anypb"
    35  )
    36  
    37  func TestInboundAnyResolver(t *testing.T) {
    38  	newReq := func() proto.Message { return &testpb.TestMessage{} }
    39  	customAnyResolver := &testAnyResolver{NewMessage: &testpb.TestMessage{}}
    40  	tests := []struct {
    41  		name     string
    42  		anyURL   string
    43  		resolver v2.AnyResolver
    44  	}{
    45  		{
    46  			name:   "nothing custom",
    47  			anyURL: "uber.yarpc.encoding.protobuf.TestMessage",
    48  		},
    49  		{
    50  			name:     "custom resolver",
    51  			anyURL:   "uber.yarpc.encoding.protobuf.TestMessage",
    52  			resolver: customAnyResolver,
    53  		},
    54  		{
    55  			name:     "custom resolver, custom URL",
    56  			anyURL:   "foo.bar.baz",
    57  			resolver: customAnyResolver,
    58  		},
    59  	}
    60  
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			handler := v2.NewUnaryHandler(v2.UnaryHandlerParams{
    64  				Handle: func(context.Context, proto.Message) (proto.Message, error) {
    65  					testMessage := &testpb.TestMessage{Value: "foo-bar-baz"}
    66  					any, err := anypb.New(testMessage)
    67  					require.NoError(t, err)
    68  					any.TypeUrl = tt.anyURL // update to custom URL
    69  					return any, nil
    70  				},
    71  				NewRequest:  newReq,
    72  				AnyResolver: tt.resolver,
    73  			})
    74  
    75  			req := &transport.Request{
    76  				Encoding: v2.Encoding,
    77  				Body:     bytes.NewReader(nil),
    78  			}
    79  
    80  			var resw transporttest.FakeResponseWriter
    81  			err := handler.Handle(context.Background(), req, &resw)
    82  			require.NoError(t, err)
    83  		})
    84  	}
    85  }