go.uber.org/yarpc@v1.72.1/internal/routertest/matcher.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 routertest 22 23 import "go.uber.org/yarpc/api/transport" 24 25 // Criterion is an argument that adds criteria to a transport request and 26 // context matcher. 27 type Criterion func(*Matcher) 28 29 // NewMatcher returns a pair of matchers corresponding to the arguments 30 // of GetHandler(Context, Request), for verifying that GetHandler is called with 31 // parameters that satisfy given constraints. Passing options like 32 // WithService("foo") adds constraints to the matcher. 33 func NewMatcher(criteria ...Criterion) *Matcher { 34 m := Matcher{ 35 constraints: make([]choiceConstraint, 0, 5), 36 } 37 for _, criterion := range criteria { 38 criterion(&m) 39 } 40 return &m 41 } 42 43 type choiceConstraint func(*transport.Request) bool 44 45 // Matcher is a gomock Matcher that validates transport requests with 46 // given criteria. 47 type Matcher struct { 48 constraints []choiceConstraint 49 } 50 51 // WithCaller adds a constraint that the request caller must match. 52 func (m *Matcher) WithCaller(caller string) *Matcher { 53 m.constraints = append(m.constraints, func(r *transport.Request) bool { 54 return caller == r.Caller 55 }) 56 return m 57 } 58 59 // WithService adds a constraint that the request callee must match. 60 func (m *Matcher) WithService(service string) *Matcher { 61 m.constraints = append(m.constraints, func(r *transport.Request) bool { 62 return service == r.Service 63 }) 64 return m 65 } 66 67 // WithProcedure adds a constraint that the request procedure must match. 68 func (m *Matcher) WithProcedure(procedure string) *Matcher { 69 m.constraints = append(m.constraints, func(r *transport.Request) bool { 70 return procedure == r.Procedure 71 }) 72 return m 73 } 74 75 // Matches returns whether a transport request matches the configured criteria 76 // for the matcher. 77 func (m *Matcher) Matches(got interface{}) bool { 78 req := got.(*transport.Request) 79 for _, check := range m.constraints { 80 if !check(req) { 81 return false 82 } 83 } 84 return true 85 } 86 87 func (m *Matcher) String() string { 88 return "choice matcher" 89 }