-
Notifications
You must be signed in to change notification settings - Fork 21
/
template_test.go
270 lines (244 loc) · 6.34 KB
/
template_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
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
package openmock
import (
"net/http"
"testing"
"github.com/gomodule/redigo/redis"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestContextMerge(t *testing.T) {
t.Run("happy path example", func(t *testing.T) {
orig_context := Context{
HTTPBody: "hello",
HTTPPath: "goodbye",
}
merge_context := Context{
HTTPPath: "au revoir",
KafkaTopic: "this is a topic",
}
expected_result := Context{
HTTPPath: "au revoir",
KafkaTopic: "this is a topic",
HTTPBody: "hello",
}
actual_result := orig_context.Merge(merge_context)
assert.Equal(t, expected_result, actual_result)
})
}
func TestTemplateRender(t *testing.T) {
t.Run("happy code path", func(t *testing.T) {
raw := `{
"transaction_id": "{{.KafkaPayload | jsonPath "transaction_id"}}",
"first_name": "{{.AMQPPayload | xmlPath "user/first_name"}}",
"middle_name": "{{.HTTPBody | jsonPath "user/middle_name"}}",
"user": {{.HTTPBody | gJsonPath "user"}}
}`
c := Context{
HTTPBody: `{"user": {"middle_name": "H"}}`,
KafkaPayload: `{"transaction_id": "t1234"}`,
AMQPPayload: `<user><first_name>John</first_name></user>`,
}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.JSONEq(t, r, `
{
"transaction_id": "t1234",
"first_name": "John",
"middle_name": "H",
"user": {"middle_name": "H"}
}
`)
})
t.Run("nil http body", func(t *testing.T) {
raw := `
{
"transaction_id": "{{.KafkaPayload | jsonPath "transaction_id"}}",
"first_name": "{{.AMQPPayload | xmlPath "user/first_name"}}",
"middle_name": "{{.HTTPBody | jsonPath "user/middle_name"}}"
}
`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.JSONEq(t, r, `
{
"transaction_id": "",
"first_name": "",
"middle_name": ""
}
`)
})
t.Run("no template variables", func(t *testing.T) {
e := echo.New()
raw := `
{
"transaction_id": "t1234",
"name": "abcd"
}
`
c := Context{
HTTPContext: e.NewContext(&http.Request{}, nil),
}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.JSONEq(t, r, `
{
"transaction_id": "t1234",
"name": "abcd"
}
`)
})
t.Run("xml in json", func(t *testing.T) {
raw := `
{
"payload": "
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Record>
<r1>
test
</r1>
</Record>
"
}
`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.JSONEq(t, r, `
{
"payload": " <?xml version=\"1.0\" encoding=\"UTF-8\"?> <Record> <r1> test </r1> </Record> "
}
`)
})
t.Run("temp templates defined in templates", func(t *testing.T) {
raw := `
{{define "T1"}}ONE{{end}}
{{define "T2"}}TWO{{end}}
{{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
{{template "T3"}}
`
c := Context{}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Equal(t, r, ` ONE TWO `)
})
t.Run("predefined templates reused in other templates", func(t *testing.T) {
t1 := `{{define "T1"}}T1{{end}}`
t2 := `{{define "T2"}}T2{{end}}`
t3 := `{{template "T1"}}{{template "T2"}}`
c := Context{}
var err error
_, err = c.Render(t1)
assert.NoError(t, err)
_, err = c.Render(t2)
assert.NoError(t, err)
r, err := c.Render(t3)
assert.NoError(t, err)
assert.Equal(t, r, `T1T2`)
})
t.Run("predefined templates reused in other templates with context", func(t *testing.T) {
t1 := `{{define "T1"}}{{.HTTPPath}}{{end}}`
t2 := `{{define "T2"}} and T2{{end}}`
t3 := `{{template "T1" .}}{{template "T2"}}`
c := Context{
HTTPPath: "/path_to_t1",
}
var err error
_, err = c.Render(t1)
assert.NoError(t, err)
_, err = c.Render(t2)
assert.NoError(t, err)
r, err := c.Render(t3)
assert.NoError(t, err)
assert.Equal(t, r, `/path_to_t1 and T2`)
})
t.Run("templates reused in other templates", func(t *testing.T) {
t1 := `{{define "T1"}}{{.HTTPPath}}{{end}}`
t2 := `{{define "T2"}}{{template "T1" .}}{{end}}` // use T1 in definition of T2
t3 := `{{template "T2" .}}`
c := Context{
HTTPPath: "/path_to_t1",
}
var err error
_, err = c.Render(t1)
assert.NoError(t, err)
_, err = c.Render(t2)
assert.NoError(t, err)
r, err := c.Render(t3)
assert.NoError(t, err)
assert.Equal(t, r, `/path_to_t1`)
})
t.Run("values used in body", func(t *testing.T) {
template := `{{.Values.foo}}`
values := map[string]interface{}{"foo": "bar"}
context := Context{
HTTPPath: "/path_to_t1",
Values: values,
}
rendered, _ := context.Render(template)
assert.Equal(t, rendered, `bar`)
})
t.Run("missing values cause an error", func(t *testing.T) {
template := `{{.Values.banana}}`
values := map[string]interface{}{}
context := Context{
HTTPPath: "/path_to_t1",
Values: values,
}
_, err := context.Render(template)
assert.Error(t, err)
})
}
func TestRenderConditions(t *testing.T) {
t.Run("match conditions happy code path", func(t *testing.T) {
raw := `{{.AMQPPayload | jsonPath "dl_number" | contains "K1111111" | not}}`
c := Context{
AMQPPayload: `{"dl_number": "K3333333"}`,
}
assert.True(t, c.MatchCondition(raw))
})
t.Run("match conditions not eq", func(t *testing.T) {
raw := `{{eq (.AMQPPayload | jsonPath "dl_number") "12345678"}}`
c := Context{
AMQPPayload: `{"dl_number": "K1111111"}`,
}
assert.False(t, c.MatchCondition(raw))
})
t.Run("match conditions when condition is empty string", func(t *testing.T) {
raw := ""
c := Context{}
assert.True(t, c.MatchCondition(raw))
})
}
func TestRenderRedis(t *testing.T) {
t.Run("Set redis in template", func(t *testing.T) {
raw := `{{.HTTPHeader.Get "X-TOKEN" | redisDo "SET" "k1"}}`
c := Context{
om: &OpenMock{},
HTTPHeader: http.Header{},
}
c.HTTPHeader.Set("X-TOKEN", "t123")
_, err := c.Render(raw)
assert.NoError(t, err)
v, _ := redis.String(c.om.redis.Do("GET", "k1"))
assert.Equal(t, "t123", v)
})
t.Run("Render arrays in template", func(t *testing.T) {
raw := `
{
"setup1": "{{redisDo "RPUSH" "random" uuidv4}}",
"setup2": "{{redisDo "RPUSH" "random" uuidv4}}",
"setup3": "{{redisDo "RPUSH" "random" uuidv4}}",
"random": [
{{range $i, $v := redisDo "LRANGE" "random" 0 -1 | split ";;"}}
"{{$v}}"
{{end}}
]
}
`
c := Context{om: &OpenMock{}}
r, err := c.Render(raw)
assert.NoError(t, err)
assert.Contains(t, r, "-4")
})
}