Debian Codegraph @

test/eventsource/eventsource-message.js

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 'use strict'
2
3 const { once } = require('node:events')
4 const http = require('node:http')
5 const { test, describe, after } = require('node:test')
6 const { EventSource, defaultReconnectionTime } = require('../../lib/web/eventsource/eventsource')
7 const FakeTimers = require('@sinonjs/fake-timers')
8
9 describe('EventSource - message', () => {
10 test('Should not emit a message if only retry field was sent', (t, done) => {
11 t.plan(2)
12
13 const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => {
14 res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
15 res.write('retry: 100\n\n')
16 setTimeout(() => res.end(), 100)
17 })
18
19 after(() => server.close())
20
21 server.listen(0, () => {
22 const port = server.address().port
23
24 const start = Date.now()
25 let connectionCount = 0
26 const eventSourceInstance = new EventSource(`http://localhost:${port}`)
27 eventSourceInstance.onopen = () => {
28 if (++connectionCount === 2) {
29 t.assert.ok(Date.now() - start >= 100)
30 t.assert.ok(Date.now() - start < 1000)
31 eventSourceInstance.close()
32 done()
33 }
34 }
35 eventSourceInstance.onmessage = () => {
36 t.assert.fail('Should not have received a message')
37 eventSourceInstance.close()
38 }
39 })
40 })
41
42 test('Should not emit a message if no data is provided', async (t) => {
43 t.plan(1)
44
45 const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => {
46 res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
47 res.write('event:message\n\n')
48 setTimeout(() => res.end(), 100)
49 })
50
51 after(() => server.close())
52 await once(server.listen(0), 'listening')
53 const port = server.address().port
54
55 const eventSourceInstance = new EventSource(`http://localhost:${port}`)
56
57 eventSourceInstance.onmessage = () => {
58 t.assert.fail('Should not have received a message')
59 eventSourceInstance.close()
60 }
61
62 eventSourceInstance.close()
63 t.assert.ok('Should not have received a message')
64 })
65
66 test('Should emit a custom type message if data is provided', (t, done) => {
67 t.plan(1)
68
69 const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => {
70 res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
71 res.write('event:custom\ndata:test\n\n')
72 setTimeout(() => res.end(), 100)
73 })
74
75 after(() => server.close())
76
77 server.listen(0, () => {
78 const port = server.address().port
79
80 const eventSourceInstance = new EventSource(`http://localhost:${port}`)
81 eventSourceInstance.addEventListener('custom', () => {
82 t.assert.ok(true)
83 done()
84 eventSourceInstance.close()
85 })
86 })
87 })
88
89 test('Should emit a message event if data is provided', (t, done) => {
90 t.plan(1)
91
92 const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => {
93 res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
94 res.write('data:test\n\n')
95 setTimeout(() => res.end(), 100)
96 })
97
98 after(() => server.close())
99
100 server.listen(0, () => {
101 const port = server.address().port
102
103 const eventSourceInstance = new EventSource(`http://localhost:${port}`)
104 eventSourceInstance.addEventListener('message', () => {
105 t.assert.ok(true)
106 eventSourceInstance.close()
107 done()
108 })
109 })
110 })
111
112 test('Should emit a message event if data as a field is provided', (t, done) => {
113 t.plan(1)
114
115 const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => {
116 res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
117 res.write('data\n\n')
118 setTimeout(() => res.end(), 100)
119 })
120
121 after(() => server.close())
122
123 server.listen(0, () => {
124 const port = server.address().port
125
126 const eventSourceInstance = new EventSource(`http://localhost:${port}`)
127 eventSourceInstance.addEventListener('message', () => {
128 t.assert.ok(true)
129 eventSourceInstance.close()
130 done()
131 })
132 })
133 })
134
135 test('Should emit a custom message event if data is empty', (t, done) => {
136 t.plan(1)
137
138 const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => {
139 res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
140 res.write('event:custom\ndata:\n\n')
141 setTimeout(() => res.end(), 100)
142 })
143
144 after(() => server.close())
145
146 server.listen(0, () => {
147 const port = server.address().port
148
149 const eventSourceInstance = new EventSource(`http://localhost:${port}`)
150 eventSourceInstance.addEventListener('custom', () => {
151 t.assert.ok(true)
152 eventSourceInstance.close()
153 done()
154 })
155 })
156 })
157
158 test('Should emit a message event if data is empty', (t, done) => {
159 t.plan(1)
160
161 const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => {
162 res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
163 res.write('data:\n\n')
164 setTimeout(() => res.end(), 100)
165 })
166
167 after(() => server.close())
168
169 server.listen(0, () => {
170 const port = server.address().port
171
172 const eventSourceInstance = new EventSource(`http://localhost:${port}`)
173 eventSourceInstance.addEventListener('message', () => {
174 t.assert.ok(true)
175 eventSourceInstance.close()
176 done()
177 })
178 })
179 })
180
181 test('Should emit a custom message event if data only as a field is provided', (t, done) => {
182 t.plan(1)
183
184 const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => {
185 res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
186 res.write('event:custom\ndata\n\n')
187 setTimeout(() => res.end(), 100)
188 })
189
190 after(() => server.close())
191
192 server.listen(0, () => {
193 const port = server.address().port
194
195 const eventSourceInstance = new EventSource(`http://localhost:${port}`)
196 eventSourceInstance.addEventListener('custom', () => {
197 t.assert.ok(true)
198 eventSourceInstance.close()
199 done()
200 })
201 })
202 })
203
204 test('Should not emit a custom type message if no data is provided', (t, done) => {
•205 const clock = FakeTimers.install({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] })
206 after(() => clock.uninstall())
207
208 t.plan(1)
209
210 const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => {
211 res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
212 res.write('event:custom\n\n')
213 setTimeout(() => res.end(), 100)
214 })
215
216 let reconnectionCount = 0
217 after(() => server.close())
218
219 server.listen(0, async () => {
220 const port = server.address().port
221
222 const eventSourceInstance = new EventSource(`http://localhost:${port}`)
223 eventSourceInstance.onopen = () => {
224 if (++reconnectionCount === 2) {
225 t.assert.ok(true)
226 eventSourceInstance.close()
227 done()
228 }
229 }
230 eventSourceInstance.addEventListener('custom', () => {
231 t.assert.fail('Should not have received a message')
232 eventSourceInstance.close()
233 })
234
235 await once(eventSourceInstance, 'open')
236 clock.tick(defaultReconnectionTime)
237 await once(eventSourceInstance, 'error')
238
239 clock.tick(defaultReconnectionTime)
240 await once(eventSourceInstance, 'open')
241 clock.tick(defaultReconnectionTime)
242 })
243 })
244 })