forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_sent_event_test.ts
217 lines (198 loc) · 5.62 KB
/
server_sent_event_test.ts
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
// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
// deno-lint-ignore-file
import {
assert,
assertEquals,
BufWriter,
StringWriter,
test,
} from "./test_deps.ts";
import { Application } from "./application.ts";
import { ServerSentEvent, ServerSentEventTarget } from "./server_sent_event.ts";
import { ServerRequest } from "./types.d.ts";
const preamble = `HTTP/1.1 200 OK
connection: Keep-Alive
content-type: text/event-stream
cache-control: no-cache
keep-alive: timeout=9007199254740991
`;
const env = {
appTarget: new EventTarget(),
appErrorEvents: [] as ErrorEvent[],
outWriter: new StringWriter(),
connCloseCalled: false,
};
function createMockApp(): Application {
env.appTarget = new EventTarget();
env.appErrorEvents = [];
env.appTarget.addEventListener(
"error",
(evt) => {
env.appErrorEvents.push(evt as ErrorEvent);
},
);
return env.appTarget as any;
}
function createMockServerRequest(): ServerRequest {
env.outWriter = new StringWriter();
env.connCloseCalled = false;
return {
conn: {
close() {
env.connCloseCalled = true;
},
},
w: new BufWriter(env.outWriter),
} as any;
}
test({
name: "ServerSentEvent - construction",
fn() {
const evt = new ServerSentEvent("message", "foobar");
assertEquals(evt.type, "message");
assertEquals(evt.data, "foobar");
assertEquals(evt.id, undefined);
assertEquals(String(evt), `event: message\ndata: foobar\n\n`);
},
});
test({
name: "ServerSentEvent - data coercion",
fn() {
const evt = new ServerSentEvent("ping", { hello: true });
assertEquals(evt.type, "ping");
assertEquals(evt.data, `{"hello":true}`);
assertEquals(evt.id, undefined);
assertEquals(String(evt), `event: ping\ndata: {"hello":true}\n\n`);
},
});
test({
name: "ServerSentEvent - init id",
fn() {
const evt = new ServerSentEvent("ping", "foobar", { id: 1234 });
assertEquals(evt.type, "ping");
assertEquals(evt.data, `foobar`);
assertEquals(evt.id, 1234);
assertEquals(
String(evt),
`event: ping\nid: 1234\ndata: foobar\n\n`,
);
},
});
test({
name: "ServerSentEvent - data space",
fn() {
const evt = new ServerSentEvent("ping", { hello: [1, 2, 3] }, { space: 2 });
assertEquals(evt.type, "ping");
assertEquals(evt.data, `{\n "hello": [\n 1,\n 2,\n 3\n ]\n}`);
assertEquals(
String(evt),
`event: ping\ndata: {\ndata: "hello": [\ndata: 1,\ndata: 2,\ndata: 3\ndata: ]\ndata: }\n\n`,
);
},
});
test({
name: "ServerSentEvent - __message",
fn() {
const evt = new ServerSentEvent("__message", { hello: "world" });
assertEquals(evt.type, "__message");
assertEquals(evt.data, `{"hello":"world"}`);
assertEquals(String(evt), `data: {"hello":"world"}\n\n`);
},
});
test({
name: "ServerSentEventTarget - construction",
async fn() {
const sse = new ServerSentEventTarget(
createMockApp(),
createMockServerRequest(),
);
assertEquals(sse.closed, false);
await sse.close();
assert(env.connCloseCalled);
assertEquals(sse.closed, true);
const actual = String(env.outWriter);
assertEquals(actual, preamble);
assertEquals(env.appErrorEvents.length, 0);
},
});
test({
name: "ServerSentEventTaget - construction with headers",
async fn() {
const expected =
`HTTP/1.1 200 OK\nconnection: Keep-Alive\ncontent-type: text/event-stream\ncache-control: special\nkeep-alive: timeout=9007199254740991\nx-oak: test\n\n`;
const sse = new ServerSentEventTarget(
createMockApp(),
createMockServerRequest(),
{
headers: new Headers([["X-Oak", "test"], ["Cache-Control", "special"]]),
},
);
await sse.close();
const actual = String(env.outWriter);
assertEquals(actual, expected);
},
});
test({
name: "ServerSentEventTarget - dispatchEvent",
async fn() {
const sse = new ServerSentEventTarget(
createMockApp(),
createMockServerRequest(),
);
const evt = new ServerSentEvent("message", "foobar");
sse.dispatchEvent(evt);
await sse.close();
assert(env.connCloseCalled);
const actual = env.outWriter.toString();
assert(actual.endsWith(`\n\nevent: message\ndata: foobar\n\n`));
assertEquals(env.appErrorEvents.length, 0);
},
});
test({
name: "ServerSentEventTarget - dispatchMessage",
async fn() {
const sse = new ServerSentEventTarget(
createMockApp(),
createMockServerRequest(),
);
sse.dispatchMessage("foobar");
await sse.close();
assert(env.connCloseCalled);
const actual = env.outWriter.toString();
assert(actual.endsWith(`\n\ndata: foobar\n\n`));
assertEquals(env.appErrorEvents.length, 0);
},
});
test({
name: "ServerSentEventTarget - dispatchComment",
async fn() {
const sse = new ServerSentEventTarget(
createMockApp(),
createMockServerRequest(),
);
sse.dispatchComment("foobar");
await sse.close();
assert(env.connCloseCalled);
const actual = env.outWriter.toString();
assert(actual.endsWith(`\n\n: foobar\n\n`));
assertEquals(env.appErrorEvents.length, 0);
},
});
test({
name: "ServerSentEVentTarget - synchronous dispatch",
async fn() {
const sse = new ServerSentEventTarget(
createMockApp(),
createMockServerRequest(),
);
sse.dispatchComment("1");
sse.dispatchComment("2");
sse.dispatchComment("3");
sse.dispatchComment("4");
await sse.close();
assert(env.connCloseCalled);
const actual = env.outWriter.toString();
assert(actual.endsWith(`\n\n: 1\n\n: 2\n\n: 3\n\n: 4\n\n`));
assertEquals(env.appErrorEvents.length, 0);
},
});