go.uber.org/yarpc@v1.72.1/internal/firstoutboundmiddleware/middleware_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 firstoutboundmiddleware_test
    22  
    23  import (
    24  	"context"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  	"go.uber.org/yarpc/api/middleware"
    30  	"go.uber.org/yarpc/api/transport"
    31  	"go.uber.org/yarpc/internal/firstoutboundmiddleware"
    32  	"go.uber.org/yarpc/yarpctest"
    33  )
    34  
    35  func TestFirstOutboundMidleware(t *testing.T) {
    36  	out := yarpctest.NewFakeTransport().NewOutbound(nil,
    37  		yarpctest.OutboundCallOverride(
    38  			func(context.Context, *transport.Request) (*transport.Response, error) { return nil, nil },
    39  		),
    40  		yarpctest.OutboundCallStreamOverride(
    41  			func(context.Context, *transport.StreamRequest) (*transport.ClientStream, error) { return nil, nil },
    42  		),
    43  		yarpctest.OutboundCallOnewayOverride(
    44  			func(context.Context, *transport.Request) (transport.Ack, error) { return nil, nil },
    45  		),
    46  	)
    47  
    48  	t.Run("unary", func(t *testing.T) {
    49  		req := &transport.Request{Transport: "", CallerProcedure: "" /* not set */}
    50  
    51  		outWithMiddleware := middleware.ApplyUnaryOutbound(out, firstoutboundmiddleware.New())
    52  		ctx := yarpctest.ContextWithCall(context.Background(), &yarpctest.Call{Transport: "", Procedure: "ABC"})
    53  		_, err := outWithMiddleware.Call(ctx, req)
    54  		require.NoError(t, err)
    55  
    56  		assert.Equal(t, "fake", string(req.Transport))
    57  		assert.Equal(t, "ABC", string(req.CallerProcedure))
    58  	})
    59  
    60  	t.Run("oneway", func(t *testing.T) {
    61  		req := &transport.Request{Transport: "" /* not set */}
    62  
    63  		outWithMiddleware := middleware.ApplyOnewayOutbound(out, firstoutboundmiddleware.New())
    64  		ctx := yarpctest.ContextWithCall(context.Background(), &yarpctest.Call{Transport: "", Procedure: "ABC"})
    65  		_, err := outWithMiddleware.CallOneway(ctx, req)
    66  		require.NoError(t, err)
    67  
    68  		assert.Equal(t, "fake", string(req.Transport))
    69  		assert.Equal(t, "ABC", string(req.CallerProcedure))
    70  	})
    71  
    72  	t.Run("stream", func(t *testing.T) {
    73  		streamReq := &transport.StreamRequest{Meta: &transport.RequestMeta{Transport: "" /* not set */}}
    74  
    75  		outWithMiddleware := middleware.ApplyStreamOutbound(out, firstoutboundmiddleware.New())
    76  		ctx := yarpctest.ContextWithCall(context.Background(), &yarpctest.Call{Transport: "", Procedure: "ABC"})
    77  		_, err := outWithMiddleware.CallStream(ctx, streamReq)
    78  		require.NoError(t, err)
    79  
    80  		assert.Equal(t, "fake", string(streamReq.Meta.Transport))
    81  		assert.Equal(t, "ABC", string(streamReq.Meta.CallerProcedure))
    82  	})
    83  }