|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/opentracing/opentracing-go" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "github.com/yarpc/yab/encoding" |
| 14 | + yabtransport "github.com/yarpc/yab/transport" |
| 15 | + |
| 16 | + apitransport "go.uber.org/yarpc/api/transport" |
| 17 | + yarpcgrpc "go.uber.org/yarpc/transport/grpc" |
| 18 | +) |
| 19 | + |
| 20 | +type unaryHandlerFunc func(context.Context, *apitransport.Request, apitransport.ResponseWriter) error |
| 21 | + |
| 22 | +func (f unaryHandlerFunc) Handle(ctx context.Context, req *apitransport.Request, resw apitransport.ResponseWriter) error { |
| 23 | + return f(ctx, req, resw) |
| 24 | +} |
| 25 | + |
| 26 | +type encodingCaptureRouter struct { |
| 27 | + expectedService string |
| 28 | + expectedProcedure string |
| 29 | + capturedEncoding chan string |
| 30 | +} |
| 31 | + |
| 32 | +func (r *encodingCaptureRouter) Procedures() []apitransport.Procedure { |
| 33 | + return nil |
| 34 | +} |
| 35 | + |
| 36 | +func (r *encodingCaptureRouter) Choose(_ context.Context, req *apitransport.Request) (apitransport.HandlerSpec, error) { |
| 37 | + if req.Service == r.expectedService && req.Procedure == r.expectedProcedure { |
| 38 | + select { |
| 39 | + case r.capturedEncoding <- string(req.Encoding): |
| 40 | + default: |
| 41 | + } |
| 42 | + return apitransport.NewUnaryHandlerSpec(unaryHandlerFunc(func(_ context.Context, _ *apitransport.Request, resw apitransport.ResponseWriter) error { |
| 43 | + _, _ = resw.Write([]byte("ok")) |
| 44 | + return nil |
| 45 | + })), nil |
| 46 | + } |
| 47 | + return apitransport.HandlerSpec{}, apitransport.UnrecognizedProcedureError(req) |
| 48 | +} |
| 49 | + |
| 50 | +func TestRPCEncodingFlagOverridesHTTPHeader(t *testing.T) { |
| 51 | + const want = "dev-override" |
| 52 | + |
| 53 | + var got string |
| 54 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 55 | + got = r.Header.Get("RPC-Encoding") |
| 56 | + w.WriteHeader(http.StatusOK) |
| 57 | + _, _ = w.Write([]byte("ok")) |
| 58 | + })) |
| 59 | + defer srv.Close() |
| 60 | + |
| 61 | + opts := TransportOptions{ |
| 62 | + ServiceName: "svc", |
| 63 | + CallerName: "caller", |
| 64 | + Peers: []string{srv.URL}, |
| 65 | + HTTPMethod: "POST", |
| 66 | + RPCEncoding: want, |
| 67 | + } |
| 68 | + |
| 69 | + tp, err := getTransport(opts, resolvedProtocolEncoding{protocol: yabtransport.HTTP, enc: encoding.JSON}, opentracing.NoopTracer{}) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + _, err = tp.Call(context.Background(), &yabtransport.Request{Method: "Foo::Bar", Body: []byte("hello")}) |
| 73 | + require.NoError(t, err) |
| 74 | + assert.Equal(t, want, got) |
| 75 | +} |
| 76 | + |
| 77 | +func TestRPCEncodingFlagOverridesGRPCHeader(t *testing.T) { |
| 78 | + const want = "dev-override" |
| 79 | + |
| 80 | + gt := yarpcgrpc.NewTransport(yarpcgrpc.Tracer(opentracing.NoopTracer{})) |
| 81 | + require.NoError(t, gt.Start()) |
| 82 | + defer func() { _ = gt.Stop() }() |
| 83 | + |
| 84 | + lis, err := net.Listen("tcp", "127.0.0.1:0") |
| 85 | + require.NoError(t, err) |
| 86 | + defer func() { _ = lis.Close() }() |
| 87 | + |
| 88 | + router := &encodingCaptureRouter{ |
| 89 | + expectedService: "svc", |
| 90 | + expectedProcedure: "svc::echo", |
| 91 | + capturedEncoding: make(chan string, 1), |
| 92 | + } |
| 93 | + |
| 94 | + inbound := gt.NewInbound(lis) |
| 95 | + inbound.SetRouter(router) |
| 96 | + require.NoError(t, inbound.Start()) |
| 97 | + defer func() { _ = inbound.Stop() }() |
| 98 | + |
| 99 | + opts := TransportOptions{ |
| 100 | + ServiceName: "svc", |
| 101 | + CallerName: "caller", |
| 102 | + Peers: []string{lis.Addr().String()}, |
| 103 | + RPCEncoding: want, |
| 104 | + } |
| 105 | + tp, err := getTransport(opts, resolvedProtocolEncoding{protocol: yabtransport.GRPC, enc: encoding.Protobuf}, opentracing.NoopTracer{}) |
| 106 | + require.NoError(t, err) |
| 107 | + |
| 108 | + _, err = tp.Call(context.Background(), &yabtransport.Request{TargetService: "svc", Method: "svc::echo", Body: []byte("hello")}) |
| 109 | + require.NoError(t, err) |
| 110 | + |
| 111 | + select { |
| 112 | + case got := <-router.capturedEncoding: |
| 113 | + assert.Equal(t, want, got) |
| 114 | + default: |
| 115 | + t.Fatal("did not capture inbound encoding") |
| 116 | + } |
| 117 | +} |
0 commit comments