deno.land / std@0.224.0 / http / server_sent_event_stream_test.ts

server_sent_event_stream_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertRejects } from "../assert/mod.ts";import { type ServerSentEventMessage, ServerSentEventStream,} from "./server_sent_event_stream.ts";
function createStream( messages: ServerSentEventMessage[],): ReadableStream<string> { return ReadableStream .from<ServerSentEventMessage>(messages) .pipeThrough(new ServerSentEventStream()) .pipeThrough(new TextDecoderStream());}
Deno.test("ServerSentEventStream enqueues a stringified server-sent event message object", async () => { const stream = createStream([ { comment: "a", event: "b", data: "c\nd\re\r\nf", id: "123", retry: 456, }, { comment: "a", }, { event: "b", }, { data: "c\nd\re\r\nf", }, { id: "123", }, { id: 123, }, { retry: 456, }, ]); const clientMessages = await Array.fromAsync(stream);
assertEquals( clientMessages, [ ":a\nevent:b\ndata:c\ndata:d\ndata:e\ndata:f\nid:123\nretry:456\n\n", ":a\n\n", "event:b\n\n", "data:c\ndata:d\ndata:e\ndata:f\n\n", "id:123\n\n", "id:123\n\n", "retry:456\n\n", ], );});
Deno.test("ServerSentEventStream throws if single-line fields contain a newline", async () => { // Comment await assertRejects( async () => await createStream([{ comment: "a\n" }]).getReader().read(), RangeError, "`message.comment` cannot contain a newline", );
await assertRejects( async () => await createStream([{ comment: "a\r" }]).getReader().read(), RangeError, "`message.comment` cannot contain a newline", );
await assertRejects( async () => await createStream([{ comment: "a\n\r" }]).getReader().read(), RangeError, "`message.comment` cannot contain a newline", );
// Event await assertRejects( async () => await createStream([{ event: "a\n" }]).getReader().read(), RangeError, "`message.event` cannot contain a newline", );
await assertRejects( async () => await createStream([{ event: "a\r" }]).getReader().read(), RangeError, "`message.event` cannot contain a newline", );
await assertRejects( async () => await createStream([{ event: "a\n\r" }]).getReader().read(), RangeError, "`message.event` cannot contain a newline", );
// ID await assertRejects( async () => await createStream([{ id: "a\n" }]).getReader().read(), RangeError, "`message.id` cannot contain a newline", );
await assertRejects( async () => await createStream([{ id: "a\r" }]).getReader().read(), RangeError, "`message.id` cannot contain a newline", );
await assertRejects( async () => await createStream([{ id: "a\n\r" }]).getReader().read(), RangeError, "`message.id` cannot contain a newline", );});
std

Version Info

Tagged at
6 months ago