/
scenario.go
578 lines (496 loc) · 13.6 KB
/
scenario.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
package httpreplay
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"sync"
yaml "gopkg.in/yaml.v2"
)
// Scenario format versions
const (
scenarioFormatV1 = 1
)
var (
// ErrInteractionNotFound indicates that a requested
// interaction was not found in the scenario file
ErrInteractionNotFound = errors.New("Requested interaction not found")
)
// Request represents a client request as recorded in the
// scenario file
type Request struct {
// Body of request
Body string `yaml:"body"`
// BodyParsed is parsed from body json
BodyParsed interface{} `yaml:"bodyParsed"`
// Form values
Form url.Values `yaml:"form"`
// Request headers
Headers http.Header `yaml:"headers"`
// Request URL
URL string `yaml:"url"`
// Request method
Method string `yaml:"method"`
}
// Response represents a server response as recorded in the
// scenario file
type Response struct {
// Body of responsen
Body string `yaml:"body"`
// BodyParsed is parsed from body json
BodyParsed interface{} `yaml:"bodyParsed"`
// Response headers
Headers http.Header `yaml:"headers"`
// Response status message
Status string `yaml:"status"`
// Response status code
Code int `yaml:"code"`
// Response duration (something like "100ms" or "10s")
Duration string `yaml:"duration"`
}
// Interaction type contains a pair of request/response for a
// single HTTP interaction between a client and a server
type Interaction struct {
Index int `yaml:"index"`
Uses int `yaml:"uses"`
Request `yaml:"request"`
Response `yaml:"response"`
}
// Matcher function returns true when the actual request matches
// a single HTTP interaction's request according to the function's
// own criteria.
type Matcher func(int, *Request, *Request) bool
func matcher(n int, r *Request, i *Request) bool {
rUrl := stripQuery(r.URL)
iUrl := stripQuery(i.URL)
if r.Method != i.Method {
return false
}
if rUrl != iUrl {
return false
}
return true
}
func stripQuery(url string) string {
l := strings.Split(url, "?")
return l[0]
}
type Interactions []Interaction
// Scenario type
type Scenario struct {
// Name of the scenario
Name string `yaml:"-"`
// File name of the scenario as written on disk
File string `yaml:"-"`
// Scenario format version
Version int `yaml:"version"`
// Mutex to lock accessing Interactions. omitempty is set
// to prevent the mutex appearing in the recorded YAML.
Mu sync.RWMutex `yaml:"mu,omitempty"`
// Interactions between client and server
Interactions `yaml:"interactions"`
sortedInteractions Interactions `yaml:"-"`
// Matches actual request with interaction requests.
Matcher `yaml:"-"`
}
// Implementations of sort.Interface to give us different orderings.
type byUsage Interactions
func (a byUsage) Len() int {
return len(a)
}
func (a byUsage) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a byUsage) Less(i, j int) bool {
if a[i].Uses != a[j].Uses {
return a[i].Uses < a[j].Uses
}
return a[i].Index < a[j].Index
}
type byIndex Interactions
func (a byIndex) Len() int {
return len(a)
}
func (a byIndex) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a byIndex) Less(i, j int) bool {
return a[i].Index < a[j].Index
}
// NewScenario creates a new empty Scenario
func NewScenario(name string) *Scenario {
s := &Scenario{
Name: name,
File: fmt.Sprintf("%s.yaml", name),
Version: scenarioFormatV1,
Interactions: make(Interactions, 0),
sortedInteractions: make(Interactions, 0),
}
return s
}
// Load reads a scenario file from disk
func Load(name string) (*Scenario, error) {
s := NewScenario(name)
fileName := "record/" + s.File
data, err := ioutil.ReadFile(fileName)
if err != nil {
debugLogf(err.Error())
return nil, err
}
err = yaml.Unmarshal(data, &s)
for index := range s.Interactions {
s.Interactions[index].Index = index
}
s.sortedInteractions = make(Interactions, len(s.Interactions))
copy(s.sortedInteractions, s.Interactions)
return s, err
}
var calls = 0
func transformer(req *Request, i Interaction, res *Response) {
if req.BodyParsed != nil {
updateFieldMap(req, &i)
}
for oldVal, changedVal := range fields {
if len(req.URL) >= len(changedVal) && strings.Contains(req.URL, oldVal) {
req.URL = strings.Replace(req.URL, oldVal, changedVal, -1)
}
}
if res.BodyParsed != nil && len(fields) > 0 {
updateResFromFieldMap(res)
}
saveOrLog(req, fmt.Sprintf("/tmp/%d-request.yaml", calls))
saveOrLog(i, fmt.Sprintf("/tmp/%d-interaction.yaml", calls))
saveOrLog(res, fmt.Sprintf("/tmp/%d-response.yaml", calls))
saveOrLog(fields, fmt.Sprintf("/tmp/%d-fields-map.yaml", calls))
calls++
}
// AddInteraction appends a new interaction to the scenario
func (s *Scenario) AddInteraction(i *Interaction) {
s.Mu.Lock()
defer s.Mu.Unlock()
i.Index = len(s.Interactions)
s.Interactions = append(s.Interactions, *i)
s.sortedInteractions = append(s.sortedInteractions, *i)
}
func (s *Scenario) GetInteractionWithFullPath(r Request) (*Interaction, error) {
newRequest, err := ConverRequestWithFullPath(r)
if err != nil {
return nil, err
}
return s.GetInteraction(newRequest)
}
// GetInteraction retrieves a recorded request/response interaction
func (s *Scenario) GetInteraction(r Request) (*Interaction, error) {
s.Mu.Lock()
defer s.Mu.Unlock()
sort.Stable(byUsage(s.sortedInteractions))
if r.Body != "" {
return s.GetInteractionWithBody(r)
}
if strings.Contains(r.URL, "?") {
return s.GetInteractionWithQueryString(r)
}
for _, i := range s.sortedInteractions {
if s.Matcher(i.Index, &r, &i.Request) {
s.updateUsageCount(i.Index)
return &i, nil
}
}
return nil, ErrInteractionNotFound
}
// Get match Interaction by compare the query string in request
func (s *Scenario) GetInteractionWithQueryString(r Request) (*Interaction, error) {
var interactionList []*Interaction
for index := range s.sortedInteractions {
if s.Matcher(index, &r, &s.Interactions[index].Request) {
interactionList = append(interactionList, &s.Interactions[index])
}
}
result, err := s.GetInteractionWithQueryStringFromList(r, interactionList)
if result != nil {
s.updateUsageCount(result.Index)
}
return result, err
}
func (s *Scenario) GetInteractionWithQueryStringFromList(r Request, list []*Interaction) (*Interaction, error) {
if len(list) < 1 {
return nil, ErrInteractionNotFound
}
if len(list) == 1 {
return list[0], nil
}
// Return a map[string]string[] of query string
getQueryMap := func(URL string) (url.Values, error) {
requestURL, err := url.Parse(URL)
if err != nil {
return nil, err
}
return url.ParseQuery(requestURL.RawQuery)
}
// Compare 2 query string and return the credit as each match
getCreditCompareQueryString := func(m1, m2 url.Values) int {
var credit int
for m1Key, m1List := range m1 {
if m2List, ok := m2[m1Key]; ok {
if len(m1List) < 1 || len(m2List) < 1 || len(m1List) != len(m2List) {
continue
}
for i := range m1List {
m1SortedList := strings.Split(m1List[i], ",")
m2SortedList := strings.Split(m2List[i], ",")
sort.Strings(m1SortedList)
sort.Strings(m2SortedList)
if strings.EqualFold(strings.Join(m1SortedList, "_"), strings.Join(m2SortedList, "_")) {
credit++
}
}
}
}
return credit
}
var maxCredit int
var iMax *Interaction
rQuery, err := getQueryMap(r.URL)
if err != nil {
return nil, err
}
for _, i := range list {
credit := 1 - i.Uses
iQuery, err := getQueryMap(i.URL)
if err != nil {
return nil, err
}
credit += getCreditCompareQueryString(rQuery, iQuery)
if credit > maxCredit {
maxCredit = credit
iMax = i
}
}
return iMax, nil
}
func (s *Scenario) updateUsageCount(n int) {
s.Interactions[n].Uses++
copy(s.sortedInteractions, s.Interactions)
sort.Stable(byUsage(s.sortedInteractions))
}
// Get Interaction with body by compare the body of request and Interaction
func (s *Scenario) GetInteractionWithBody(r Request) (*Interaction, error) {
var interactionList []*Interaction
for index := range s.sortedInteractions {
if s.Matcher(index, &r, &s.Interactions[index].Request) {
interactionList = append(interactionList, &s.Interactions[index])
}
}
result, err := s.GetInteractionWithBodyFromList(r, interactionList)
if result != nil {
s.updateUsageCount(result.Index)
}
return result, err
}
func (s *Scenario) GetInteractionWithBodyFromList(r Request, list []*Interaction) (*Interaction, error) {
if len(list) < 1 {
return nil, ErrInteractionNotFound
}
if len(list) == 1 {
return list[0], nil
}
if r.BodyParsed == nil {
debugLogf("BodyParsed nil")
var iMax *Interaction
var maxCredit int
for _, i := range list {
credit := 1 - i.Uses
if credit > maxCredit {
maxCredit = credit
iMax = i
}
}
return iMax, nil
}
rBody := r.BodyParsed.(jsonObj)
var maxCredit int
var iMax *Interaction
var credit int
matchObj := func(iBody jsonObj) {
for key, rUnk := range rBody {
if rStringVal, ok := rUnk.(string); ok {
if iUnk, ok := iBody[key]; ok {
if iStringVal, ok := iUnk.(string); ok {
if rStringVal == iStringVal {
credit++
}
}
}
}
}
}
debugLogf("In GetInteractionWithBodyFromList with %v items...", len(list))
for _, i := range list {
credit = 1 - i.Uses
if nil == i.Request.BodyParsed {
i.Request.BodyParsed, _ = unmarshal([]byte(i.Request.Body))
}
if iBody, ok := i.Request.BodyParsed.(jsonObj); ok {
matchObj(iBody)
} else {
if aBody, ok := i.Request.BodyParsed.(jsonArr); ok {
for _, i := range aBody {
matchObj(i)
}
}
}
debugLogf("\t...Interaction %v has match %v", i.Index, credit)
if credit > maxCredit {
maxCredit = credit
iMax = i
}
}
debugLogf("\t-> Returning match with number %v", iMax.Index)
return iMax, nil
}
// Reset returns us to the beginning of the scenario
func (s *Scenario) Reset() {
for index := range s.Interactions {
s.Interactions[index].Uses = 0
s.sortedInteractions[index].Uses = 0
}
sort.Stable(byIndex(s.Interactions))
sort.Stable(byIndex(s.sortedInteractions))
}
// Save writes the scenario data on disk for future re-use
func (s *Scenario) Save() error {
s.Mu.RLock()
defer s.Mu.RUnlock()
fileName := "record/" + s.File
// Create directory for scenario if missing
scenarioDir := filepath.Dir(fileName)
if _, err := os.Stat(scenarioDir); os.IsNotExist(err) {
if err = os.MkdirAll(scenarioDir, 0755); err != nil {
return err
}
}
// Marshal to YAML and save interactions
s.Reset()
data, err := yaml.Marshal(s)
if err != nil {
return err
}
f, err := os.Create(fileName)
if err != nil {
return err
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
log.Fatal(err)
}
}(f)
// Honor the YAML structure specification
// http://www.yaml.org/spec/1.2/spec.html#id2760395
_, err = f.Write([]byte("---\n"))
if err != nil {
return err
}
_, err = f.Write(data)
if err != nil {
return err
}
_, err = f.Seek(0, 0)
if err != nil {
return err
}
//Use to upload the record to cloud
//err = UploadObject(s.Name, f)
//if err != nil {
// return err
//}
return nil
}
var fields = make(map[string]string)
func ConverRequestWithFullPath(r Request) (Request, error) {
for key, value := range fields {
if strings.Contains(r.URL, value) {
r.URL = strings.Replace(r.URL, value, key, -1)
return r, nil
}
}
return r, ErrInteractionNotFound
}
func updateFieldMap(req *Request, i *Interaction) {
if body, ok := req.BodyParsed.(jsonObj); ok {
if iBody, ok := i.Request.BodyParsed.(jsonObj); ok {
updateInternalFieldMap(iBody, body)
}
}
}
func updateInternalFieldMap(oldValue, newValue interface{}) {
if stringOldValue, ok := oldValue.(string); ok {
stringNewValue, _ := newValue.(string)
if strings.EqualFold(stringOldValue, stringNewValue) == false {
fields[stringOldValue] = stringNewValue
}
} else if mapOldValue, ok := oldValue.(jsonObj); ok {
mapNewValue, _ := newValue.(jsonObj)
for k, v := range mapOldValue {
updateInternalFieldMap(v, mapNewValue[k])
}
} else if mapOldValue, ok := oldValue.(map[string]interface{}); ok {
mapNewValue, _ := newValue.(map[string]interface{})
for k, v := range mapOldValue {
updateInternalFieldMap(v, mapNewValue[k])
}
} else if arrayOldValue, ok := oldValue.([]interface{}); ok {
arrayNewValue, _ := newValue.([]interface{})
for i := range arrayOldValue {
updateInternalFieldMap(arrayOldValue[i], arrayNewValue[i])
}
} else {
debugLogf("HttpReplay will ignore the type match for type %s", reflect.TypeOf(oldValue))
}
}
func updateBody(body jsonObj) {
for key, unkVal := range body {
if unkVal == nil {
continue
} else if val, ok := unkVal.(string); ok {
bodyValueHandle(body, val, key)
} else if val, ok := unkVal.(map[string]interface{}); ok {
updateBody(val)
} else if val, ok := unkVal.([]interface{}); ok {
for _, item := range val {
if jsonItem, ok := item.(map[string]interface{}); ok {
updateBody(jsonItem)
} else if strItem, ok := item.(string); ok {
bodyValueHandle(body, strItem, key)
}
}
} else {
debugLogf("HttpReplay will ignore the type match for type %s, %v", reflect.TypeOf(unkVal), unkVal)
}
}
}
func bodyValueHandle(body jsonObj, val string, key string) {
for oldVal, changedVal := range fields {
if len(val) >= len(changedVal) && strings.Contains(val, oldVal) {
body[key] = strings.Replace(val, oldVal, changedVal, -1)
}
}
}
func updateResFromFieldMap(res *Response) {
if body, ok := res.BodyParsed.(jsonObj); ok {
updateBody(body)
}
if iBodyArr, ok := res.BodyParsed.(jsonArr); ok {
for objIndex := range iBodyArr {
updateBody(iBodyArr[objIndex])
}
}
}