-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsse_test.go
507 lines (454 loc) · 14.8 KB
/
sse_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
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
package sse
import (
"crypto/tls"
"io"
"net/http"
"net/http/cookiejar"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
httpModule "go.k6.io/k6/js/modules/k6/http"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils/httpmultibin"
"go.k6.io/k6/metrics"
"gopkg.in/guregu/null.v3"
)
func assertSSEMetricsEmitted(t *testing.T, sampleContainers []metrics.SampleContainer, subprotocol, url string, status int, group string) { //nolint:unparam
seenEvents := false
seenRequestDuration := false
seenHTTPReq := false
for _, sampleContainer := range sampleContainers {
for _, sample := range sampleContainer.GetSamples() {
tags := sample.Tags.Map()
if tags["url"] == url {
switch sample.Metric.Name {
case metrics.HTTPReqsName:
seenHTTPReq = true
case metrics.HTTPReqDurationName:
seenRequestDuration = true
case MetricEventName:
seenEvents = true
}
assert.Equal(t, strconv.Itoa(status), tags["status"])
assert.Equal(t, subprotocol, tags["subproto"])
assert.Equal(t, group, tags["group"])
}
}
}
assert.True(t, seenEvents, "url %s didn't emit SSE events", url)
assert.True(t, seenRequestDuration, "url %s didn't emit seenRequestDuration", url)
assert.True(t, seenHTTPReq, "url %s didn't emit seenHTTPReq", url)
}
func assertMetricEmittedCount(t *testing.T, metricName string, sampleContainers []metrics.SampleContainer, url string, count int) {
t.Helper()
actualCount := 0
for _, sampleContainer := range sampleContainers {
for _, sample := range sampleContainer.GetSamples() {
surl, ok := sample.Tags.Get("url")
assert.True(t, ok)
if surl == url && sample.Metric.Name == metricName {
actualCount++
}
}
}
assert.Equal(t, count, actualCount, "url %s emitted %s %d times, expected was %d times", url, metricName, actualCount, count)
}
func assertSseCount(t *testing.T, sampleContainers []metrics.SampleContainer, url string, count int) {
assertMetricEmittedCount(t, MetricEventName, sampleContainers, url, count)
}
type testState struct {
*modulestest.Runtime
tb *httpmultibin.HTTPMultiBin
samples chan metrics.SampleContainer
}
func newTestState(tb testing.TB) testState {
httpMultiBin := httpmultibin.NewHTTPMultiBin(tb)
httpMultiBin.Mux.Handle("/sse", sseHandler(tb, false))
httpMultiBin.Mux.Handle("/sse-invalid", sseHandler(tb, true))
testRuntime := modulestest.NewRuntime(tb)
registry := metrics.NewRegistry()
samples := make(chan metrics.SampleContainer, 1000)
state := &lib.State{
Dialer: httpMultiBin.Dialer,
Options: lib.Options{
SystemTags: metrics.NewSystemTagSet(
metrics.TagURL,
metrics.TagProto,
metrics.TagStatus,
metrics.TagSubproto,
),
UserAgent: null.StringFrom("TestUserAgent"),
Throw: null.BoolFrom(true),
},
Samples: samples,
TLSConfig: httpMultiBin.TLSClientConfig,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(registry),
Tags: lib.NewVUStateTags(registry.RootTagSet()),
}
m := New().NewModuleInstance(testRuntime.VU)
require.NoError(tb, testRuntime.VU.RuntimeField.Set("sse", m.Exports().Default))
testRuntime.MoveToVUContext(state)
return testState{
Runtime: testRuntime,
tb: httpMultiBin,
samples: samples,
}
}
func TestOpen(t *testing.T) {
t.Parallel()
t.Run("nominal get", func(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
_, err := test.VU.Runtime().RunString(sr(`
var open = false;
var error = false;
var events = [];
var res = sse.open("HTTPBIN_IP_URL/sse", function(client){
client.on("error", function(err) {
error = true
});
client.on("open", function(err) {
open = true
});
client.on("error", function(err) {
error = true
});
client.on("event", function(event) {
events.push(event);
});
});
if (!open) {
throw new Error("opened is not called");
}
if (error) {
throw new Error("error raised");
}
for (let i = 0; i < events.length; i++) {
let event = events[i];
switch(i) {
case 0:
if (event.id !== "ABCD") {
throw new Error("unexpected event id: " + event.id);
}
if (event.comment !== 'hello') {
throw new Error("unexpected event comment: " + event.comment);
}
if (event.data !== '{"ping": "pong"}\n{"hello": "sse"}') {
throw new Error("unexpected event data: " + event.data);
}
break;
case 1:
if (event.id !== "") {
throw new Error("unexpected event id: " + event.id);
}
if (event.name !== "EFGH") {
throw new Error("unexpected event name: " + event.name);
}
if (event.data !== '{"hello": "sse"}') {
throw new Error("unexpected event data: " + event.data);
}
break;
default:
throw new Error("unexpected event");
}
}
`))
require.NoError(t, err)
samplesBuf := metrics.GetBufferedSamples(test.samples)
assertSseCount(t, samplesBuf, sr("HTTPBIN_IP_URL/sse"), 2)
})
t.Run("post method", func(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
_, err := test.VU.Runtime().RunString(sr(`
var events = [];
var res = sse.open("HTTPBIN_IP_URL/sse", {method: 'POST', body: '{"ping": true}', headers: {"content-type": "application/json", "Authorization": "Bearer XXXX"}}, function(client){
client.on("event", function(event) {
events.push(event);
});
});
for (let i = 0; i < events.length; i++) {
let event = events[i];
switch(i) {
case 0:
if (event.id !== "pong") {
throw new Error("unexpected event id: " + event.id);
}
if (event.data !== '{"ping": "pong"}') {
throw new Error("unexpected event data: " + event.data);
}
break;
default:
throw new Error("unexpected event");
}
}
`))
require.NoError(t, err)
samplesBuf := metrics.GetBufferedSamples(test.samples)
assertSseCount(t, samplesBuf, sr("HTTPBIN_IP_URL/sse"), 1)
})
}
func TestClose(t *testing.T) {
t.Parallel()
t.Run("nominal get close", func(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
_, err := test.VU.Runtime().RunString(sr(`
var open = false;
var error = false;
var events = [];
var res = sse.open("HTTPBIN_IP_URL/sse", function(client){
client.on("error", function(err) {
error = true
});
client.on("open", function(err) {
open = true
});
client.on("error", function(err) {
error = true
});
client.on("event", function(event) {
client.close()
events.push(event);
});
});
if (!open) {
throw new Error("opened is not called");
}
if (error) {
throw new Error("error raised");
}
if (events.length != 1) {
throw new Error("unexpected number of events");
}
`))
require.NoError(t, err)
samplesBuf := metrics.GetBufferedSamples(test.samples)
assertSseCount(t, samplesBuf, sr("HTTPBIN_IP_URL/sse"), 1)
})
t.Run("post method", func(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
_, err := test.VU.Runtime().RunString(sr(`
var events = [];
var res = sse.open("HTTPBIN_IP_URL/sse", {method: 'POST', body: '{"ping": true}', headers: {"content-type": "application/json", "Authorization": "Bearer XXXX"}}, function(client){
client.on("event", function(event) {
events.push(event);
});
});
for (let i = 0; i < events.length; i++) {
let event = events[i];
switch(i) {
case 0:
if (event.id !== "pong") {
throw new Error("unexpected event id: " + event.id);
}
if (event.data !== '{"ping": "pong"}') {
throw new Error("unexpected event data: " + event.data);
}
break;
default:
throw new Error("unexpected event");
}
}
`))
require.NoError(t, err)
samplesBuf := metrics.GetBufferedSamples(test.samples)
assertSseCount(t, samplesBuf, sr("HTTPBIN_IP_URL/sse"), 1)
})
}
func TestErrors(t *testing.T) {
t.Parallel()
t.Run("invalid_url", func(t *testing.T) {
t.Parallel()
test := newTestState(t)
_, err := test.VU.Runtime().RunString(`
var res = sse.open("INVALID", function(client){
client.on("open", function(client){});
});
`)
assert.Error(t, err)
})
t.Run("error_in_setup", func(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
_, err := test.VU.Runtime().RunString(sr(`
var res = sse.open("HTTPBIN_URL/sse-echo", function(client){
throw new Error("error in setup");
});
`))
assert.Error(t, err)
})
t.Run("error_in_response", func(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
_, err := test.VU.Runtime().RunString(sr(`
var error = false;
var res = sse.open("HTTPBIN_IP_URL/sse-invalid", function(client){
client.on("error", function(err) {
error = true
});
});
if (!error) {
throw new Error("no error raised");
}
`))
require.NoError(t, err)
})
}
func TestOpenWrongStatusCode(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
test.VU.StateField.Options.Throw = null.BoolFrom(false)
_, err := test.VU.Runtime().RunString(sr(`
var res = sse.open("HTTPBIN_IP_URL/status/404", function(client){});
if (res.status != 404) {
throw new Error ("no status code set for invalid response");
}
`))
assert.NoError(t, err)
}
func TestUserAgent(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
test.tb.Mux.HandleFunc("/sse-echo-useragent", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Echo back User-Agent header if it exists
responseHeaders := w.Header()
if ua := req.Header.Get("User-Agent"); ua != "" {
responseHeaders.Add("X-Echo-User-Agent", req.Header.Get("User-Agent"))
}
_, err := w.Write([]byte(`data: {"ping": "pong"}` + "\n\n"))
require.NoError(t, err)
}))
// client handler should echo back User-Agent as Echo-User-Agent for this test to work
_, err := test.VU.Runtime().RunString(sr(`
var res = sse.open("HTTPBIN_IP_URL/sse-echo-useragent", function(client){})
var userAgent = res.headers["X-Echo-User-Agent"];
if (userAgent == undefined) {
throw new Error("user agent is not echoed back by test server");
}
if (userAgent != "TestUserAgent") {
throw new Error("incorrect user agent: " + userAgent);
}
`))
require.NoError(t, err)
assertSSEMetricsEmitted(t, metrics.GetBufferedSamples(test.samples), "", sr("HTTPBIN_IP_URL/sse-echo-useragent"), http.StatusOK, "")
}
func TestCookieJar(t *testing.T) {
t.Parallel()
ts := newTestState(t)
sr := ts.tb.Replacer.Replace
ts.tb.Mux.HandleFunc("/sse-echo-someheader", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
responseHeaders := w.Header()
if sh, err := req.Cookie("someheader"); err == nil {
responseHeaders.Add("Echo-Someheader", sh.Value)
}
_, err := w.Write([]byte(`data: {"ping": "pong"}` + "\n\n"))
require.NoError(t, err)
}))
err := ts.VU.Runtime().Set("http", httpModule.New().NewModuleInstance(ts.VU).Exports().Default)
require.NoError(t, err)
ts.VU.State().CookieJar, _ = cookiejar.New(nil)
_, err = ts.VU.Runtime().RunString(sr(`
var res = sse.open("HTTPBIN_IP_URL/sse-echo-someheader", function(client){})
var someheader = res.headers["Echo-Someheader"];
if (someheader !== undefined) {
throw new Error("someheader is echoed back by test server even though it doesn't exist");
}
http.cookieJar().set("HTTPBIN_IP_URL/sse-echo-someheader", "someheader", "defaultjar")
res = sse.open("HTTPBIN_IP_URL/sse-echo-someheader", function(client){})
someheader = res.headers["Echo-Someheader"];
if (someheader != "defaultjar") {
throw new Error("someheader has wrong value "+ someheader + " instead of defaultjar");
}
var jar = new http.CookieJar();
jar.set("HTTPBIN_IP_URL/sse-echo-someheader", "someheader", "customjar")
res = sse.open("HTTPBIN_IP_URL/sse-echo-someheader", {jar: jar}, function(client){})
someheader = res.headers["Echo-Someheader"];
if (someheader != "customjar") {
throw new Error("someheader has wrong value "+ someheader + " instead of customjar");
}
`))
require.NoError(t, err)
assertSSEMetricsEmitted(t, metrics.GetBufferedSamples(ts.samples), "", sr("HTTPBIN_IP_URL/sse-echo-someheader"), http.StatusOK, "")
}
func TestTLSConfig(t *testing.T) {
t.Parallel()
t.Run("insecure skip verify", func(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
test.VU.StateField.TLSConfig = &tls.Config{
InsecureSkipVerify: true, //nolint:gosec
}
_, err := test.VU.Runtime().RunString(sr(`
var res = sse.open("HTTPBIN_IP_URL/sse", function(client){});
if (res.status != 200) { throw new Error("TLS connection failed with status: " + res.status); }
`))
require.NoError(t, err)
assertSSEMetricsEmitted(t, metrics.GetBufferedSamples(test.samples), "", sr("HTTPBIN_IP_URL/sse"), http.StatusOK, "")
})
t.Run("custom certificates", func(t *testing.T) {
t.Parallel()
test := newTestState(t)
sr := test.tb.Replacer.Replace
test.VU.StateField.TLSConfig = test.tb.TLSClientConfig
_, err := test.VU.Runtime().RunString(sr(`
var res = sse.open("HTTPBIN_IP_URL/sse", function(client){});
if (res.status != 200) {
throw new Error("TLS connection failed with status: " + res.status);
}
`))
require.NoError(t, err)
assertSSEMetricsEmitted(t, metrics.GetBufferedSamples(test.samples), "", sr("HTTPBIN_IP_URL/sse"), http.StatusOK, "")
})
}
// sseHandler handles sse requests and generates some events.
// If generateErrors is true then it generates junk
// without respecting the protocol.
func sseHandler(t testing.TB, generateErrors bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if generateErrors {
_, _ = w.Write([]byte("junk\n"))
} else {
if req.Method == http.MethodPost {
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
assert.Equal(t, "Bearer XXXX", req.Header.Get("Authorization"))
body, err := io.ReadAll(req.Body)
require.NoError(t, err)
if `{"ping": true}` != string(body) {
t.Fail()
}
_, err = w.Write([]byte("id: pong\n")) // event id
require.NoError(t, err)
_, err = w.Write([]byte(`data: {"ping": "pong"}` + "\n\n")) // event data
require.NoError(t, err)
} else {
_, err := w.Write([]byte("retry: 10000\n")) // retry
require.NoError(t, err)
_, err = w.Write([]byte(": hello\n")) // comment
require.NoError(t, err)
_, err = w.Write([]byte("id: ABCD\n")) // id
require.NoError(t, err)
_, err = w.Write([]byte(`data: {"ping": "pong"}` + "\n")) // event 1 data 1
require.NoError(t, err)
_, err = w.Write([]byte(`data: {"hello": "sse"}` + "\n\n")) // event 1 data 2
require.NoError(t, err)
_, err = w.Write([]byte("event: EFGH\n")) // event name
require.NoError(t, err)
_, err = w.Write([]byte(`data: {"hello": "sse"}` + "\n\n")) // event 2 data
require.NoError(t, err)
}
}
})
}