-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathtemplate_helpers.go
More file actions
702 lines (606 loc) · 18.6 KB
/
template_helpers.go
File metadata and controls
702 lines (606 loc) · 18.6 KB
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package templating
import (
"fmt"
"math"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/SpectoLabs/hoverfly/core/journal"
"github.com/SpectoLabs/raymond"
"github.com/pborman/uuid"
"github.com/SpectoLabs/hoverfly/core/util"
"github.com/brianvoe/gofakeit/v6"
"github.com/icrowley/fake"
log "github.com/sirupsen/logrus"
)
const defaultDateTimeFormat = "2006-01-02T15:04:05Z07:00"
type templateHelpers struct {
now func() time.Time
fakerSource *gofakeit.Faker
TemplateDataSource *TemplateDataSource
journal *journal.Journal
}
func (t templateHelpers) nowHelper(offset string, format string) string {
now := t.now()
if offset != "" {
duration, err := ParseDuration(offset)
if err == nil {
now = now.Add(duration)
}
}
var formatted string
if format == "" {
formatted = now.UTC().Format(defaultDateTimeFormat)
} else if format == "unix" {
formatted = strconv.FormatInt(now.Unix(), 10)
} else if format == "epoch" {
formatted = strconv.FormatInt(now.UnixNano()/1000000, 10)
} else {
formatted = now.UTC().Format(format)
}
return formatted
}
func (t templateHelpers) randomString() string {
return util.RandomString()
}
func (t templateHelpers) randomStringLength(length int) string {
return util.RandomStringWithLength(length)
}
func (t templateHelpers) randomBoolean() string {
return strconv.FormatBool(util.RandomBoolean())
}
func (t templateHelpers) randomInteger() string {
return strconv.Itoa(util.RandomInteger())
}
func (t templateHelpers) randomIntegerRange(min, max int) string {
return strconv.Itoa(util.RandomIntegerRange(min, max))
}
func (t templateHelpers) randomFloat() string {
return strconv.FormatFloat(util.RandomFloat(), 'f', 6, 64)
}
func (t templateHelpers) randomFloatRange(min, max float64) string {
return strconv.FormatFloat(util.RandomFloatRange(min, max), 'f', 6, 64)
}
func (t templateHelpers) randomEmail() string {
return fake.EmailAddress()
}
func (t templateHelpers) randomIPv4() string {
return fake.IPv4()
}
func (t templateHelpers) randomIPv6() string {
return fake.IPv6()
}
func (t templateHelpers) randomUuid() string {
return uuid.New()
}
func (t templateHelpers) requestBody(queryType, query string, options *raymond.Options) interface{} {
body := ""
if toMatch, exists := options.Value("request").(Request); exists {
body = toMatch.body
} else {
journalToMatch := options.Value("Request").(journal.Request)
body = journalToMatch.BodyStr
}
queryType = strings.ToLower(queryType)
return util.FetchFromRequestBody(queryType, query, body)
}
func (t templateHelpers) replace(target, oldValue, newValue string) string {
return strings.Replace(target, oldValue, newValue, -1)
}
func (t templateHelpers) split(target, separator string) []string {
return strings.Split(target, separator)
}
// Concatenates any number of arguments together as strings.
func (t templateHelpers) concat(args ...interface{}) string {
var parts []string
for _, arg := range args {
// If arg is a slice, flatten it
if s, ok := arg.([]interface{}); ok {
for _, v := range s {
parts = append(parts, fmt.Sprint(v))
}
} else {
parts = append(parts, fmt.Sprint(arg))
}
}
return strings.Join(parts, "")
}
func (t templateHelpers) isNumeric(stringToCheck string) bool {
_, err := strconv.ParseFloat(stringToCheck, 64)
//return fmt.Sprintf("%t", err == nil)
return err == nil
}
func (t templateHelpers) isAlphanumeric(s string) bool {
regex := regexp.MustCompile("^[a-zA-Z0-9]+$")
return regex.MatchString(s)
}
func (t templateHelpers) isBool(s string) bool {
_, err := strconv.ParseBool(s)
return err == nil
}
// Comparison function type that takes two float64 values and returns a boolean.
type comparator func(float64, float64) bool
// Generic comparison function that parses strings to floats and applies the given comparison function.
func compareValues(value1, value2 string, comp comparator) bool {
num1, err := strconv.ParseFloat(value1, 64)
if err != nil {
return false
}
num2, err := strconv.ParseFloat(value2, 64)
if err != nil {
return false
}
return comp(num1, num2)
}
// Specific comparison functions using the generic compareValues function.
func isGreaterThan(value1, value2 string) bool {
return compareValues(value1, value2, func(a, b float64) bool { return a > b })
}
func isGreaterThanOrEqual(value1, value2 string) bool {
return compareValues(value1, value2, func(a, b float64) bool { return a >= b })
}
func isLessThan(value1, value2 string) bool {
return compareValues(value1, value2, func(a, b float64) bool { return a < b })
}
func isLessThanOrEqual(value1, value2 string) bool {
return compareValues(value1, value2, func(a, b float64) bool { return a <= b })
}
func (t templateHelpers) isGreaterThan(value1, value2 string) bool {
return isGreaterThan(value1, value2)
}
func (t templateHelpers) isGreaterThanOrEqual(value1, value2 string) bool {
return isGreaterThanOrEqual(value1, value2)
}
func (t templateHelpers) isLessThan(value1, value2 string) bool {
return isLessThan(value1, value2)
}
func (t templateHelpers) isLessThanOrEqual(value1, value2 string) bool {
return isLessThanOrEqual(value1, value2)
}
func (t templateHelpers) isBetween(value, min, max string) bool {
return t.isGreaterThan(value, min) && t.isLessThan(value, max)
}
func (t templateHelpers) matchesRegex(valueToCheck, pattern string) bool {
re, err := regexp.Compile(pattern)
if err != nil {
return false
}
return re.MatchString(valueToCheck)
}
func (t templateHelpers) length(stringToCheck string) string {
return strconv.Itoa(len(stringToCheck))
}
func (t templateHelpers) substring(str, startStr, endStr string) string {
start, err := strconv.Atoi(startStr)
if err != nil {
return ""
}
end, err := strconv.Atoi(endStr)
if err != nil {
return ""
}
if start < 0 || end > len(str) || start > end {
return ""
}
return str[start:end]
}
func (t templateHelpers) rightmostCharacters(str, countStr string) string {
count, err := strconv.Atoi(countStr)
if err != nil {
return ""
}
if count < 0 || count > len(str) {
return ""
}
return str[len(str)-count:]
}
func (t templateHelpers) faker(fakerType string) []reflect.Value {
if t.fakerSource == nil {
t.fakerSource = gofakeit.New(0)
}
if reflect.ValueOf(t.fakerSource).MethodByName(fakerType).IsValid() {
return reflect.ValueOf(t.fakerSource).MethodByName(fakerType).Call([]reflect.Value{})
}
return []reflect.Value{}
}
func (t templateHelpers) fetchSingleFieldCsv(dataSourceName, searchFieldName, searchFieldValue, returnFieldName string, options *raymond.Options) string {
source, exists := t.TemplateDataSource.GetDataSource(dataSourceName)
if !exists {
log.Error("could not find datasource " + dataSourceName)
return getEvaluationString("csv", options)
}
source.mu.Lock()
defer source.mu.Unlock()
searchIndex, err := getHeaderIndex(source.Data, searchFieldName)
if err != nil {
log.Error(err)
return getEvaluationString("csv", options)
}
returnIndex, err := getHeaderIndex(source.Data, returnFieldName)
if err != nil {
log.Error(err)
return getEvaluationString("csv", options)
}
searchValue := getSearchFieldValue(options, searchFieldValue)
var fallbackString string
for i := 1; i < len(source.Data); i++ {
record := source.Data[i]
if strings.ToLower(record[searchIndex]) == strings.ToLower(searchValue) {
return record[returnIndex]
} else if record[searchIndex] == "*" {
fallbackString = record[returnIndex]
}
}
if fallbackString != "" {
return fallbackString
}
return getEvaluationString("csv", options)
}
func (t templateHelpers) fetchMatchingRowsCsv(dataSourceName string, searchFieldName string, searchFieldValue string) []RowMap {
source, exists := t.TemplateDataSource.GetDataSource(dataSourceName)
if !exists {
log.Error("could not find datasource " + dataSourceName)
return []RowMap{}
}
if len(source.Data) < 1 {
log.Error("no data available in datasource " + dataSourceName)
return []RowMap{}
}
source.mu.Lock()
defer source.mu.Unlock()
headers := source.Data[0]
fieldIndex := -1
for i, header := range headers {
if header == searchFieldName {
fieldIndex = i
break
}
}
if fieldIndex == -1 {
log.Error("could not find search field name " + searchFieldName)
return []RowMap{}
}
var result []RowMap
for _, row := range source.Data[1:] {
if fieldIndex < len(row) && row[fieldIndex] == searchFieldValue {
rowMap := make(RowMap)
for i, cell := range row {
if i < len(headers) {
rowMap[headers[i]] = cell
}
}
result = append(result, rowMap)
}
}
return result
}
func (t templateHelpers) csvAsArray(dataSourceName string) [][]string {
source, exists := t.TemplateDataSource.GetDataSource(dataSourceName)
if exists {
source.mu.Lock()
defer source.mu.Unlock()
return source.Data
} else {
log.Error("could not find datasource " + dataSourceName)
return [][]string{}
}
}
func (t templateHelpers) csvAsMap(dataSourceName string) []RowMap {
source, exists := t.TemplateDataSource.GetDataSource(dataSourceName)
if !exists {
log.Error("could not find datasource " + dataSourceName)
return []RowMap{}
}
source.mu.Lock()
defer source.mu.Unlock()
if len(source.Data) < 1 {
log.Error("no data available in datasource " + dataSourceName)
return []RowMap{}
}
headers := source.Data[0]
var result []RowMap
for _, row := range source.Data[1:] {
rowMap := make(RowMap)
for i, cell := range row {
if i < len(headers) {
rowMap[headers[i]] = cell
}
}
result = append(result, rowMap)
}
return result
}
func (t templateHelpers) csvAddRow(dataSourceName string, newRow []string) string {
source, exists := t.TemplateDataSource.GetDataSource(dataSourceName)
if exists {
source.mu.Lock()
defer source.mu.Unlock()
source.Data = append(source.Data, newRow)
} else {
log.Error("could not find datasource " + dataSourceName)
}
return ""
}
func (t templateHelpers) csvDeleteRows(dataSourceName, searchFieldName, searchFieldValue string, output bool) string {
source, exists := t.TemplateDataSource.GetDataSource(dataSourceName)
if !exists {
log.Error("could not find datasource " + dataSourceName)
return ""
}
source.mu.Lock()
defer source.mu.Unlock()
if len(source.Data) == 0 {
log.Error("datasource " + dataSourceName + " is empty")
return ""
}
header := source.Data[0]
fieldIndex := -1
for i, fieldName := range header {
if fieldName == searchFieldName {
fieldIndex = i
break
}
}
if fieldIndex == -1 {
log.Error("could not find field name " + searchFieldName + " in datasource " + dataSourceName)
return ""
}
filteredData := [][]string{header}
rowsDeleted := 0
for _, row := range source.Data[1:] {
if row[fieldIndex] != searchFieldValue {
filteredData = append(filteredData, row)
} else {
rowsDeleted++
}
}
source.Data = filteredData
if output {
return fmt.Sprintf("%d", rowsDeleted)
}
return ""
}
func (t templateHelpers) csvCountRows(dataSourceName string) string {
source, exists := t.TemplateDataSource.GetDataSource(dataSourceName)
if !exists {
log.Error("could not find datasource " + dataSourceName)
return ""
}
source.mu.Lock()
defer source.mu.Unlock()
if len(source.Data) == 0 {
return "0"
}
numRows := len(source.Data) - 1 // The number of rows is len(source.Data) - 1 (subtracting 1 for the header row)
return fmt.Sprintf("%d", numRows)
}
func (t templateHelpers) csvSqlCommand(commandString string) []RowMap {
// Parse the command string to get the Sql command
command, err := parseSqlCommand(strings.TrimSpace(commandString), t.TemplateDataSource)
if err != nil {
log.Error("Error parsing sql command:", err)
return []RowMap{}
}
// Find the data source by name
source, exists := t.TemplateDataSource.GetDataSource(command.DataSourceName)
if !exists {
log.Error("Could not find datasource " + command.DataSourceName)
return []RowMap{}
}
source.mu.Lock()
defer source.mu.Unlock()
var results []RowMap
switch command.Type {
case "SELECT":
results = executeSqlSelectQuery(&source.Data, command)
case "UPDATE":
rowsAffected := executeSqlUpdateCommand(&source.Data, command)
log.Debug(strconv.Itoa(rowsAffected) + " rows affected by " + commandString)
return nil
case "DELETE":
rowsAffected := executeSqlDeleteCommand(&source.Data, command)
log.Debug(strconv.Itoa(rowsAffected) + " rows affected by " + commandString)
return nil
default:
log.Error(fmt.Errorf("unsupported query type %s", command.Type))
return nil
}
return results
}
func (t templateHelpers) parseJournalBasedOnIndex(indexName, keyValue, dataSource, queryType, lookupQuery string, options *raymond.Options) interface{} {
if journalEntry, err := getIndexEntry(t.journal, indexName, keyValue); err == nil {
if body := getBodyDataToParse(dataSource, journalEntry); body != "" {
data := util.FetchFromRequestBody(queryType, lookupQuery, body)
if _, ok := data.(error); ok {
// The interface is an error
return getEvaluationString("journal", options)
} else {
return data
}
}
}
return getEvaluationString("journal", options)
}
func (t templateHelpers) hasJournalKey(indexName, keyValue string) bool {
journalEntry, _ := getIndexEntry(t.journal, indexName, keyValue)
return journalEntry != nil
}
func (t templateHelpers) setStatusCode(statusCode string, options *raymond.Options) string {
intStatusCode, err := strconv.Atoi(statusCode)
if err != nil {
log.Error("status code is not a valid integer")
return ""
}
if intStatusCode < 100 || intStatusCode > 599 {
log.Error("status code is not valid")
return ""
}
internalVars := options.ValueFromAllCtx("InternalVars").(map[string]interface{})
internalVars["statusCode"] = intStatusCode
return ""
}
func (t templateHelpers) setHeader(headerName string, headerValue string, options *raymond.Options) string {
if headerName == "" {
log.Error("header name cannot be empty")
return ""
}
internalVars := options.ValueFromAllCtx("InternalVars").(map[string]interface{})
var headers map[string][]string
if h, ok := internalVars["setHeaders"]; ok {
headers = h.(map[string][]string)
} else {
headers = make(map[string][]string)
}
// Replace or add the header
headers[headerName] = []string{headerValue}
internalVars["setHeaders"] = headers
return ""
}
func (t templateHelpers) sum(numbers []string, format string) string {
return sumNumbers(numbers, format)
}
func (t templateHelpers) add(val1 string, val2 string, format string) string {
return sumNumbers([]string{val1, val2}, format)
}
func (t templateHelpers) subtract(val1 string, val2 string, format string) string {
f1, err1 := strconv.ParseFloat(val1, 64)
f2, err2 := strconv.ParseFloat(val2, 64)
if err1 != nil || err2 != nil {
return "NaN"
}
return formatNumber(f1-f2, format)
}
func (t templateHelpers) multiply(val1 string, val2 string, format string) string {
f1, err1 := strconv.ParseFloat(val1, 64)
f2, err2 := strconv.ParseFloat(val2, 64)
if err1 != nil || err2 != nil {
return "NaN"
}
return formatNumber(f1*f2, format)
}
func (t templateHelpers) divide(val1 string, val2 string, format string) string {
f1, err1 := strconv.ParseFloat(val1, 64)
f2, err2 := strconv.ParseFloat(val2, 64)
if err1 != nil || err2 != nil {
return "NaN"
}
return formatNumber(f1/f2, format)
}
func (t templateHelpers) addToArray(key string, value string, output bool, options *raymond.Options) string {
arrayData := options.ValueFromAllCtx("Kvs").(map[string]interface{})
if array, ok := arrayData[key]; ok {
arrayData[key] = append(array.([]string), value)
} else {
arrayData[key] = []string{value}
}
if output {
return value
} else {
return ""
}
}
// Initializes (clears) an array in the template context under the given key.
func (t templateHelpers) initArray(key string, options *raymond.Options) string {
arrayData := options.ValueFromAllCtx("Kvs").(map[string]interface{})
arrayData[key] = []string{}
return ""
}
func (t templateHelpers) getArray(key string, options *raymond.Options) []string {
arrayData := options.ValueFromAllCtx("Kvs").(map[string]interface{})
if array, ok := arrayData[key]; ok {
return array.([]string)
} else {
return []string{}
}
}
func (t templateHelpers) putValue(key string, value string, output bool, options *raymond.Options) string {
kvs := options.ValueFromAllCtx("Kvs").(map[string]interface{})
kvs[key] = value
if output {
return value
} else {
return ""
}
}
func (t templateHelpers) getValue(key string, options *raymond.Options) string {
kvs := options.ValueFromAllCtx("Kvs").(map[string]interface{})
value, exits := kvs[key]
if exits {
return value.(string)
} else {
return ""
}
}
func sumNumbers(numbers []string, format string) string {
var sum float64 = 0
for _, number := range numbers {
value, err := strconv.ParseFloat(number, 64)
if err != nil {
log.Error(err)
return "NaN"
}
sum += value
}
return formatNumber(sum, format)
}
func formatNumber(number float64, format string) string {
if format == "" {
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%f", number), "0"), ".")
}
decimalPlaces := 0
parts := strings.Split(format, ".")
if len(parts) == 2 {
decimalPlaces = len(parts[1])
}
multiplier := math.Pow(10, float64(decimalPlaces))
rounded := math.Round(number*multiplier) / multiplier
return fmt.Sprintf("%."+strconv.Itoa(decimalPlaces)+"f", rounded)
}
func getIndexEntry(journal *journal.Journal, indexName, indexValue string) (*journal.JournalEntry, error) {
for _, index := range journal.Indexes {
if index.Name == indexName {
if journalEntry, exists := index.Entries[indexValue]; exists {
return journalEntry, nil
}
}
}
return nil, fmt.Errorf("no entry found for index %s", indexName)
}
func getBodyDataToParse(source string, journalEntry *journal.JournalEntry) string {
if strings.EqualFold(source, "request") {
return journalEntry.Request.Body
}
if strings.EqualFold(source, "response") {
return journalEntry.Response.Body
}
return ""
}
func getSearchFieldValue(options *raymond.Options, value string) string {
if tpl, err := raymond.Parse("{{ " + value + " }}"); err == nil {
if returnValue, err := tpl.Exec(options.Ctx()); err == nil && returnValue != "" {
return returnValue
}
}
return value
}
func getEvaluationString(helperName string, options *raymond.Options) string {
evaluationString := "{{ " + helperName + " "
for _, params := range options.Params() {
evaluationString = evaluationString + fmt.Sprint(params) + ` `
}
return evaluationString + "}}"
}
func getHeaderIndex(data [][]string, headerName string) (int, error) {
if len(data) == 0 {
return -1, fmt.Errorf("empty file provided")
}
headerRecord := data[0]
for index, fieldName := range headerRecord {
if strings.ToLower(fieldName) == strings.ToLower(headerName) {
return index, nil
}
}
return -1, fmt.Errorf("search field %s does not found", headerName)
}