-
Notifications
You must be signed in to change notification settings - Fork 839
/
request_matcher.go
176 lines (154 loc) · 4.91 KB
/
request_matcher.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// +build go1.13
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package testframework
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"github.com/dnaeon/go-vcr/cassette"
)
type RequestMatcher struct {
context TestContext
IgnoredHeaders map[string]*string
bodyMatcher StringMatcher
urlMatcher StringMatcher
methodMatcher StringMatcher
}
type StringMatcher func(reqVal string, recVal string) bool
type matcherWrapper func(matcher StringMatcher, testContext TestContext) bool
var ignoredHeaders = map[string]*string{
"Date": nil,
"X-Ms-Date": nil,
"x-ms-date": nil,
"x-ms-client-request-id": nil,
"User-Agent": nil,
"Request-Id": nil,
"traceparent": nil,
"Authorization": nil,
}
const (
recordingHeaderMissing = "Test recording headers do not match. Header '%s' is present in request but not in recording."
requestHeaderMissing = "Test recording headers do not match. Header '%s' is present in recording but not in request."
headerValuesMismatch = "Test recording header '%s' does not match. request: %s, recording: %s"
methodMismatch = "Test recording methods do not match. request: %s, recording: %s"
urlMismatch = "Test recording URLs do not match. request: %s, recording: %s"
bodiesMismatch = "Test recording bodies do not match.\nrequest: %s\nrecording: %s"
)
func DefaultMatcher(testContext TestContext) *RequestMatcher {
// The default sanitizer sanitizes the Authorization header
matcher := &RequestMatcher{
context: testContext,
IgnoredHeaders: ignoredHeaders,
}
matcher.SetBodyMatcher(func(req string, rec string) bool {
return DefaultStringMatcher(req, rec)
})
matcher.SetURLMatcher(func(req string, rec string) bool {
return DefaultStringMatcher(req, rec)
})
matcher.SetMethodMatcher(func(req string, rec string) bool {
return DefaultStringMatcher(req, rec)
})
return matcher
}
func (m *RequestMatcher) SetBodyMatcher(matcher StringMatcher) {
m.bodyMatcher = func(reqVal string, recVal string) bool {
isMatch := matcher(reqVal, recVal)
if !isMatch {
m.context.Log(fmt.Sprintf(bodiesMismatch, recVal, recVal))
}
return isMatch
}
}
func (m *RequestMatcher) SetURLMatcher(matcher StringMatcher) {
m.urlMatcher = func(reqVal string, recVal string) bool {
isMatch := matcher(reqVal, recVal)
if !isMatch {
m.context.Log(fmt.Sprintf(urlMismatch, recVal, recVal))
}
return isMatch
}
}
func (m *RequestMatcher) SetMethodMatcher(matcher StringMatcher) {
m.methodMatcher = func(reqVal string, recVal string) bool {
isMatch := matcher(reqVal, recVal)
if !isMatch {
m.context.Log(fmt.Sprintf(methodMismatch, recVal, recVal))
}
return isMatch
}
}
func DefaultStringMatcher(s1 string, s2 string) bool {
return s1 == s2
}
func getBody(r *http.Request) string {
body := bytes.Buffer{}
if r.Body != nil {
_, err := body.ReadFrom(r.Body)
if err != nil {
return "could not parse body: " + err.Error()
}
r.Body = ioutil.NopCloser(&body)
}
return body.String()
}
func getUrl(r *http.Request) string {
return r.URL.String()
}
func getMethod(r *http.Request) string {
return r.Method
}
func (m *RequestMatcher) compareBodies(r *http.Request, recordedBody string) bool {
body := getBody(r)
return m.bodyMatcher(body, recordedBody)
}
func (m *RequestMatcher) compareURLs(r *http.Request, recordedUrl string) bool {
url := getUrl(r)
return m.urlMatcher(url, recordedUrl)
}
func (m *RequestMatcher) compareMethods(r *http.Request, recordedMethod string) bool {
method := getMethod(r)
return m.methodMatcher(method, recordedMethod)
}
func (m *RequestMatcher) compareHeaders(r *http.Request, i cassette.Request) bool {
unVisitedCassetteKeys := make(map[string]*string, len(i.Headers))
// clone the cassette keys to track which we have seen
for k := range i.Headers {
if _, ignore := ignoredHeaders[k]; ignore {
// don't copy ignored headers
continue
}
unVisitedCassetteKeys[k] = nil
}
//iterate through all the request headers to compare them to cassette headers
for key, requestHeader := range r.Header {
if _, ignore := ignoredHeaders[key]; ignore {
// this is an ignorable header
continue
}
delete(unVisitedCassetteKeys, key)
if recordedHeader, foundMatch := i.Headers[key]; foundMatch {
headersMatch := reflect.DeepEqual(requestHeader, recordedHeader)
if !headersMatch {
// headers don't match
m.context.Log(fmt.Sprintf(headerValuesMismatch, key, requestHeader, recordedHeader))
return false
}
} else {
// header not found
m.context.Log(fmt.Sprintf(recordingHeaderMissing, key))
return false
}
}
if len(unVisitedCassetteKeys) > 0 {
// headers exist in the recording that do not exist in the request
for headerName := range unVisitedCassetteKeys {
m.context.Log(fmt.Sprintf(requestHeaderMissing, headerName))
}
return false
}
return true
}