This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
gock_matchers.go
108 lines (95 loc) · 2.95 KB
/
gock_matchers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package test
import (
"github.com/dgrijalva/jwt-go"
jwtrequest "github.com/dgrijalva/jwt-go/request"
"github.com/fabric8-services/fabric8-common/log"
"gopkg.in/h2non/gock.v1"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"strings"
)
const ClusterURL = "http://api.cluster1"
func Normalize(url string) string {
if !strings.HasSuffix(url, "/") {
return url + "/"
}
return url
}
func ExpectRequest(matchers ...gock.MatchFunc) gock.Matcher {
return createReqMatcher(matchers)
}
type RequestMatcher func(req *http.Request) bool
func createReqMatcher(matchers []gock.MatchFunc) gock.Matcher {
matcher := gock.NewBasicMatcher()
matcher.Add(func(req *http.Request, gockReq *gock.Request) (bool, error) {
for _, match := range matchers {
ok, err := match(req, gockReq)
if err != nil {
return ok, err
}
if !ok {
return false, nil
}
}
return true, nil
})
return matcher
}
func HasBearerWithSub(sub string) gock.MatchFunc {
return func(req *http.Request, gockReq *gock.Request) (bool, error) {
authHeader, ok := req.Header["Authorization"]
if ok && len(authHeader) == 1 && strings.HasPrefix(authHeader[0], "Bearer ") {
return authHeader[0][7:] == sub, nil
}
return false, nil
}
}
func HasJWTWithSub(sub string) gock.MatchFunc {
return func(req *http.Request, gockReq *gock.Request) (bool, error) {
// look-up the JWT's "sub" claim and compare with the request
token, err := jwtrequest.ParseFromRequest(req, jwtrequest.AuthorizationHeaderExtractor, func(*jwt.Token) (interface{}, error) {
return PublicKey("../test/public_key.pem")
})
if err != nil {
log.Error(nil, map[string]interface{}{"error": err.Error(), "request_method": req.Method,
"request_url": req.URL, "authorization_header": req.Header["Authorization"]}, "failed to parse token from request")
return false, err
}
claims := token.Claims.(jwt.MapClaims)
log.Debug(nil, map[string]interface{}{
"req_method": req.Method,
"req_url": req.URL,
"req_sub": req.Header.Get("sub"),
"request_token_sub": claims["sub"],
}, "comparing `sub` headers")
return claims["sub"] == sub, nil
}
}
func HasBodyContainingObject(object map[interface{}]interface{}) gock.MatchFunc {
return func(req *http.Request, gockReq *gock.Request) (bool, error) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Error(nil, map[string]interface{}{"body": string(body)}, err.Error())
return false, err
}
expBody, err := yaml.Marshal(object)
if err != nil {
log.Error(nil, map[string]interface{}{"object": object}, err.Error())
return false, err
}
return string(body) == string(expBody), nil
}
}
// SpyOnCalls checks the number of calls
func SpyOnCalls(counter *int) gock.Matcher {
matcher := gock.NewBasicMatcher()
matcher.Add(SpyOnCallsMatchFunc(counter))
return matcher
}
func SpyOnCallsMatchFunc(counter *int) gock.MatchFunc {
return func(req *http.Request, _ *gock.Request) (bool, error) {
*counter++
return true, nil
}
}