go.uber.org/yarpc@v1.72.1/api/encoding/inbound_call_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 encoding
    22  
    23  import (
    24  	"context"
    25  	"sort"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  	"go.uber.org/yarpc/api/transport"
    31  	"go.uber.org/yarpc/api/transport/transporttest"
    32  )
    33  
    34  func TestInboundCallReadFromRequest(t *testing.T) {
    35  	ctx, inboundCall := NewInboundCall(context.Background())
    36  	err := inboundCall.ReadFromRequest(&transport.Request{
    37  		Caller:    "caller",
    38  		Service:   "service",
    39  		Encoding:  transport.Encoding("raw"),
    40  		Procedure: "hello",
    41  		Headers: transport.HeadersFromMap(map[string]string{
    42  			"hello":   "World",
    43  			"Foo":     "bar",
    44  			"success": "true",
    45  		}),
    46  		ShardKey:        "shardKey",
    47  		RoutingKey:      "routingKey",
    48  		RoutingDelegate: "routingDelegate",
    49  		CallerProcedure: "callerProcedure",
    50  	})
    51  	require.NoError(t, err)
    52  
    53  	call := CallFromContext(ctx)
    54  	assert.Equal(t, "caller", call.Caller())
    55  	assert.Equal(t, "service", call.Service())
    56  	assert.Equal(t, "raw", string(call.Encoding()))
    57  	assert.Equal(t, "hello", call.Procedure())
    58  	assert.Equal(t, "shardKey", call.ShardKey())
    59  	assert.Equal(t, "routingKey", call.RoutingKey())
    60  	assert.Equal(t, "routingDelegate", call.RoutingDelegate())
    61  	assert.Equal(t, "callerProcedure", call.CallerProcedure())
    62  	assert.Equal(t, "World", call.Header("Hello"))
    63  	assert.Equal(t, "bar", call.Header("FOO"))
    64  	assert.Equal(t, "true", call.Header("success"))
    65  	assert.Equal(t, "", call.Header("does-not-exist"))
    66  
    67  	headerNames := call.HeaderNames()
    68  	sort.Strings(headerNames)
    69  	assert.Equal(t, []string{"foo", "hello", "success"}, headerNames)
    70  }
    71  
    72  func TestInboundCallWriteToResponse(t *testing.T) {
    73  	tests := []struct {
    74  		desc        string
    75  		sendHeaders map[string]string
    76  		wantHeaders transport.Headers
    77  	}{
    78  		{
    79  			desc: "no headers",
    80  		},
    81  	}
    82  
    83  	for _, tt := range tests {
    84  		t.Run(tt.desc, func(t *testing.T) {
    85  			ctx, inboundCall := NewInboundCall(context.Background())
    86  			call := CallFromContext(ctx)
    87  			for k, v := range tt.sendHeaders {
    88  				call.WriteResponseHeader(k, v)
    89  			}
    90  
    91  			var resw transporttest.FakeResponseWriter
    92  			assert.NoError(t, inboundCall.WriteToResponse(&resw))
    93  			assert.Equal(t, tt.wantHeaders, resw.Headers)
    94  		})
    95  	}
    96  }