go.uber.org/yarpc@v1.72.1/encoding/protobuf/outbound_unit_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 22 23 import ( 24 "context" 25 "testing" 26 27 "github.com/golang/mock/gomock" 28 "github.com/stretchr/testify/assert" 29 "go.uber.org/yarpc" 30 "go.uber.org/yarpc/api/transport" 31 "go.uber.org/yarpc/api/transport/transporttest" 32 "go.uber.org/yarpc/yarpcerrors" 33 ) 34 35 func TestInvalidOutboundEncoding(t *testing.T) { 36 client := newClient("foo", &transport.OutboundConfig{CallerName: "foo", Outbounds: transport.Outbounds{ServiceName: "bar"}}, nil /*AnyResolver*/) 37 _, _, _, _, err := client.buildTransportRequest(context.Background(), "hello", nil, nil) 38 assert.NoError(t, err) 39 client.encoding = "bat" 40 _, _, _, _, err = client.buildTransportRequest(context.Background(), "hello", nil, nil) 41 assert.Equal(t, yarpcerrors.CodeInternal, yarpcerrors.FromError(err).Code()) 42 } 43 44 func TestNonOutboundConfigWithUnaryClient(t *testing.T) { 45 mockCtrl := gomock.NewController(t) 46 defer mockCtrl.Finish() 47 48 cc := transporttest.NewMockClientConfig(mockCtrl) 49 cc.EXPECT().Caller().Return("caller") 50 cc.EXPECT().Service().Return("service") 51 cc.EXPECT().GetUnaryOutbound().Return(transporttest.NewMockUnaryOutbound(mockCtrl)) 52 53 assert.NotPanics(t, func() { 54 newClient("test", cc, nil /*AnyResolver*/) 55 }) 56 } 57 58 func TestNonOutboundConfigClient(t *testing.T) { 59 mockCtrl := gomock.NewController(t) 60 defer mockCtrl.Finish() 61 62 cc := transporttest.NewMockClientConfig(mockCtrl) 63 cc.EXPECT().Caller().Return("caller") 64 cc.EXPECT().Service().Return("service") 65 cc.EXPECT().GetUnaryOutbound().Do(func() { panic("bad times") }) 66 67 assert.Panics(t, func() { 68 newClient("test", cc, nil /*AnyResolver*/) 69 }) 70 } 71 72 func TestInvalidStreamClientEncoding(t *testing.T) { 73 client := &client{ 74 serviceName: "test", 75 outboundConfig: &transport.OutboundConfig{ 76 Outbounds: transport.Outbounds{}, 77 }, 78 encoding: transport.Encoding("raw"), 79 } 80 81 _, err := client.CallStream(context.Background(), "somemethod") 82 83 assert.Contains(t, err.Error(), "code:internal") 84 assert.Contains(t, err.Error(), "can only use encodings") 85 } 86 87 func TestNoStreamOutbound(t *testing.T) { 88 client := &client{ 89 serviceName: "test", 90 outboundConfig: &transport.OutboundConfig{ 91 Outbounds: transport.Outbounds{}, 92 }, 93 encoding: Encoding, 94 } 95 96 _, err := client.CallStream(context.Background(), "somemethod") 97 98 assert.Contains(t, err.Error(), "code:internal") 99 assert.Contains(t, err.Error(), "no stream outbounds for OutboundConfig") 100 } 101 102 func TestNoResponseHeaders(t *testing.T) { 103 client := &client{ 104 serviceName: "test", 105 outboundConfig: &transport.OutboundConfig{ 106 Outbounds: transport.Outbounds{}, 107 }, 108 encoding: Encoding, 109 } 110 111 headers := make(map[string]string) 112 113 _, err := client.CallStream(context.Background(), "somemethod", yarpc.ResponseHeaders(&headers)) 114 115 assert.Contains(t, err.Error(), "code:invalid-argument") 116 assert.Contains(t, err.Error(), "response headers are not supported for streams") 117 }