-
Notifications
You must be signed in to change notification settings - Fork 67
/
request.go
301 lines (241 loc) · 7.18 KB
/
request.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package mockingjay
import (
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"github.com/moul/http2curl"
"io/ioutil"
"log"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
)
// Request is a simplified version of a http.Request
type Request struct {
URI string `yaml:"uri"`
RegexURI *RegexField `yaml:"regexuri,omitempty" json:"RegexURI,omitempty"`
Method string `yaml:"method"`
Headers map[string]string `yaml:"headers,omitempty"`
Body string `yaml:"body,omitempty"`
Form map[string]string `yaml:"form,omitempty"`
}
var (
errBadRegex = errors.New("A regex defined in the request does not pass against it's defined URI")
errEmptyURI = errors.New("Cannot have an empty URI")
errBadURI = errors.New("URI should start with a slash")
errEmptyMethod = errors.New("Cannot have an empty HTTP method")
errBadHeaders = errors.New("Headers are bad (no spaces please, each key must have a value")
errBodyAndForm = errors.New("Cannot have a request with both a body and a form defined")
)
func (r Request) errors() error {
regexPassed := r.RegexURI == nil || r.RegexURI.MatchString(r.URI)
if !regexPassed {
return errBadRegex
}
if r.URI == "" {
return errEmptyURI
}
if r.URI[0] != '/' {
return errBadURI
}
if r.Method == "" {
return errEmptyMethod
}
if !httpHeadersValid(r.Headers) {
return errBadHeaders
}
if r.Form != nil && len(r.Body) > 0 {
return errBodyAndForm
}
return nil
}
// AsCURL returns a string which is the curl command to match the reqquest
func (r Request) AsCURL(baseURL string) (string, error) {
asHTTPReq, err := r.AsHTTPRequest(baseURL)
if err != nil {
return "", err
}
curl, err := http2curl.GetCurlCommand(asHTTPReq)
if err != nil {
return "", err
}
return curl.String(), err
}
// AsHTTPRequest tries to create a http.Request from a given baseURL
func (r Request) AsHTTPRequest(baseURL string) (req *http.Request, err error) {
body := r.Body
if r.Form != nil {
form := url.Values{}
for formKey, formValue := range r.Form {
form.Add(formKey, formValue)
}
body = form.Encode()
}
req, err = http.NewRequest(r.Method, baseURL+r.URI, strings.NewReader(body))
if err != nil {
return
}
for headerName, headerValue := range r.Headers {
req.Header.Add(headerName, headerValue)
}
if r.Form != nil {
req.Header.Add("content-type", "application/x-www-form-urlencoded")
}
return
}
// NewRequest creates a mockingjay request from a http request
func NewRequest(httpRequest *http.Request) (req Request) {
req.URI = httpRequest.URL.String()
req.Method = httpRequest.Method
req.Headers = make(map[string]string)
for header, values := range httpRequest.Header {
req.Headers[strings.ToLower(header)] = strings.Join(values, ",")
}
if httpRequest.Header.Get("content-type") == "application/x-www-form-urlencoded" {
err := httpRequest.ParseForm()
if err == nil {
req.Form = make(map[string]string)
for key, val := range httpRequest.PostForm {
req.Form[key] = val[0] // bit naughty
}
} else {
log.Println("Problem parsing http form", err)
}
} else if httpRequest.Body != nil {
reqBody, err := ioutil.ReadAll(httpRequest.Body)
if err != nil {
log.Println(err)
} else {
req.Body = string(reqBody)
}
}
return
}
func (r Request) String() string {
base := fmt.Sprintf("%s %s", r.Method, r.URI)
if r.Headers != nil {
headersAsString := stringifyMap(r.Headers)
base = fmt.Sprintf("%s Headers: [%s]", base, headersAsString)
}
if r.Form != nil {
headersAsString := stringifyMap(r.Form)
base = fmt.Sprintf("%s Form: [%s]", base, headersAsString)
}
if len(r.Body) > 0 && len(r.Body) > 50 {
base = fmt.Sprintf("%s Body: [%s...]", base, r.Body[:50])
} else if len(r.Body) > 0 {
base = fmt.Sprintf("%s Body: [%s]", base, r.Body)
}
return base
}
func (r Request) Hash() string {
base := fmt.Sprintf("%s %s", r.Method, r.URI)
if r.Headers != nil {
headersAsString := stringifyMap(r.Headers)
base = fmt.Sprintf("%s Headers: [%s]", base, headersAsString)
}
if r.Form != nil {
headersAsString := stringifyMap(r.Form)
base = fmt.Sprintf("%s Form: [%s]", base, headersAsString)
}
if len(r.Body) > 0 {
base = fmt.Sprintf("%s Body: [%s]", base, r.Body)
}
return fmt.Sprintf("%x", sha1.Sum([]byte(base)))
}
func stringifyMap(m map[string]string) string {
var headerStrings []string
for k, v := range m {
headerStrings = append(headerStrings, fmt.Sprintf("%s->%s", k, v))
}
return strings.Join(headerStrings, ", ")
}
func requestMatches(expected, incoming Request, endpointName string, logger mjLogger) bool {
headersOk := matchHeaders(expected.Headers, incoming.Headers)
bodyOk := expected.Body == "*" || expected.Body == incoming.Body || matchJSON(expected.Body, incoming.Body)
urlOk := matchURI(expected.URI, expected.RegexURI, incoming.URI)
methodOk := expected.Method == incoming.Method
formOK := matchForm(expected.Form, incoming.Form)
matches := bodyOk && urlOk && methodOk && headersOk && formOK
if matches {
logger.Println("Matched on", endpointName)
} else {
debugMsg := fmt.Sprintf(
"%s | URL OK? %s | Body OK? %s | Method OK? %s | Form OK? %s | Headers OK? %s",
fixedLengthString(20, endpointName),
fixedLengthString(5, strconv.FormatBool(urlOk)),
fixedLengthString(5, strconv.FormatBool(bodyOk)),
fixedLengthString(5, strconv.FormatBool(methodOk)),
fixedLengthString(5, strconv.FormatBool(formOK)),
fixedLengthString(5, strconv.FormatBool(headersOk)),
)
logger.Println(debugMsg)
}
return matches
}
func matchForm(expected map[string]string, incoming map[string]string) bool {
if expected == nil {
return true
}
for expectedKey, expectedValue := range expected {
if incoming[expectedKey] != expectedValue {
return false
}
}
return true
}
func matchJSON(a string, b string) bool {
var aJSON map[string]interface{}
var bJSON map[string]interface{}
err := json.Unmarshal([]byte(a), &aJSON)
err = json.Unmarshal([]byte(b), &bJSON)
if err != nil {
return false
}
return reflect.DeepEqual(aJSON, bJSON)
}
func matchHeaders(expected, incoming map[string]string) bool {
incominglowercased := lowercaseMapKeys(incoming)
expectedLowercased := lowercaseMapKeys(expected)
for key, expectedValue := range expectedLowercased {
if value, exists := incominglowercased[key]; exists {
if value != expectedValue {
return false
}
} else {
return false
}
}
return true
}
func matchURI(serverURI string, serverRegex *RegexField, incomingURI string) bool {
if serverURI == incomingURI {
return true
} else if serverRegex != nil {
return serverRegex.MatchString(incomingURI)
} else {
inURL, err := url.Parse(incomingURI)
expectedURL, err := url.Parse(serverURI)
if err != nil {
return false
}
if reflect.DeepEqual(inURL.Query(), expectedURL.Query()) {
return inURL.Path == expectedURL.Path
}
}
return false
}
func lowercaseMapKeys(upperCasedMap map[string]string) map[string]string {
lowerCasedMap := make(map[string]string)
for key, value := range upperCasedMap {
lowerCasedMap[strings.ToLower(key)] = value
}
return lowerCasedMap
}
func fixedLengthString(length int, str string) string {
verb := fmt.Sprintf("%%%d.%ds", length, length)
return fmt.Sprintf(verb, str)
}