-
Notifications
You must be signed in to change notification settings - Fork 208
/
hoverfly_funcs.go
432 lines (368 loc) · 11.8 KB
/
hoverfly_funcs.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package hoverfly
import (
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"github.com/SpectoLabs/hoverfly/core/errors"
v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/core/matching"
"github.com/SpectoLabs/hoverfly/core/matching/matchers"
"github.com/SpectoLabs/hoverfly/core/models"
"github.com/SpectoLabs/hoverfly/core/modes"
"github.com/SpectoLabs/hoverfly/core/util"
"github.com/aymerick/raymond"
log "github.com/sirupsen/logrus"
)
// DoRequest - performs request and returns response that should be returned to client and error
func (hf *Hoverfly) DoRequest(request *http.Request) (*http.Response, error) {
// We can't have this set. And it only contains "/pkg/net/http/" anyway
request.RequestURI = ""
client, err := GetHttpClient(hf, request.Host)
if err != nil {
return nil, err
}
resp, err := client.Do(request)
if err != nil {
return nil, err
}
resp.Header.Set("Hoverfly", "Was-Here")
if hf.Cfg.Mode == "spy" {
resp.Header.Add("Hoverfly", "Forwarded")
}
return resp, nil
}
// GetResponse returns stored response from cache
func (hf *Hoverfly) GetResponse(requestDetails models.RequestDetails) (*models.ResponseDetails, *errors.HoverflyError) {
var response models.ResponseDetails
var cachedResponse *models.CachedResponse
cachedResponse, cacheErr := hf.CacheMatcher.GetCachedResponse(&requestDetails)
// Get the cached response and return if there is a miss
if cacheErr == nil && cachedResponse.MatchingPair == nil {
return nil, errors.MatchingFailedError(cachedResponse.ClosestMiss)
// If it's cached, use that response
} else if cacheErr == nil {
response = cachedResponse.MatchingPair.Response
//If it's not cached, perform matching to find a hit
} else {
mode := (hf.modeMap[modes.Simulate]).(*modes.SimulateMode)
// Matching
result := matching.Match(mode.MatchingStrategy, requestDetails, hf.Cfg.Webserver, hf.Simulation, hf.state)
// Cache result
if result.Cacheable {
cachedResponse, _ = hf.CacheMatcher.SaveRequestMatcherResponsePair(requestDetails, result.Pair, result.Error)
}
// If we miss, just return
if result.Error != nil {
log.WithFields(log.Fields{
"error": result.Error.Error(),
"query": requestDetails.Query,
"path": requestDetails.Path,
"destination": requestDetails.Destination,
"method": requestDetails.Method,
}).Warn("Failed to find matching request from simulation")
return nil, errors.MatchingFailedError(result.Error.ClosestMiss)
} else {
response = result.Pair.Response
}
}
// Templating applies at the end, once we have loaded a response. Comes BEFORE state transitions,
// as we use the current state in templates
if response.Templated == true {
responseBody, err := hf.applyBodyTemplating(&requestDetails, &response, cachedResponse)
if err == nil {
response.Body = responseBody
} else {
log.Warnf("Failed to applying body templating: %s", err.Error())
}
responseHeaders, err := hf.applyHeadersTemplating(&requestDetails, &response, cachedResponse)
if err == nil {
response.Headers = responseHeaders
} else {
log.Warnf("Failed to applying headers templating: %s", err.Error())
}
responseTransitionsState, err := hf.applyTransitionsStateTemplating(&requestDetails, &response, cachedResponse)
if err == nil {
response.TransitionsState = responseTransitionsState
} else {
log.Warnf("Failed to applying transitions state templating: %s", err.Error())
}
}
// State transitions after we have the response
if response.TransitionsState != nil {
hf.state.PatchState(response.TransitionsState)
}
if response.RemovesState != nil {
hf.state.RemoveState(response.RemovesState)
}
return &response, nil
}
func (hf *Hoverfly) readResponseBodyFiles(pairs []v2.RequestMatcherResponsePairViewV5) v2.SimulationImportResult {
result := v2.SimulationImportResult{}
for i, pair := range pairs {
if len(pair.Response.GetBody()) > 0 && len(pair.Response.GetBodyFile()) > 0 {
result.AddBodyAndBodyFileWarning(i)
continue
}
if len(pair.Response.GetBody()) == 0 && len(pair.Response.GetBodyFile()) > 0 {
var content string
var err error
bodyFile := pair.Response.GetBodyFile()
if util.IsURL(bodyFile) {
content, err = hf.readResponseBodyURL(bodyFile)
} else {
content, err = hf.readResponseBodyFile(bodyFile)
}
if err != nil {
result.SetError(fmt.Errorf("data.pairs[%d].response %s", i, err.Error()))
return result
}
pairs[i].Response.Body = content
}
}
return result
}
func (hf *Hoverfly) readResponseBodyURL(fileURL string) (string, error) {
isAllowed := false
for _, allowedOrigin := range hf.Cfg.ResponsesBodyFilesAllowedOrigins {
if strings.HasPrefix(fileURL, allowedOrigin) {
isAllowed = true
break
}
}
if !isAllowed {
return "", fmt.Errorf("bodyFile %s is not allowed. To allow this origin run hoverfly with -response-body-files-allow-origin", fileURL)
}
resp, err := http.DefaultClient.Get(fileURL)
if err != nil {
err := fmt.Errorf("bodyFile %s cannot be downloaded: %s", fileURL, err.Error())
return "", err
}
content, err := util.GetResponseBody(resp)
if err != nil {
err := fmt.Errorf("response from bodyFile %s cannot be read: %s", fileURL, err.Error())
return "", err
}
return content, nil
}
func (hf *Hoverfly) readResponseBodyFile(filePath string) (string, error) {
if filepath.IsAbs(filePath) {
return "", fmt.Errorf("bodyFile contains absolute path (%s). only relative is supported", filePath)
}
fileContents, err := ioutil.ReadFile(filepath.Join(hf.Cfg.ResponsesBodyFilesPath, filePath))
if err != nil {
return "", err
}
return string(fileContents[:]), nil
}
func (hf *Hoverfly) applyTransitionsStateTemplating(requestDetails *models.RequestDetails, response *models.ResponseDetails, cachedResponse *models.CachedResponse) (map[string]string, error) {
if response.TransitionsState == nil {
return nil, nil
}
var stateTemplates map[string]*raymond.Template
if cachedResponse != nil && cachedResponse.ResponseStateTemplates != nil {
stateTemplates = cachedResponse.ResponseStateTemplates
} else {
stateTemplates = map[string]*raymond.Template{}
for k, v := range response.TransitionsState {
stateTemplates[k], _ = hf.templator.ParseTemplate(v)
}
if cachedResponse != nil {
cachedResponse.ResponseStateTemplates = stateTemplates
}
}
var err error
state := make(map[string]string)
for k, v := range stateTemplates {
state[k], err = hf.templator.RenderTemplate(v, requestDetails, hf.Simulation.Literals, hf.Simulation.Vars, hf.state.State)
if err != nil {
return nil, err
}
}
return state, nil
}
func (hf *Hoverfly) applyBodyTemplating(requestDetails *models.RequestDetails, response *models.ResponseDetails, cachedResponse *models.CachedResponse) (string, error) {
var template *raymond.Template
if cachedResponse != nil && cachedResponse.ResponseTemplate != nil {
template = cachedResponse.ResponseTemplate
} else {
// Parse and cache the template
template, _ = hf.templator.ParseTemplate(response.Body)
if cachedResponse != nil {
cachedResponse.ResponseTemplate = template
}
}
return hf.templator.RenderTemplate(template, requestDetails, hf.Simulation.Literals, hf.Simulation.Vars, hf.state.State)
}
func (hf *Hoverfly) applyHeadersTemplating(requestDetails *models.RequestDetails, response *models.ResponseDetails, cachedResponse *models.CachedResponse) (map[string][]string, error) {
var headersTemplates map[string][]*raymond.Template
if cachedResponse != nil && cachedResponse.ResponseHeadersTemplates != nil {
headersTemplates = cachedResponse.ResponseHeadersTemplates
} else {
var header []*raymond.Template
headersTemplates = map[string][]*raymond.Template{}
// Parse and cache headers templates
for k, v := range response.Headers {
header = make([]*raymond.Template, len(v))
for i, h := range v {
header[i], _ = hf.templator.ParseTemplate(h)
}
headersTemplates[k] = header
}
if cachedResponse != nil {
cachedResponse.ResponseHeadersTemplates = headersTemplates
}
}
var (
header []string
err error
)
headers := map[string][]string{}
// Render headers templates
for k, v := range headersTemplates {
header = make([]string, len(v))
for i, h := range v {
header[i], err = hf.templator.RenderTemplate(h, requestDetails, hf.Simulation.Literals, hf.Simulation.Vars, hf.state.State)
if err != nil {
return nil, err
}
}
headers[k] = header
}
return headers, nil
}
// save gets request fingerprint, extracts request body, status code and headers, then saves it to cache
func (hf *Hoverfly) Save(request *models.RequestDetails, response *models.ResponseDetails, modeArgs *modes.ModeArguments) error {
body := []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: request.Body,
},
}
contentType := util.GetContentTypeFromHeaders(request.Headers)
if contentType == "json" {
body = []models.RequestFieldMatchers{
{
Matcher: matchers.Json,
Value: request.Body,
},
}
} else if contentType == "xml" {
body = []models.RequestFieldMatchers{
{
Matcher: matchers.Xml,
Value: request.Body,
},
}
} else if contentType == "form" {
if len(request.FormData) > 0 {
form := make(map[string][]models.RequestFieldMatchers)
for formKey, formValue := range request.FormData {
form[formKey] = []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: formValue[0],
},
}
}
body = []models.RequestFieldMatchers{
{
Matcher: "form",
Value: form,
},
}
}
}
var headers map[string][]string
if len(modeArgs.Headers) >= 1 {
if modeArgs.Headers[0] == "*" {
headers = request.Headers
} else {
headers = map[string][]string{}
for _, header := range modeArgs.Headers {
headerValues := request.Headers[header]
if len(headerValues) > 0 {
headers[header] = headerValues
}
}
}
}
var requestHeaders map[string][]models.RequestFieldMatchers
if len(headers) > 0 {
requestHeaders = map[string][]models.RequestFieldMatchers{}
for key, values := range headers {
requestHeaders[key] = getRequestMatcherForMultipleValues(values)
}
}
var queries *models.QueryRequestFieldMatchers
if len(request.Query) > 0 {
queries = &models.QueryRequestFieldMatchers{}
for key, values := range request.Query {
queries.Add(key, getRequestMatcherForMultipleValues(values))
}
}
pair := models.RequestMatcherResponsePair{
RequestMatcher: models.RequestMatcher{
Path: []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: request.Path,
},
},
Method: []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: request.Method,
},
},
Destination: []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: request.Destination,
},
},
Scheme: []models.RequestFieldMatchers{
{
Matcher: matchers.Exact,
Value: request.Scheme,
},
},
Query: queries,
Body: body,
Headers: requestHeaders,
},
Response: *response,
}
if modeArgs.Stateful {
hf.Simulation.AddPairInSequence(&pair, hf.state)
} else if modeArgs.OverwriteDuplicate {
hf.Simulation.AddPairWithOverwritingDuplicate(&pair)
} else {
hf.Simulation.AddPair(&pair)
}
return nil
}
func (hf *Hoverfly) ApplyMiddleware(pair models.RequestResponsePair) (models.RequestResponsePair, error) {
if hf.Cfg.Middleware.IsSet() {
return hf.Cfg.Middleware.Execute(pair)
}
return pair, nil
}
func getRequestMatcherForMultipleValues(values []string) []models.RequestFieldMatchers {
var matcher string
var value interface{}
if len(values) > 1 {
matcher = matchers.Array
value = values
} else {
matcher = matchers.Exact
value = strings.Join(values, ";")
}
return []models.RequestFieldMatchers{
{
Matcher: matcher,
Value: value,
},
}
}