go.uber.org/yarpc@v1.72.1/x/yarpctest/types/header.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 types 22 23 import ( 24 "context" 25 26 "github.com/stretchr/testify/require" 27 "go.uber.org/yarpc/api/transport" 28 "go.uber.org/yarpc/x/yarpctest/api" 29 ) 30 31 // GiveHeader is an API for giving headers to a Request or a Response. 32 type GiveHeader struct { 33 api.NoopLifecycle 34 35 Key string 36 Value string 37 } 38 39 // ApplyClientStreamRequest implements api.ClientStreamRequestOption. 40 func (h *GiveHeader) ApplyClientStreamRequest(opts *api.ClientStreamRequestOpts) { 41 opts.GiveRequest.Meta.Headers = opts.GiveRequest.Meta.Headers.With(h.Key, h.Value) 42 } 43 44 // ApplyRequest implements RequestOption. 45 func (h *GiveHeader) ApplyRequest(opts *api.RequestOpts) { 46 opts.GiveRequest.Headers = opts.GiveRequest.Headers.With(h.Key, h.Value) 47 } 48 49 // Handle implements middleware.UnaryInbound. 50 func (h *GiveHeader) Handle(ctx context.Context, req *transport.Request, resw transport.ResponseWriter, handler transport.UnaryHandler) error { 51 err := handler.Handle(ctx, req, resw) 52 resw.AddHeaders(transport.NewHeaders().With(h.Key, h.Value)) 53 return err 54 } 55 56 // WantHeader is an API for asserting headers sent on a Request or a Response. 57 type WantHeader struct { 58 api.SafeTestingTBOnStart 59 api.NoopStop 60 61 Key string 62 Value string 63 } 64 65 // ApplyServerStream implements ServerStreamAction. 66 func (h *WantHeader) ApplyServerStream(c *transport.ServerStream) error { 67 actualValue, ok := c.Request().Meta.Headers.Get(h.Key) 68 require.True(h.GetTestingTB(), ok, "header %q was not set on the request", h.Key) 69 require.Equal(h.GetTestingTB(), actualValue, h.Value, "headers did not match for %q", h.Key) 70 return nil 71 } 72 73 // ApplyRequest implements RequestOption. 74 func (h *WantHeader) ApplyRequest(opts *api.RequestOpts) { 75 opts.WantResponse.Headers = opts.WantResponse.Headers.With(h.Key, h.Value) 76 } 77 78 // Handle implements middleware.UnaryInbound. 79 func (h *WantHeader) Handle(ctx context.Context, req *transport.Request, resw transport.ResponseWriter, handler transport.UnaryHandler) error { 80 actualValue, ok := req.Headers.Get(h.Key) 81 require.True(h.GetTestingTB(), ok, "header %q was not set on the request", h.Key) 82 require.Equal(h.GetTestingTB(), actualValue, h.Value, "headers did not match for %q", h.Key) 83 return handler.Handle(ctx, req, resw) 84 }