forked from sreeharikmarar/tsb-wasm-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
315 lines (262 loc) · 8.77 KB
/
main_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
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
package main
import (
"encoding/json"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/proxytest"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
vmTest(t, func(t *testing.T, vm types.VMContext) {
opt := proxytest.NewEmulatorOption().WithVMContext(vm)
host, reset := proxytest.NewHostEmulator(opt)
defer reset()
t.Run("add a default request header", func(t *testing.T) {
// Initialize http context.
id := host.InitializeHttpContext()
// Call OnHttpRequesteHeaders.
hs := [][2]string{}
action := host.CallOnRequestHeaders(id,
hs, false)
require.Equal(t, types.ActionContinue, action)
resultHeaders := host.GetCurrentRequestHeaders(id)
var found bool
for _, val := range resultHeaders {
if val[0] == "x-request-header" {
require.Equal(t, "changed/created by wasm", val[1])
found = true
}
}
require.True(t, found)
// Call OnHttpStreamDone.
host.CompleteHttpContext(id)
// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, fmt.Sprintf("%d finished", id))
require.Contains(t, logs, "request header --> x-request-header: changed/created by wasm")
})
t.Run("add a new request header", func(t *testing.T) {
// Initialize http context.
id := host.InitializeHttpContext()
// Call OnHttpRequesteHeaders.
hs := [][2]string{{"key", "value"}}
action := host.CallOnRequestHeaders(id,
hs, false)
require.Equal(t, types.ActionContinue, action)
resultHeaders := host.GetCurrentRequestHeaders(id)
var found bool
for _, val := range resultHeaders {
if val[0] == "key" {
require.Equal(t, "value", val[1])
found = true
}
}
require.True(t, found)
// Call OnHttpStreamDone.
host.CompleteHttpContext(id)
// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, fmt.Sprintf("%d finished", id))
require.Contains(t, logs, "request header --> key: value")
require.Contains(t, logs, "request header --> x-request-header: changed/created by wasm")
})
t.Run("modify the request header", func(t *testing.T) {
// Initialize http context.
id := host.InitializeHttpContext()
// Call OnHttpRequesteHeaders.
hs := [][2]string{{"x-request-header", "test"}}
action := host.CallOnRequestHeaders(id,
hs, false)
require.Equal(t, types.ActionContinue, action)
// Check headers.
resultHeaders := host.GetCurrentRequestHeaders(id)
var found bool
for _, val := range resultHeaders {
if val[0] == "x-request-header" {
require.Equal(t, "changed/created by wasm", val[1])
found = true
}
}
require.True(t, found)
// Call OnHttpStreamDone.
host.CompleteHttpContext(id)
// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, fmt.Sprintf("%d finished", id))
require.Contains(t, logs, "request header --> x-request-header: changed/created by wasm")
})
})
}
func TestHttpHeaders_OnHttpRequestHeadersWithOnPluginStart(t *testing.T) {
vmTest(t, func(t *testing.T, vm types.VMContext) {
proxyConfig, err := proxyConfiguration("x-user-id", "123", "request")
if err != nil {
require.NoError(t, err)
}
opt := proxytest.NewEmulatorOption().
WithVMContext(vm).
WithPluginConfiguration(proxyConfig)
host, reset := proxytest.NewHostEmulator(opt)
defer reset()
t.Run("add a new request header based on plugin configuration", func(t *testing.T) {
// call start Plugin to configure plugin configuration in context
require.Equal(t, host.StartPlugin(), types.OnPluginStartStatusOK)
// Initialize http context.
id := host.InitializeHttpContext()
hs := [][2]string{}
action := host.CallOnRequestHeaders(id,
hs, false)
require.Equal(t, types.ActionContinue, action)
// Check headers.
resultHeaders := host.GetCurrentRequestHeaders(id)
var found bool
for _, val := range resultHeaders {
if val[0] == "x-user-id" {
require.Equal(t, "123", val[1])
found = true
}
}
require.True(t, found)
// Call OnHttpStreamDone.
host.CompleteHttpContext(id)
// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, fmt.Sprintf("%d finished", id))
require.Contains(t, logs, "request header --> x-request-header: changed/created by wasm")
require.Contains(t, logs, "request header --> x-user-id: 123")
})
})
}
func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
vmTest(t, func(t *testing.T, vm types.VMContext) {
opt := proxytest.NewEmulatorOption().WithVMContext(vm)
host, reset := proxytest.NewHostEmulator(opt)
defer reset()
t.Run("add a default response header", func(t *testing.T) {
// Initialize http context.
id := host.InitializeHttpContext()
// Call OnHttpRequesteHeaders.
hs := [][2]string{}
action := host.CallOnResponseHeaders(id,
hs, false)
require.Equal(t, types.ActionContinue, action)
resultHeaders := host.GetCurrentResponseHeaders(id)
var found bool
for _, val := range resultHeaders {
if val[0] == "x-proxy-wasm-go-sdk-example" {
require.Equal(t, "http_headers", val[1])
found = true
}
}
require.True(t, found)
// Call OnHttpStreamDone.
host.CompleteHttpContext(id)
// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, fmt.Sprintf("%d finished", id))
require.Contains(t, logs, "response header <-- x-proxy-wasm-go-sdk-example: http_headers")
})
t.Run("add a new response header", func(t *testing.T) {
// Initialize http context.
id := host.InitializeHttpContext()
// Call OnHttpRequesteHeaders.
hs := [][2]string{{"x-user-id", "123"}}
action := host.CallOnResponseHeaders(id,
hs, false)
require.Equal(t, types.ActionContinue, action)
// Check headers.
resultHeaders := host.GetCurrentResponseHeaders(id)
var found bool
for _, val := range resultHeaders {
if val[0] == "x-user-id" {
require.Equal(t, "123", val[1])
found = true
}
}
require.True(t, found)
// Call OnHttpStreamDone.
host.CompleteHttpContext(id)
// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, fmt.Sprintf("%d finished", id))
require.Contains(t, logs, "response header <-- x-proxy-wasm-go-sdk-example: http_headers")
require.Contains(t, logs, "response header <-- x-user-id: 123")
})
})
}
func TestHttpHeaders_OnHttpResponseHeadersWithOnPluginStart(t *testing.T) {
vmTest(t, func(t *testing.T, vm types.VMContext) {
proxyConfig, err := proxyConfiguration("x-ingress-header", "powered by TSB", "response")
if err != nil {
require.NoError(t, err)
}
opt := proxytest.NewEmulatorOption().
WithVMContext(vm).
WithPluginConfiguration(proxyConfig)
host, reset := proxytest.NewHostEmulator(opt)
defer reset()
t.Run("add a new response header based on plugin configuration", func(t *testing.T) {
// call start Plugin to configure plugin configuration in context
require.Equal(t, host.StartPlugin(), types.OnPluginStartStatusOK)
// Initialize http context.
id := host.InitializeHttpContext()
hs := [][2]string{}
action := host.CallOnResponseHeaders(id,
hs, false)
require.Equal(t, types.ActionContinue, action)
// Check headers.
resultHeaders := host.GetCurrentResponseHeaders(id)
var found bool
for _, val := range resultHeaders {
if val[0] == "x-ingress-header" {
require.Equal(t, "powered by TSB", val[1])
found = true
}
}
require.True(t, found)
// Call OnHttpStreamDone.
host.CompleteHttpContext(id)
// Check Envoy logs.
logs := host.GetInfoLogs()
require.Contains(t, logs, fmt.Sprintf("%d finished", id))
require.Contains(t, logs, "response header <-- x-proxy-wasm-go-sdk-example: http_headers")
require.Contains(t, logs, "response header <-- x-ingress-header: powered by TSB")
})
})
}
// vmTest executes f twice, once with a types.VMContext that executes plugin code directly
// in the host, and again by executing the plugin code within the compiled main.wasm binary.
// Execution with main.wasm will be skipped if the file cannot be found.
func vmTest(t *testing.T, f func(*testing.T, types.VMContext)) {
t.Helper()
t.Run("go", func(t *testing.T) {
f(t, &vmContext{})
})
t.Run("wasm", func(t *testing.T) {
wasm, err := os.ReadFile("main.wasm")
if err != nil {
t.Skip("wasm not found")
}
v, err := proxytest.NewWasmVMContext(wasm)
require.NoError(t, err)
defer v.Close()
f(t, v)
})
}
func proxyConfiguration(header string, value string, path string) ([]byte, error) {
rawJson, err := json.Marshal(&struct {
Header string `json:"header"`
Value string `json:"value"`
Path string `json:"path"`
}{
Header: header,
Value: value,
Path: path,
})
if err != nil {
return nil, err
}
return rawJson, nil
}