Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions test/eventsource/eventsource-attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const assert = require('node:assert')
const events = require('node:events')
const { once } = require('node:events')
const http = require('node:http')
const { test, describe } = require('node:test')
const { EventSource } = require('../../lib/web/eventsource/eventsource')
Expand All @@ -11,8 +11,7 @@ describe('EventSource - eventhandler idl', async () => {
res.writeHead(200, 'dummy')
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

let done = 0
Expand Down
46 changes: 25 additions & 21 deletions test/eventsource/eventsource-close.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,63 @@
'use strict'

const assert = require('node:assert')
const events = require('node:events')
const { tspl } = require('@matteo.collina/tspl')
const { once } = require('node:events')
const http = require('node:http')
const { setTimeout } = require('node:timers/promises')
const { test, describe } = require('node:test')
const { test, describe, after } = require('node:test')
const { EventSource } = require('../../lib/web/eventsource/eventsource')

describe('EventSource - close', () => {
test('should not emit error when closing the EventSource Instance', async () => {
test('should not emit error when closing the EventSource Instance', async (t) => {
t = tspl(t, { plan: 1 })

const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
assert.strictEqual(req.headers.connection, 'keep-alive')
t.strictEqual(req.headers.connection, 'keep-alive')
res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' })
res.write('data: hello\n\n')

res.on('close', () => {
server.close()
})
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
eventSourceInstance.onopen = async () => {
eventSourceInstance.onopen = () => {
eventSourceInstance.close()
await setTimeout(1000, { ref: false })
server.close()
}

eventSourceInstance.onerror = () => {
assert.fail('Should not have errored')
t.fail('Should not have errored')
}

await t.completed
})

test('should set readyState to CLOSED', async () => {
test('should set readyState to CLOSED', async (t) => {
t = tspl(t, { plan: 3 })
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
assert.strictEqual(req.headers.connection, 'keep-alive')
t.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')
after(() => server.close())
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
eventSourceInstance.onopen = () => {
assert.strictEqual(eventSourceInstance.readyState, EventSource.OPEN)
t.strictEqual(eventSourceInstance.readyState, EventSource.OPEN)
eventSourceInstance.close()
assert.strictEqual(eventSourceInstance.readyState, EventSource.CLOSED)
t.strictEqual(eventSourceInstance.readyState, EventSource.CLOSED)
}

eventSourceInstance.onerror = () => {
assert.fail('Should not have errored')
t.fail('Should not have errored')
}

await setTimeout(2000, { ref: false })
server.close()
await t.completed
})
})
42 changes: 17 additions & 25 deletions test/eventsource/eventsource-connect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const assert = require('node:assert')
const events = require('node:events')
const { once } = require('node:events')
const http = require('node:http')
const { test, describe, after } = require('node:test')
const FakeTimers = require('@sinonjs/fake-timers')
Expand All @@ -16,8 +16,7 @@ describe('EventSource - sending correct request headers', () => {
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
Expand All @@ -38,8 +37,7 @@ describe('EventSource - sending correct request headers', () => {
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
Expand All @@ -61,8 +59,7 @@ describe('EventSource - sending correct request headers', () => {
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
Expand All @@ -83,8 +80,7 @@ describe('EventSource - sending correct request headers', () => {
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
Expand All @@ -106,8 +102,7 @@ describe('EventSource - received response must have content-type to be text/even
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
Expand All @@ -127,8 +122,7 @@ describe('EventSource - received response must have content-type to be text/even
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
Expand All @@ -148,8 +142,7 @@ describe('EventSource - received response must have content-type to be text/even
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
Expand All @@ -169,8 +162,7 @@ describe('EventSource - received response must have content-type to be text/even
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
Expand Down Expand Up @@ -199,15 +191,15 @@ describe('EventSource - received response must have content-type to be text/even
}
clock.tick(reconnectionTime)

await events.once(eventSourceInstance, 'error')
await once(eventSourceInstance, 'error')

const start = Date.now()
clock.tick(reconnectionTime)
await events.once(eventSourceInstance, 'error')
await once(eventSourceInstance, 'error')
clock.tick(reconnectionTime)
await events.once(eventSourceInstance, 'error')
await once(eventSourceInstance, 'error')
clock.tick(reconnectionTime)
await events.once(eventSourceInstance, 'error')
await once(eventSourceInstance, 'error')
const end = Date.now()

eventSourceInstance.close()
Expand All @@ -234,15 +226,15 @@ describe('EventSource - received response must have content-type to be text/even
onerrorCalls.push(error)
}

await events.once(eventSourceInstance, 'error')
await once(eventSourceInstance, 'error')

const start = Date.now()
clock.tick(reconnectionTime)
await events.once(eventSourceInstance, 'error')
await once(eventSourceInstance, 'error')
clock.tick(reconnectionTime)
await events.once(eventSourceInstance, 'error')
await once(eventSourceInstance, 'error')
clock.tick(reconnectionTime)
await events.once(eventSourceInstance, 'error')
await once(eventSourceInstance, 'error')
const end = Date.now()

eventSourceInstance.close()
Expand Down
5 changes: 2 additions & 3 deletions test/eventsource/eventsource-constructor-stringify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const assert = require('node:assert')
const events = require('node:events')
const { once } = require('node:events')
const http = require('node:http')
const { test, describe } = require('node:test')
const { EventSource } = require('../../lib/web/eventsource/eventsource')
Expand All @@ -14,8 +14,7 @@ describe('EventSource - constructor stringify', () => {
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource({ toString: function () { return `http://localhost:${port}` } })
Expand Down
8 changes: 3 additions & 5 deletions test/eventsource/eventsource-constructor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const assert = require('node:assert')
const events = require('node:events')
const { once } = require('node:events')
const http = require('node:http')
const { test, describe } = require('node:test')
const { EventSource } = require('../../lib/web/eventsource/eventsource')
Expand All @@ -13,8 +13,7 @@ describe('EventSource - withCredentials', () => {
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`)
Expand All @@ -35,8 +34,7 @@ describe('EventSource - withCredentials', () => {
res.end()
})

server.listen(0)
await events.once(server, 'listening')
await once(server.listen(0), 'listening')
const port = server.address().port

const eventSourceInstance = new EventSource(`http://localhost:${port}`, { withCredentials: true })
Expand Down
Loading
Loading