go.uber.org/yarpc@v1.72.1/api/middleware/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 middleware_test 22 23 import ( 24 "bytes" 25 "context" 26 "errors" 27 "testing" 28 29 "go.uber.org/yarpc/api/middleware" 30 "go.uber.org/yarpc/api/transport" 31 "go.uber.org/yarpc/api/transport/transporttest" 32 "go.uber.org/yarpc/encoding/raw" 33 "go.uber.org/yarpc/internal/testtime" 34 35 "github.com/golang/mock/gomock" 36 "github.com/stretchr/testify/assert" 37 "github.com/stretchr/testify/require" 38 ) 39 40 func TestUnaryNopInboundMiddleware(t *testing.T) { 41 mockCtrl := gomock.NewController(t) 42 defer mockCtrl.Finish() 43 44 h := transporttest.NewMockUnaryHandler(mockCtrl) 45 wrappedH := middleware.ApplyUnaryInbound(h, middleware.NopUnaryInbound) 46 47 ctx, cancel := context.WithTimeout(context.Background(), testtime.Second) 48 defer cancel() 49 req := &transport.Request{ 50 Caller: "somecaller", 51 Service: "someservice", 52 Encoding: raw.Encoding, 53 Procedure: "hello", 54 Body: bytes.NewReader([]byte{1, 2, 3}), 55 } 56 resw := new(transporttest.FakeResponseWriter) 57 err := errors.New("great sadness") 58 h.EXPECT().Handle(ctx, req, resw).Return(err) 59 60 assert.Equal(t, err, wrappedH.Handle(ctx, req, resw)) 61 } 62 63 func TestOnewayNopInboundMiddleware(t *testing.T) { 64 mockCtrl := gomock.NewController(t) 65 defer mockCtrl.Finish() 66 67 h := transporttest.NewMockOnewayHandler(mockCtrl) 68 wrappedH := middleware.ApplyOnewayInbound(h, middleware.NopOnewayInbound) 69 70 ctx, cancel := context.WithTimeout(context.Background(), testtime.Second) 71 defer cancel() 72 req := &transport.Request{ 73 Caller: "somecaller", 74 Service: "someservice", 75 Encoding: raw.Encoding, 76 Procedure: "hello", 77 Body: bytes.NewReader([]byte{1, 2, 3}), 78 } 79 err := errors.New("great sadness") 80 h.EXPECT().HandleOneway(ctx, req).Return(err) 81 82 assert.Equal(t, err, wrappedH.HandleOneway(ctx, req)) 83 } 84 85 func TestNilInboundMiddleware(t *testing.T) { 86 ctrl := gomock.NewController(t) 87 defer ctrl.Finish() 88 89 ctx := context.Background() 90 req := &transport.Request{} 91 92 t.Run("unary", func(t *testing.T) { 93 handler := transporttest.NewMockUnaryHandler(ctrl) 94 mw := middleware.ApplyUnaryInbound(handler, nil) 95 96 resWriter := &transporttest.FakeResponseWriter{} 97 98 handler.EXPECT().Handle(ctx, req, resWriter) 99 err := mw.Handle(ctx, req, resWriter) 100 require.NoError(t, err, "unexpected error calling handler") 101 }) 102 103 t.Run("oneway", func(t *testing.T) { 104 handler := transporttest.NewMockOnewayHandler(ctrl) 105 mw := middleware.ApplyOnewayInbound(handler, nil) 106 107 handler.EXPECT().HandleOneway(ctx, req) 108 err := mw.HandleOneway(ctx, req) 109 require.NoError(t, err, "unexpected error calling handler") 110 }) 111 } 112 113 func TestStreamNopInboundMiddleware(t *testing.T) { 114 mockCtrl := gomock.NewController(t) 115 defer mockCtrl.Finish() 116 117 h := transporttest.NewMockStreamHandler(mockCtrl) 118 wrappedH := middleware.ApplyStreamInbound(h, middleware.NopStreamInbound) 119 s, err := transport.NewServerStream(transporttest.NewMockStream(mockCtrl)) 120 require.NoError(t, err) 121 122 err = errors.New("great sadness") 123 h.EXPECT().HandleStream(s).Return(err) 124 125 assert.Equal(t, err, wrappedH.HandleStream(s)) 126 } 127 128 func TestStreamDefaultsToHandlerWhenNil(t *testing.T) { 129 mockCtrl := gomock.NewController(t) 130 defer mockCtrl.Finish() 131 132 h := transporttest.NewMockStreamHandler(mockCtrl) 133 wrappedH := middleware.ApplyStreamInbound(h, nil) 134 assert.Equal(t, wrappedH, h) 135 }