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
52 changes: 27 additions & 25 deletions test/eventsource/eventsource-attributes.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
'use strict'

const assert = require('node:assert')
const { once } = require('node:events')
const http = require('node:http')
const { test, describe } = require('node:test')
const { test, describe, before, after } = require('node:test')
const { EventSource } = require('../../lib/web/eventsource/eventsource')

describe('EventSource - eventhandler idl', async () => {
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.writeHead(200, 'dummy')
describe('EventSource - eventhandler idl', () => {
let server
let port

before(async () => {
server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.writeHead(200, 'dummy')
})

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

await once(server.listen(0), 'listening')
const port = server.address().port
after(() => { server.close() })

let done = 0
const eventhandlerIdl = ['onmessage', 'onerror', 'onopen']

eventhandlerIdl.forEach((type) => {
test(`Should properly configure the ${type} eventhandler idl`, () => {
test(`Should properly configure the ${type} eventhandler idl`, (t) => {
const eventSourceInstance = new EventSource(`http://localhost:${port}`)

// Eventsource eventhandler idl is by default null,
assert.strictEqual(eventSourceInstance[type], null)
t.assert.strictEqual(eventSourceInstance[type], null)

// The eventhandler idl is by default not enumerable.
assert.strictEqual(Object.prototype.propertyIsEnumerable.call(eventSourceInstance, type), false)
t.assert.strictEqual(Object.prototype.propertyIsEnumerable.call(eventSourceInstance, type), false)

// The eventhandler idl ignores non-functions.
eventSourceInstance[type] = 7
assert.strictEqual(EventSource[type], undefined)
t.assert.strictEqual(EventSource[type], undefined)

// The eventhandler idl accepts functions.
function fn () {
assert.fail('Should not have called the eventhandler')
t.assert.fail('Should not have called the eventhandler')
}
eventSourceInstance[type] = fn
assert.strictEqual(eventSourceInstance[type], fn)
t.assert.strictEqual(eventSourceInstance[type], fn)

// The eventhandler idl can be set to another function.
function fn2 () { }
eventSourceInstance[type] = fn2
assert.strictEqual(eventSourceInstance[type], fn2)
t.assert.strictEqual(eventSourceInstance[type], fn2)

// The eventhandler idl overrides the previous function.
eventSourceInstance.dispatchEvent(new Event(type))

eventSourceInstance.close()
done++

if (done === eventhandlerIdl.length) server.close()
})
})
})
Expand All @@ -60,31 +62,31 @@ describe('EventSource - constants', () => {
['OPEN', 1],
['CLOSED', 2]
].forEach((config) => {
test(`Should expose the ${config[0]} constant`, () => {
test(`Should expose the ${config[0]} constant`, (t) => {
const [constant, value] = config

// EventSource exposes the constant.
assert.strictEqual(Object.hasOwn(EventSource, constant), true)
t.assert.strictEqual(Object.hasOwn(EventSource, constant), true)

// The value is properly set.
assert.strictEqual(EventSource[constant], value)
t.assert.strictEqual(EventSource[constant], value)

// The constant is enumerable.
assert.strictEqual(Object.prototype.propertyIsEnumerable.call(EventSource, constant), true)
t.assert.strictEqual(Object.prototype.propertyIsEnumerable.call(EventSource, constant), true)

// The constant is not writable.
try {
EventSource[constant] = 666
} catch (e) {
assert.strictEqual(e instanceof TypeError, true)
t.assert.strictEqual(e instanceof TypeError, true)
}
// The constant is not configurable.
try {
delete EventSource[constant]
} catch (e) {
assert.strictEqual(e instanceof TypeError, true)
t.assert.strictEqual(e instanceof TypeError, true)
}
assert.strictEqual(EventSource[constant], value)
t.assert.strictEqual(EventSource[constant], value)
})
})
})
24 changes: 11 additions & 13 deletions test/eventsource/eventsource-close.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { once } = require('node:events')
const http = require('node:http')
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 (t) => {
t = tspl(t, { plan: 1 })
t.plan(1)

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

Expand All @@ -29,35 +28,34 @@ describe('EventSource - close', () => {
}

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

await t.completed
await once(server, 'close')
})

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

after(() => server.close())
await once(server.listen(0), 'listening')
const port = server.address().port

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

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

await t.completed
await once(server, 'close')
})
})
Loading
Loading