-
Notifications
You must be signed in to change notification settings - Fork 21
/
template_helper_test.go
196 lines (163 loc) · 5.38 KB
/
template_helper_test.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
package openmock
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestJSONPath(t *testing.T) {
var tmpl string
var ret string
var err error
tmpl = `{"transaction_id": "t1234"}`
ret, err = jsonPath("/transaction_id", tmpl)
assert.NoError(t, err)
assert.Equal(t, "t1234", ret)
tmpl = `{"transaction_id": "t1234"}`
ret, err = jsonPath("/transaction_id/abc", tmpl)
assert.NoError(t, err)
assert.Equal(t, "", ret)
tmpl = `{"user": {"first_name": "John"}}`
ret, err = jsonPath("/user/first_name", tmpl)
assert.NoError(t, err)
assert.Equal(t, "John", ret)
tmpl = `{"user": {"first_name": "John"}}`
ret, err = jsonPath("/*/first_name", tmpl)
assert.NoError(t, err)
assert.Equal(t, "John", ret)
tmpl = `{"user": {"first_name": "John"}}`
ret, err = jsonPath("//first_name", tmpl)
assert.NoError(t, err)
assert.Equal(t, "John", ret)
tmpl = `[{"jsonrpc":"2.0","method":"classify","params":["GUILTY"],"id":112879785776}]`
ret, err = jsonPath("*[1]/method", tmpl)
assert.NoError(t, err)
assert.Equal(t, "classify", ret)
tmpl = `[{"jsonrpc":"2.0","method":"classify","params":["GUILTY"],"id":112879785776}]`
ret, err = jsonPath("*[1]/id", tmpl)
assert.NoError(t, err)
assert.Equal(t, "112879785776", ret)
}
func TestGJsonPath(t *testing.T) {
var tmpl string
var ret string
var err error
tmpl = `{"payload": {"user": {"username": "johnny", "first_name": "John", "valid": true, "id": 7, "profile": null}}}`
ret, err = gJsonPath("payload.user", tmpl)
assert.NoError(t, err)
assert.Equal(t, "{\"username\": \"johnny\", \"first_name\": \"John\", \"valid\": true, \"id\": 7, \"profile\": null}", ret)
ret, err = gJsonPath("payload.user.first_name", tmpl)
assert.NoError(t, err)
assert.Equal(t, "John", ret)
ret, err = gJsonPath("payload.user.valid", tmpl)
assert.NoError(t, err)
assert.Equal(t, "true", ret)
ret, err = gJsonPath("payload.user.id", tmpl)
assert.NoError(t, err)
assert.Equal(t, "7", ret)
ret, err = gJsonPath("payload.user.profile", tmpl)
assert.NoError(t, err)
assert.Equal(t, "null", ret)
tmpl = `{"payload": {"rivers": ["klamath", "merced", "american", "mississippi"]}}`
ret, err = gJsonPath("payload.rivers.#", tmpl)
assert.NoError(t, err)
assert.Equal(t, "4", ret)
ret, err = gJsonPath("payload.rivers.1", tmpl)
assert.NoError(t, err)
assert.Equal(t, "merced", ret)
ret, err = gJsonPath("payload.riv*.2", tmpl)
assert.NoError(t, err)
assert.Equal(t, "american", ret)
ret, err = gJsonPath("payload.r?vers.0", tmpl)
assert.NoError(t, err)
assert.Equal(t, "klamath", ret)
tmpl = `{"payload": {"rivers": [{"name": "klamath", "length": 257}, {"name": "merced", "length": 145}]}}`
ret, err = gJsonPath("payload.rivers.#.length", tmpl)
assert.NoError(t, err)
assert.Equal(t, "[257,145]", ret)
tmpl = `{"payload": {"rivers": [{"name": "klamath", "length": 257}, {"name": "merced", "length": 145}]}}`
ret, err = gJsonPath("payload.rivers.1.name", tmpl)
assert.NoError(t, err)
assert.Equal(t, "merced", ret)
}
func TestHelpers(t *testing.T) {
t.Run("uuid4 helpers", func(t *testing.T) {
raw := `{{ uuidv4 }}`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Contains(t, r, "-4")
})
t.Run("uuid5 helpers", func(t *testing.T) {
raw := `{{ "1234" | uuidv5 }}`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Contains(t, r, "-5")
})
t.Run("isLastIndex helpers", func(t *testing.T) {
raw := `{{ "abc;;def" | splitList ";;" | isLastIndex 1 }}`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Contains(t, r, "true")
})
t.Run("regexFind helpers", func(t *testing.T) {
raw := `{{ regexFind "foo.?" "seafood fool" }}`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Equal(t, "food", r)
})
t.Run("regexFindFirstSubmatch helpers", func(t *testing.T) {
t.Run("normal [a-z]", func(t *testing.T) {
raw := `{{ "peach" | regexFindFirstSubmatch "p([a-z]+)ch" }}`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Equal(t, "ea", r)
})
t.Run("\\w", func(t *testing.T) {
raw := `{{ "peach" | regexFindFirstSubmatch "p(\\w+)ch" }}`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Equal(t, "ea", r)
})
})
t.Run("regexFindAllSubmatch helpers", func(t *testing.T) {
raw := `{{ "peach" | regexFindAllSubmatch "p([a-z]+)ch" }}`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Equal(t, "[peach ea]", r)
})
t.Run("regexFindAllSubmatch helpers with index", func(t *testing.T) {
raw := `h{{index (.HTTPBody | regexFindAllSubmatch "(p)([a-z]+)ch") 2}}l`
c := Context{
HTTPBody: "peach",
}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Equal(t, "heal", r)
})
t.Run("htmlEscapeString helpers", func(t *testing.T) {
raw := `{{ "<note>
<to>Tove</to>
<from>Jani</from>
<heading name=\"heading\">Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>" | htmlEscapeString }}`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.NotContains(t, r, "<")
assert.NotContains(t, r, ">")
assert.NotContains(t, r, "\"")
})
t.Run("hmacSHA256 helpers", func(t *testing.T) {
raw := `{{ "some data to be signed" | hmacSHA256 "secret-key" }}`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Equal(t, "a5e93ff4bc1ef4e57ade71c759617fec0897b7d480add9a13ae37694ce319aed", r)
})
}