-
Notifications
You must be signed in to change notification settings - Fork 513
/
eventsource-close.js
59 lines (49 loc) · 1.91 KB
/
eventsource-close.js
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
'use strict'
const assert = require('node:assert')
const events = require('node:events')
const http = require('node:http')
const { setTimeout } = require('node:timers/promises')
const { test, describe } = require('node:test')
const { EventSource } = require('../../lib/web/eventsource/eventsource')
describe('EventSource - close', () => {
test('should not emit error when closing the EventSource Instance', async () => {
const server = http.createServer((req, res) => {
assert.strictEqual(req.headers.connection, 'keep-alive')
res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
res.write('data: hello\n\n')
})
server.listen(0)
await events.once(server, 'listening')
const port = server.address().port
const eventSourceInstance = new EventSource(`http:https://localhost:${port}`)
eventSourceInstance.onopen = async () => {
eventSourceInstance.close()
await setTimeout(1000, { ref: false })
server.close()
}
eventSourceInstance.onerror = () => {
assert.fail('Should not have errored')
}
})
test('should set readyState to CLOSED', async () => {
const server = http.createServer((req, res) => {
assert.strictEqual(req.headers.connection, 'keep-alive')
res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
res.write('data: hello\n\n')
})
server.listen(0)
await events.once(server, 'listening')
const port = server.address().port
const eventSourceInstance = new EventSource(`http:https://localhost:${port}`)
eventSourceInstance.onopen = () => {
assert.strictEqual(eventSourceInstance.readyState, EventSource.OPEN)
eventSourceInstance.close()
assert.strictEqual(eventSourceInstance.readyState, EventSource.CLOSED)
}
eventSourceInstance.onerror = () => {
assert.fail('Should not have errored')
}
await setTimeout(2000, { ref: false })
server.close()
})
})