go.uber.org/yarpc@v1.72.1/yarpctest/context.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 yarpctest 22 23 import ( 24 "context" 25 26 "go.uber.org/yarpc/api/transport" 27 "go.uber.org/yarpc/internal/inboundcall" 28 ) 29 30 // Call specifies metadata for ContextWithCall. 31 type Call struct { 32 Caller string 33 Service string 34 Transport string 35 Procedure string 36 Encoding transport.Encoding 37 Headers map[string]string 38 ShardKey string 39 RoutingKey string 40 RoutingDelegate string 41 CallerProcedure string 42 43 // If set, this map will be filled with response headers written to 44 // yarpc.Call. 45 ResponseHeaders map[string]string 46 } 47 48 // ContextWithCall builds a Context which will yield the provided request 49 // metadata when used with yarpc.CallFromContext. 50 // 51 // ctx := yarpctest.ContextWithCall(ctx, &Call{..}) 52 // handler.GetValue(ctx, &Request{...}) 53 // 54 func ContextWithCall(ctx context.Context, call *Call) context.Context { 55 if call == nil { 56 return ctx // no-op 57 } 58 59 return inboundcall.WithMetadata(ctx, callMetadata{call}) 60 } 61 62 type callMetadata struct{ c *Call } 63 64 func (c callMetadata) WriteResponseHeader(k string, v string) error { 65 if c.c.ResponseHeaders != nil { 66 c.c.ResponseHeaders[k] = v 67 } 68 return nil 69 } 70 71 func (c callMetadata) Caller() string { return c.c.Caller } 72 func (c callMetadata) Service() string { return c.c.Service } 73 func (c callMetadata) Transport() string { return c.c.Transport } 74 func (c callMetadata) Procedure() string { return c.c.Procedure } 75 func (c callMetadata) Encoding() transport.Encoding { return c.c.Encoding } 76 func (c callMetadata) CallerProcedure() string { return c.c.CallerProcedure } 77 78 func (c callMetadata) Headers() transport.Headers { 79 return transport.HeadersFromMap(c.c.Headers) 80 } 81 82 func (c callMetadata) ShardKey() string { return c.c.ShardKey } 83 func (c callMetadata) RoutingKey() string { return c.c.RoutingKey } 84 func (c callMetadata) RoutingDelegate() string { return c.c.RoutingDelegate }