/
journal_index.go
99 lines (87 loc) · 2.75 KB
/
journal_index.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
package journal
import (
v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/core/models"
"github.com/SpectoLabs/hoverfly/core/util"
"github.com/aymerick/raymond"
"strings"
)
type Index struct {
Name string
template string
Entries map[string]*JournalEntry
}
type Request struct {
QueryParam map[string][]string
Header map[string][]string
Path []string
Scheme string
FormData map[string][]string
Body func(queryType, query string, options *raymond.Options) interface{}
BodyStr string
Method string
}
func (index Index) AddJournalEntry(entry *JournalEntry) {
indexKey := parseIndexKey(index.template, entry.Request)
//it will be same in the condition when raymond(i.e. templating library) unable to parse it
if indexKey == "" || indexKey != index.template {
index.Entries[indexKey] = entry
}
}
func parseIndexKey(source string, details *models.RequestDetails) string {
if tpl, err := raymond.Parse("{{" + source + "}}"); err == nil {
ctx := make(map[string]Request)
ctx["Request"] = getRequest(details)
if parsedValue, execErr := tpl.Exec(ctx); execErr == nil {
return parsedValue
}
}
return source
}
func getRequest(requestDetails *models.RequestDetails) Request {
return Request{
Path: strings.Split(requestDetails.Path, "/")[1:],
QueryParam: requestDetails.Query,
Header: requestDetails.Headers,
Scheme: requestDetails.Scheme,
FormData: requestDetails.FormData,
BodyStr: requestDetails.Body,
Body: requestBody,
Method: requestDetails.Method,
}
}
func (index Index) convertIndex(filteredJournalEntryIds util.HashSet) v2.JournalIndexView {
var journalIndexEntries []v2.JournalIndexEntryView
for key, journalEntry := range index.Entries {
if filteredJournalEntryIds.Contains(journalEntry.Id) {
journalIndexEntry := v2.JournalIndexEntryView{
Key: key,
JournalEntryId: journalEntry.Id,
}
journalIndexEntries = append(journalIndexEntries, journalIndexEntry)
}
}
return v2.JournalIndexView{
Name: index.Name,
Entries: journalIndexEntries,
}
}
func (index Index) getIndexView() v2.JournalIndexView {
var journalIndexEntries []v2.JournalIndexEntryView
for key, journalEntry := range index.Entries {
journalIndexEntry := v2.JournalIndexEntryView{
Key: key,
JournalEntryId: journalEntry.Id,
}
journalIndexEntries = append(journalIndexEntries, journalIndexEntry)
}
return v2.JournalIndexView{
Name: index.Name,
Entries: journalIndexEntries,
}
}
func requestBody(queryType, query string, options *raymond.Options) interface{} {
toMatch := options.Value("Request").(Request).BodyStr
queryType = strings.ToLower(queryType)
return util.FetchFromRequestBody(queryType, query, toMatch)
}