github.com/verrazzano/verrazzano@v1.7.1/pkg/test/mockmatchers/matchers.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package mockmatchers 5 6 import ( 7 "fmt" 8 "net/http" 9 10 "github.com/golang/mock/gomock" 11 ) 12 13 // URIMatcher for use with gomock for http requests 14 type URIMatcher struct { 15 path string 16 } 17 18 func MatchesURI(path string) gomock.Matcher { 19 return &URIMatcher{path} 20 } 21 22 func (u *URIMatcher) Matches(x interface{}) bool { 23 if !gomock.AssignableToTypeOf(&http.Request{}).Matches(x) { 24 return false 25 } 26 req := x.(*http.Request) 27 return req.URL.Path == u.path 28 } 29 30 func (u *URIMatcher) String() string { 31 return "is a request whose URI path matches " + u.path 32 } 33 34 // URIMethodMatcher for use with gomock for http requests 35 // matches both the path and the method for and http request 36 type URIMethodMatcher struct { 37 method, path string 38 } 39 40 func MatchesURIMethod(method, path string) gomock.Matcher { 41 return &URIMethodMatcher{method, path} 42 } 43 44 func (u *URIMethodMatcher) Matches(x interface{}) bool { 45 if !gomock.AssignableToTypeOf(&http.Request{}).Matches(x) { 46 return false 47 } 48 req := x.(*http.Request) 49 return req.URL.Path == u.path && req.Method == u.method 50 } 51 52 func (u *URIMethodMatcher) String() string { 53 return fmt.Sprintf("is a request whose method matches %s and URI path matches %s", u.method, u.path) 54 }