go.uber.org/yarpc@v1.72.1/encoding/thrift/thriftrw-plugin-yarpc/sanitization_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 main_test
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  	tchannel "github.com/uber/tchannel-go"
    30  	tutils "github.com/uber/tchannel-go/testutils"
    31  	"go.uber.org/yarpc/api/transport"
    32  	wc "go.uber.org/yarpc/encoding/thrift/thriftrw-plugin-yarpc/internal/tests/weather/weatherclient"
    33  	ytchannel "go.uber.org/yarpc/transport/tchannel"
    34  	"go.uber.org/yarpc/yarpcerrors"
    35  	"golang.org/x/net/context"
    36  )
    37  
    38  func TestSanitization(t *testing.T) {
    39  	copts := tutils.NewOpts().DisableLogVerification()
    40  	copts.DisableRelay = true
    41  
    42  	var handlerWasCalled bool
    43  	copts.Handler = tchannel.HandlerFunc(func(ctx context.Context, call *tchannel.InboundCall) {
    44  		headered := ctx.(tchannel.ContextWithHeaders)
    45  		assert.Len(t, headered.Headers(), 0)
    46  		handlerWasCalled = true
    47  		call.Response().SendSystemError(
    48  			tchannel.NewSystemError(tchannel.ErrCodeBadRequest, "infinite sadness"),
    49  		)
    50  	})
    51  
    52  	server := tutils.NewTestServer(t, copts)
    53  
    54  	client, done := newWeatherClient(t, server.HostPort())
    55  	defer done()
    56  
    57  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    58  	defer cancel()
    59  
    60  	_, err := client.Check(tchannel.WrapWithHeaders(ctx, map[string]string{"key": "value"}))
    61  	require.Error(t, err, "expected Check to fail")
    62  	assert.Equal(t, yarpcerrors.CodeInvalidArgument, yarpcerrors.FromError(err).Code(),
    63  		"error code must match")
    64  	assert.True(t, handlerWasCalled, "newTestServer handler was never called")
    65  }
    66  
    67  func newWeatherClient(t *testing.T, hostPort string) (_ wc.Interface, done func()) {
    68  	trans, err := ytchannel.NewTransport(ytchannel.ServiceName(tutils.DefaultClientName))
    69  	require.NoError(t, err)
    70  	require.NoError(t, trans.Start(), "failed to start transport")
    71  
    72  	outbound := trans.NewSingleOutbound(hostPort)
    73  	require.NoError(t, outbound.Start(), "failed to start outbound")
    74  
    75  	cc := testClientConfig{outbound: outbound}
    76  	return wc.New(cc), func() {
    77  		assert.NoError(t, outbound.Stop(), "failed to stop outbound")
    78  		assert.NoError(t, trans.Stop(), "failed to stop transport")
    79  	}
    80  }
    81  
    82  type testClientConfig struct {
    83  	outbound transport.UnaryOutbound
    84  }
    85  
    86  func (cc testClientConfig) Caller() string {
    87  	return tutils.DefaultClientName
    88  }
    89  
    90  func (cc testClientConfig) Service() string {
    91  	return tutils.DefaultServerName
    92  }
    93  
    94  func (cc testClientConfig) GetUnaryOutbound() transport.UnaryOutbound {
    95  	return cc.outbound
    96  }
    97  
    98  func (cc testClientConfig) GetOnewayOutbound() transport.OnewayOutbound {
    99  	panic("Not implemented")
   100  }