go.uber.org/yarpc@v1.72.1/x/yarpctest/api/request_unary.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 api 22 23 import ( 24 "bytes" 25 "time" 26 27 "go.uber.org/yarpc/api/middleware" 28 "go.uber.org/yarpc/api/transport" 29 ) 30 31 // RequestOpts are configuration options for a yarpc Request and assertions 32 // to make on the response. 33 type RequestOpts struct { 34 Port uint16 35 UnaryMiddleware []middleware.UnaryOutbound 36 GiveTimeout time.Duration 37 GiveRequest *transport.Request 38 WantResponse *transport.Response 39 WantError error 40 RetryCount int 41 RetryInterval time.Duration 42 } 43 44 // NewRequestOpts initializes a RequestOpts struct. 45 func NewRequestOpts() RequestOpts { 46 return RequestOpts{ 47 GiveTimeout: time.Second * 10, 48 GiveRequest: &transport.Request{ 49 Caller: "unknown", 50 Encoding: transport.Encoding("raw"), 51 Headers: transport.NewHeaders(), 52 Body: bytes.NewBufferString(""), 53 }, 54 WantResponse: &transport.Response{ 55 Headers: transport.NewHeaders(), 56 }, 57 } 58 } 59 60 // RequestOption can be used to configure a request. 61 type RequestOption interface { 62 ApplyRequest(*RequestOpts) 63 } 64 65 // RequestOptionFunc converts a function into a RequestOption. 66 type RequestOptionFunc func(*RequestOpts) 67 68 // ApplyRequest implements RequestOption. 69 func (f RequestOptionFunc) ApplyRequest(opts *RequestOpts) { f(opts) }