diff --git a/test/eventsource/eventsource-attributes.js b/test/eventsource/eventsource-attributes.js index f6b9f0a2cc8..4050c023278 100644 --- a/test/eventsource/eventsource-attributes.js +++ b/test/eventsource/eventsource-attributes.js @@ -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() }) }) }) @@ -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) }) }) }) diff --git a/test/eventsource/eventsource-close.js b/test/eventsource/eventsource-close.js index 805306459be..0eb3b7fe878 100644 --- a/test/eventsource/eventsource-close.js +++ b/test/eventsource/eventsource-close.js @@ -1,6 +1,5 @@ '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') @@ -8,10 +7,10 @@ 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') @@ -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') }) }) diff --git a/test/eventsource/eventsource-connect.js b/test/eventsource/eventsource-connect.js index 3c69505e289..1b9967550b2 100644 --- a/test/eventsource/eventsource-connect.js +++ b/test/eventsource/eventsource-connect.js @@ -1,6 +1,5 @@ 'use strict' -const assert = require('node:assert') const { once } = require('node:events') const http = require('node:http') const { test, describe, after } = require('node:test') @@ -9,9 +8,9 @@ const { EventSource, defaultReconnectionTime } = require('../../lib/web/eventsou const { randomInt } = require('node:crypto') describe('EventSource - sending correct request headers', () => { - test('should send request with connection keep-alive', async () => { + test('should send request with connection keep-alive', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { - assert.strictEqual(req.headers.connection, 'keep-alive') + t.assert.strictEqual(req.headers.connection, 'keep-alive') res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) res.end() }) @@ -20,19 +19,19 @@ describe('EventSource - sending correct request headers', () => { const port = server.address().port const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { + eventSourceInstance.onopen = (t) => { eventSourceInstance.close() server.close() } - eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + eventSourceInstance.onerror = (t) => { + t.assert.fail('Should not have errored') } }) - test('should send request with sec-fetch-mode set to cors', async () => { + test('should send request with sec-fetch-mode set to cors', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { - assert.strictEqual(req.headers['sec-fetch-mode'], 'cors') + t.assert.strictEqual(req.headers['sec-fetch-mode'], 'cors') res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) res.end() }) @@ -41,20 +40,20 @@ describe('EventSource - sending correct request headers', () => { const port = server.address().port const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { + eventSourceInstance.onopen = (t) => { eventSourceInstance.close() server.close() } - eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + eventSourceInstance.onerror = (t) => { + t.assert.fail('Should not have errored') } }) - test('should send request with pragma and cache-control set to no-cache', async () => { + test('should send request with pragma and cache-control set to no-cache', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { - assert.strictEqual(req.headers['cache-control'], 'no-cache') - assert.strictEqual(req.headers.pragma, 'no-cache') + t.assert.strictEqual(req.headers['cache-control'], 'no-cache') + t.assert.strictEqual(req.headers.pragma, 'no-cache') res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) res.end() }) @@ -63,19 +62,19 @@ describe('EventSource - sending correct request headers', () => { const port = server.address().port const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { + eventSourceInstance.onopen = (t) => { eventSourceInstance.close() server.close() } - eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + eventSourceInstance.onerror = (t) => { + t.assert.fail('Should not have errored') } }) - test('should send request with accept text/event-stream', async () => { + test('should send request with accept text/event-stream', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { - assert.strictEqual(req.headers.accept, 'text/event-stream') + t.assert.strictEqual(req.headers.accept, 'text/event-stream') res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) res.end() }) @@ -84,19 +83,19 @@ describe('EventSource - sending correct request headers', () => { const port = server.address().port const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { + eventSourceInstance.onopen = (t) => { eventSourceInstance.close() server.close() } - eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + eventSourceInstance.onerror = (t) => { + t.assert.fail('Should not have errored') } }) }) describe('EventSource - received response must have content-type to be text/event-stream', () => { - test('should send request with accept text/event-stream', async () => { + test('should send request with accept text/event-stream', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) res.end() @@ -106,17 +105,17 @@ describe('EventSource - received response must have content-type to be text/even const port = server.address().port const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { + eventSourceInstance.onopen = (t) => { eventSourceInstance.close() server.close() } - eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + eventSourceInstance.onerror = (t) => { + t.assert.fail('Should not have errored') } }) - test('should send request with accept text/event-stream;', async () => { + test('should send request with accept text/event-stream;', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream;' }) res.end() @@ -126,17 +125,17 @@ describe('EventSource - received response must have content-type to be text/even const port = server.address().port const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { + eventSourceInstance.onopen = (t) => { eventSourceInstance.close() server.close() } - eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + eventSourceInstance.onerror = (t) => { + t.assert.fail('Should not have errored') } }) - test('should handle content-type text/event-stream;charset=UTF-8 properly', async () => { + test('should handle content-type text/event-stream;charset=UTF-8 properly', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream;charset=UTF-8' }) res.end() @@ -146,17 +145,17 @@ describe('EventSource - received response must have content-type to be text/even const port = server.address().port const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { + eventSourceInstance.onopen = (t) => { eventSourceInstance.close() server.close() } - eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + eventSourceInstance.onerror = (t) => { + t.assert.fail('Should not have errored') } }) - test('should throw if content-type is text/html properly', async () => { + test('should throw if content-type is text/html properly', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/html' }) res.end() @@ -166,17 +165,17 @@ describe('EventSource - received response must have content-type to be text/even const port = server.address().port const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { - assert.fail('Should not have opened') + eventSourceInstance.onopen = (t) => { + t.assert.fail('Should not have opened') } - eventSourceInstance.onerror = () => { + eventSourceInstance.onerror = (t) => { eventSourceInstance.close() server.close() } }) - test('should try to connect again if server is unreachable', async () => { + test('should try to connect again if server is unreachable', async (t) => { const clock = FakeTimers.install() after(() => clock.uninstall()) @@ -204,11 +203,11 @@ describe('EventSource - received response must have content-type to be text/even eventSourceInstance.close() - assert.strictEqual(onerrorCalls.length, 4, 'Expected 4 error events') - assert.strictEqual(end - start, 3 * reconnectionTime, `Expected reconnection to happen after ${3 * reconnectionTime}ms, but took ${end - start}ms`) + t.assert.strictEqual(onerrorCalls.length, 4, 'Expected 4 error events') + t.assert.strictEqual(end - start, 3 * reconnectionTime, `Expected reconnection to happen after ${3 * reconnectionTime}ms, but took ${end - start}ms`) }) - test('should try to connect again if server is unreachable, configure reconnectionTime', async () => { + test('should try to connect again if server is unreachable, configure reconnectionTime', async (t) => { const reconnectionTime = 1000 const clock = FakeTimers.install() after(() => clock.uninstall()) @@ -239,7 +238,7 @@ describe('EventSource - received response must have content-type to be text/even eventSourceInstance.close() - assert.strictEqual(onerrorCalls.length, 4, 'Expected 4 error events') - assert.strictEqual(end - start, 3 * reconnectionTime, `Expected reconnection to happen after ${3 * reconnectionTime}ms, but took ${end - start}ms`) + t.assert.strictEqual(onerrorCalls.length, 4, 'Expected 4 error events') + t.assert.strictEqual(end - start, 3 * reconnectionTime, `Expected reconnection to happen after ${3 * reconnectionTime}ms, but took ${end - start}ms`) }) }) diff --git a/test/eventsource/eventsource-constructor-stringify.js b/test/eventsource/eventsource-constructor-stringify.js index f4c6415445e..3061a9712d5 100644 --- a/test/eventsource/eventsource-constructor-stringify.js +++ b/test/eventsource/eventsource-constructor-stringify.js @@ -1,15 +1,14 @@ 'use strict' -const assert = require('node:assert') const { once } = require('node:events') const http = require('node:http') const { test, describe } = require('node:test') const { EventSource } = require('../../lib/web/eventsource/eventsource') describe('EventSource - constructor stringify', () => { - test('should stringify argument', async () => { + test('should stringify argument', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { - assert.strictEqual(req.headers.connection, 'keep-alive') + t.assert.strictEqual(req.headers.connection, 'keep-alive') res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) res.end() }) @@ -24,7 +23,7 @@ describe('EventSource - constructor stringify', () => { } eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + t.assert.fail('Should not have errored') } }) }) diff --git a/test/eventsource/eventsource-constructor.js b/test/eventsource/eventsource-constructor.js index d0dcc7d2023..00a33965ed6 100644 --- a/test/eventsource/eventsource-constructor.js +++ b/test/eventsource/eventsource-constructor.js @@ -1,13 +1,12 @@ 'use strict' -const assert = require('node:assert') const { once } = require('node:events') const http = require('node:http') const { test, describe } = require('node:test') const { EventSource } = require('../../lib/web/eventsource/eventsource') describe('EventSource - withCredentials', () => { - test('withCredentials should be false by default', async () => { + test('withCredentials should be false by default', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) res.end() @@ -18,17 +17,17 @@ describe('EventSource - withCredentials', () => { const eventSourceInstance = new EventSource(`http://localhost:${port}`) eventSourceInstance.onopen = () => { - assert.strictEqual(eventSourceInstance.withCredentials, false) + t.assert.strictEqual(eventSourceInstance.withCredentials, false) eventSourceInstance.close() server.close() } eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + t.assert.fail('Should not have errored') } }) - test('withCredentials can be set to true', async () => { + test('withCredentials can be set to true', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) res.end() @@ -39,13 +38,13 @@ describe('EventSource - withCredentials', () => { const eventSourceInstance = new EventSource(`http://localhost:${port}`, { withCredentials: true }) eventSourceInstance.onopen = () => { - assert.strictEqual(eventSourceInstance.withCredentials, true) + t.assert.strictEqual(eventSourceInstance.withCredentials, true) eventSourceInstance.close() server.close() } eventSourceInstance.onerror = () => { - assert.fail('Should not have errored') + t.assert.fail('Should not have errored') } }) }) diff --git a/test/eventsource/eventsource-custom-dispatcher.js b/test/eventsource/eventsource-custom-dispatcher.js index 81542656843..591626beb0a 100644 --- a/test/eventsource/eventsource-custom-dispatcher.js +++ b/test/eventsource/eventsource-custom-dispatcher.js @@ -1,71 +1,72 @@ 'use strict' const { createServer } = require('node:http') -const { once } = require('node:events') const { Agent, EventSource } = require('../..') -const { tspl } = require('@matteo.collina/tspl') const { test } = require('node:test') -test('EventSource allows setting custom dispatcher.', async (t) => { - const { completed, deepStrictEqual } = tspl(t, { plan: 1 }) +test('EventSource allows setting custom dispatcher.', (t, done) => { + t.plan(1) const server = createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) - deepStrictEqual(req.headers['x-customer-header'], 'hello world') + t.assert.deepStrictEqual(req.headers['x-customer-header'], 'hello world') res.end() - }).listen(0) + done() + }) t.after(() => { server.close() - eventSourceInstance.close() }) - await once(server, 'listening') - - class CustomHeaderAgent extends Agent { - dispatch (opts) { - opts.headers['x-customer-header'] = 'hello world' - return super.dispatch(...arguments) + server.listen(0, () => { + class CustomHeaderAgent extends Agent { + dispatch (opts) { + opts.headers['x-customer-header'] = 'hello world' + return super.dispatch(...arguments) + } } - } - const eventSourceInstance = new EventSource(`http://localhost:${server.address().port}`, { - dispatcher: new CustomHeaderAgent() + const eventSourceInstance = new EventSource(`http://localhost:${server.address().port}`, { + dispatcher: new CustomHeaderAgent() + }) + t.after(() => { + eventSourceInstance.close() + }) }) - - await completed }) -test('EventSource allows setting custom dispatcher in EventSourceDict.', async (t) => { - const { completed, deepStrictEqual } = tspl(t, { plan: 1 }) +test('EventSource allows setting custom dispatcher in EventSourceDict.', (t, done) => { + t.plan(1) const server = createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) - deepStrictEqual(req.headers['x-customer-header'], 'hello world') + t.assert.deepStrictEqual(req.headers['x-customer-header'], 'hello world') res.end() - }).listen(0) + + done() + }) t.after(() => { server.close() - eventSourceInstance.close() }) - await once(server, 'listening') - - class CustomHeaderAgent extends Agent { - dispatch (opts) { - opts.headers['x-customer-header'] = 'hello world' - return super.dispatch(...arguments) + server.listen(0, () => { + class CustomHeaderAgent extends Agent { + dispatch (opts) { + opts.headers['x-customer-header'] = 'hello world' + return super.dispatch(...arguments) + } } - } - const eventSourceInstance = new EventSource(`http://localhost:${server.address().port}`, { - node: { - dispatcher: new CustomHeaderAgent() - } + const eventSourceInstance = new EventSource(`http://localhost:${server.address().port}`, { + node: { + dispatcher: new CustomHeaderAgent() + } + }) + t.after(() => { + eventSourceInstance.close() + }) }) - - await completed }) diff --git a/test/eventsource/eventsource-message.js b/test/eventsource/eventsource-message.js index aa3273343db..f76503752c7 100644 --- a/test/eventsource/eventsource-message.js +++ b/test/eventsource/eventsource-message.js @@ -3,13 +3,12 @@ const { once } = require('node:events') const http = require('node:http') const { test, describe, after } = require('node:test') -const { tspl } = require('@matteo.collina/tspl') const { EventSource, defaultReconnectionTime } = require('../../lib/web/eventsource/eventsource') const FakeTimers = require('@sinonjs/fake-timers') describe('EventSource - message', () => { - test('Should not emit a message if only retry field was sent', async (t) => { - t = tspl(t, { plan: 2 }) + test('Should not emit a message if only retry field was sent', (t, done) => { + t.plan(2) const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) @@ -18,29 +17,30 @@ describe('EventSource - message', () => { }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const start = Date.now() - let connectionCount = 0 - const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { - if (++connectionCount === 2) { - t.ok(Date.now() - start >= 100) - t.ok(Date.now() - start < 1000) + server.listen(0, () => { + const port = server.address().port + + const start = Date.now() + let connectionCount = 0 + const eventSourceInstance = new EventSource(`http://localhost:${port}`) + eventSourceInstance.onopen = () => { + if (++connectionCount === 2) { + t.assert.ok(Date.now() - start >= 100) + t.assert.ok(Date.now() - start < 1000) + eventSourceInstance.close() + done() + } + } + eventSourceInstance.onmessage = () => { + t.assert.fail('Should not have received a message') eventSourceInstance.close() } - } - eventSourceInstance.onmessage = () => { - t.fail('Should not have received a message') - eventSourceInstance.close() - } - - await t.completed + }) }) test('Should not emit a message if no data is provided', async (t) => { - t = tspl(t, { plan: 1 }) + t.plan(1) const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) @@ -55,18 +55,16 @@ describe('EventSource - message', () => { const eventSourceInstance = new EventSource(`http://localhost:${port}`) eventSourceInstance.onmessage = () => { - t.fail('Should not have received a message') + t.assert.fail('Should not have received a message') eventSourceInstance.close() } eventSourceInstance.close() - t.ok('Should not have received a message') - - await t.completed + t.assert.ok('Should not have received a message') }) - test('Should emit a custom type message if data is provided', async (t) => { - t = tspl(t, { plan: 1 }) + test('Should emit a custom type message if data is provided', (t, done) => { + t.plan(1) const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) @@ -75,20 +73,21 @@ describe('EventSource - message', () => { }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.addEventListener('custom', () => { - t.ok(true) - eventSourceInstance.close() - }) + server.listen(0, () => { + const port = server.address().port - await t.completed + const eventSourceInstance = new EventSource(`http://localhost:${port}`) + eventSourceInstance.addEventListener('custom', () => { + t.assert.ok(true) + done() + eventSourceInstance.close() + }) + }) }) - test('Should emit a message event if data is provided', async (t) => { - t = tspl(t, { plan: 1 }) + test('Should emit a message event if data is provided', (t, done) => { + t.plan(1) const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) @@ -97,20 +96,21 @@ describe('EventSource - message', () => { }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.addEventListener('message', () => { - t.ok(true) - eventSourceInstance.close() - }) + server.listen(0, () => { + const port = server.address().port - await t.completed + const eventSourceInstance = new EventSource(`http://localhost:${port}`) + eventSourceInstance.addEventListener('message', () => { + t.assert.ok(true) + eventSourceInstance.close() + done() + }) + }) }) - test('Should emit a message event if data as a field is provided', async (t) => { - t = tspl(t, { plan: 1 }) + test('Should emit a message event if data as a field is provided', (t, done) => { + t.plan(1) const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) @@ -119,20 +119,21 @@ describe('EventSource - message', () => { }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.addEventListener('message', () => { - t.ok(true) - eventSourceInstance.close() - }) + server.listen(0, () => { + const port = server.address().port - await t.completed + const eventSourceInstance = new EventSource(`http://localhost:${port}`) + eventSourceInstance.addEventListener('message', () => { + t.assert.ok(true) + eventSourceInstance.close() + done() + }) + }) }) - test('Should emit a custom message event if data is empty', async (t) => { - t = tspl(t, { plan: 1 }) + test('Should emit a custom message event if data is empty', (t, done) => { + t.plan(1) const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) @@ -141,20 +142,21 @@ describe('EventSource - message', () => { }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.addEventListener('custom', () => { - t.ok(true) - eventSourceInstance.close() - }) + server.listen(0, () => { + const port = server.address().port - await t.completed + const eventSourceInstance = new EventSource(`http://localhost:${port}`) + eventSourceInstance.addEventListener('custom', () => { + t.assert.ok(true) + eventSourceInstance.close() + done() + }) + }) }) - test('Should emit a message event if data is empty', async (t) => { - t = tspl(t, { plan: 1 }) + test('Should emit a message event if data is empty', (t, done) => { + t.plan(1) const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) @@ -163,20 +165,21 @@ describe('EventSource - message', () => { }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.addEventListener('message', () => { - t.ok(true) - eventSourceInstance.close() - }) + server.listen(0, () => { + const port = server.address().port - await t.completed + const eventSourceInstance = new EventSource(`http://localhost:${port}`) + eventSourceInstance.addEventListener('message', () => { + t.assert.ok(true) + eventSourceInstance.close() + done() + }) + }) }) - test('Should emit a custom message event if data only as a field is provided', async (t) => { - t = tspl(t, { plan: 1 }) + test('Should emit a custom message event if data only as a field is provided', (t, done) => { + t.plan(1) const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) @@ -185,23 +188,24 @@ describe('EventSource - message', () => { }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.addEventListener('custom', () => { - t.ok(true) - eventSourceInstance.close() - }) + server.listen(0, () => { + const port = server.address().port - await t.completed + const eventSourceInstance = new EventSource(`http://localhost:${port}`) + eventSourceInstance.addEventListener('custom', () => { + t.assert.ok(true) + eventSourceInstance.close() + done() + }) + }) }) - test('Should not emit a custom type message if no data is provided', async (t) => { + test('Should not emit a custom type message if no data is provided', (t, done) => { const clock = FakeTimers.install() after(() => clock.uninstall()) - t = tspl(t, { plan: 1 }) + t.plan(1) const server = http.createServer({ joinDuplicateHeaders: true }, async (req, res) => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) @@ -211,28 +215,30 @@ describe('EventSource - message', () => { let reconnectionCount = 0 after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const eventSourceInstance = new EventSource(`http://localhost:${port}`) - eventSourceInstance.onopen = () => { - if (++reconnectionCount === 2) { - eventSourceInstance.close() - t.ok(true) + server.listen(0, async () => { + const port = server.address().port + + const eventSourceInstance = new EventSource(`http://localhost:${port}`) + eventSourceInstance.onopen = () => { + if (++reconnectionCount === 2) { + t.assert.ok(true) + eventSourceInstance.close() + done() + } } - } - eventSourceInstance.addEventListener('custom', () => { - t.fail('Should not have received a message') - eventSourceInstance.close() - }) + eventSourceInstance.addEventListener('custom', () => { + t.assert.fail('Should not have received a message') + eventSourceInstance.close() + }) - await once(eventSourceInstance, 'open') - clock.tick(defaultReconnectionTime) - await once(eventSourceInstance, 'error') + await once(eventSourceInstance, 'open') + clock.tick(defaultReconnectionTime) + await once(eventSourceInstance, 'error') - clock.tick(defaultReconnectionTime) - await once(eventSourceInstance, 'open') - clock.tick(defaultReconnectionTime) - await t.completed + clock.tick(defaultReconnectionTime) + await once(eventSourceInstance, 'open') + clock.tick(defaultReconnectionTime) + }) }) }) diff --git a/test/eventsource/eventsource-properties.js b/test/eventsource/eventsource-properties.js index 58a02a91614..deeaf5d679c 100644 --- a/test/eventsource/eventsource-properties.js +++ b/test/eventsource/eventsource-properties.js @@ -1,15 +1,14 @@ 'use strict' const { test } = require('node:test') -const assert = require('node:assert') const { EventSource } = require('../..') // assuming the test is in test/eventsource/ -test('EventSource.prototype properties are configured correctly', () => { +test('EventSource.prototype properties are configured correctly', (t) => { const props = Object.entries(Object.getOwnPropertyDescriptors(EventSource.prototype)) for (const [key, value] of props) { if (key !== 'constructor') { - assert(value.enumerable, `${key} is not enumerable`) + t.assert.ok(value.enumerable, `${key} is not enumerable`) } } }) diff --git a/test/eventsource/eventsource-reconnect.js b/test/eventsource/eventsource-reconnect.js index e254ef4e75a..08c43758e94 100644 --- a/test/eventsource/eventsource-reconnect.js +++ b/test/eventsource/eventsource-reconnect.js @@ -1,16 +1,14 @@ 'use strict' -const assert = require('node:assert') const { once } = require('node:events') const http = require('node:http') const { test, describe, after } = require('node:test') -const { tspl } = require('@matteo.collina/tspl') const FakeTimers = require('@sinonjs/fake-timers') const { EventSource, defaultReconnectionTime } = require('../../lib/web/eventsource/eventsource') describe('EventSource - reconnect', () => { - test('Should reconnect on connection closed by server', async (t) => { - t = tspl(t, { plan: 1 }) + test('Should reconnect on connection closed by server', (t, done) => { + t.plan(1) const clock = FakeTimers.install() after(() => clock.uninstall()) @@ -21,30 +19,30 @@ describe('EventSource - reconnect', () => { }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - - const eventSourceInstance = new EventSource(`http://localhost:${port}`) - let connectionCount = 0 - eventSourceInstance.onopen = () => { - if (++connectionCount === 2) { - eventSourceInstance.close() - t.ok(true) + server.listen(0, async () => { + const port = server.address().port + + const eventSourceInstance = new EventSource(`http://localhost:${port}`) + let connectionCount = 0 + eventSourceInstance.onopen = () => { + if (++connectionCount === 2) { + eventSourceInstance.close() + t.assert.ok(true) + done() + } } - } - await once(eventSourceInstance, 'open') - - clock.tick(10) - await once(eventSourceInstance, 'error') + await once(eventSourceInstance, 'open') - clock.tick(defaultReconnectionTime) + clock.tick(10) + await once(eventSourceInstance, 'error') - await t.completed + clock.tick(defaultReconnectionTime) + }) }) - test('Should reconnect on with reconnection timeout', async (t) => { - t = tspl(t, { plan: 1 }) + test('Should reconnect on with reconnection timeout', (t, done) => { + t.plan(2) const clock = FakeTimers.install() after(() => clock.uninstall()) @@ -53,33 +51,35 @@ describe('EventSource - reconnect', () => { res.end() }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const start = Date.now() - const eventSourceInstance = new EventSource(`http://localhost:${port}`) + server.listen(0, async () => { + const port = server.address().port - let connectionCount = 0 - eventSourceInstance.onopen = () => { - if (++connectionCount === 2) { - assert.ok(Date.now() - start >= defaultReconnectionTime) - eventSourceInstance.close() - t.ok(true) - } - } + const start = Date.now() + const eventSourceInstance = new EventSource(`http://localhost:${port}`) - await once(eventSourceInstance, 'open') + let connectionCount = 0 + eventSourceInstance.onopen = () => { + if (++connectionCount === 2) { + t.assert.ok(Date.now() - start >= defaultReconnectionTime) + eventSourceInstance.close() + t.assert.ok(true) - clock.tick(10) - await once(eventSourceInstance, 'error') + done() + } + } - clock.tick(defaultReconnectionTime) + await once(eventSourceInstance, 'open') + + clock.tick(10) + await once(eventSourceInstance, 'error') - await t.completed + clock.tick(defaultReconnectionTime) + }) }) - test('Should reconnect on with modified reconnection timeout', async (t) => { - t = tspl(t, { plan: 1 }) + test('Should reconnect on with modified reconnection timeout', (t, done) => { + t.plan(3) const clock = FakeTimers.install() after(() => clock.uninstall()) @@ -89,34 +89,36 @@ describe('EventSource - reconnect', () => { res.end() }) after(() => server.close()) - await once(server.listen(0), 'listening') - const port = server.address().port - const start = Date.now() - const eventSourceInstance = new EventSource(`http://localhost:${port}`) + server.listen(0, async () => { + const port = server.address().port - let connectionCount = 0 - eventSourceInstance.onopen = () => { - if (++connectionCount === 2) { - assert.ok(Date.now() - start >= 100) - assert.ok(Date.now() - start < 1000) - eventSourceInstance.close() - t.ok(true) - } - } + const start = Date.now() + const eventSourceInstance = new EventSource(`http://localhost:${port}`) - await once(eventSourceInstance, 'open') + let connectionCount = 0 + eventSourceInstance.onopen = () => { + if (++connectionCount === 2) { + t.assert.ok(Date.now() - start >= 100) + t.assert.ok(Date.now() - start < 1000) + eventSourceInstance.close() + t.assert.ok(true) - clock.tick(10) - await once(eventSourceInstance, 'error') + done() + } + } + + await once(eventSourceInstance, 'open') - clock.tick(100) + clock.tick(10) + await once(eventSourceInstance, 'error') - await t.completed + clock.tick(100) + }) }) test('Should reconnect and send lastEventId', async (t) => { - t = tspl(t, { plan: 1 }) + t.plan(1) const clock = FakeTimers.install() after(() => clock.uninstall()) @@ -126,7 +128,7 @@ describe('EventSource - reconnect', () => { res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) res.write('id: 1337\n\n') if (++requestCount === 2) { - t.strictEqual(req.headers['last-event-id'], '1337') + t.assert.strictEqual(req.headers['last-event-id'], '1337') } res.end() }) @@ -143,7 +145,5 @@ describe('EventSource - reconnect', () => { clock.tick(defaultReconnectionTime) await once(eventSourceInstance, 'open') - - await t.completed }) }) diff --git a/test/eventsource/eventsource-redirecting.js b/test/eventsource/eventsource-redirecting.js index 1f054aaa2f1..8b7cb95c6c8 100644 --- a/test/eventsource/eventsource-redirecting.js +++ b/test/eventsource/eventsource-redirecting.js @@ -1,6 +1,5 @@ 'use strict' -const assert = require('node:assert') const { once } = require('node:events') const http = require('node:http') const { test, describe } = require('node:test') @@ -8,7 +7,7 @@ const { EventSource } = require('../../lib/web/eventsource/eventsource') describe('EventSource - redirecting', () => { [301, 302, 307, 308].forEach((statusCode) => { - test(`Should redirect on ${statusCode} status code`, async () => { + test(`Should redirect on ${statusCode} status code`, async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { if (res.req.url === '/redirect') { res.writeHead(statusCode, undefined, { Location: '/target' }) @@ -25,17 +24,17 @@ describe('EventSource - redirecting', () => { const eventSourceInstance = new EventSource(`http://localhost:${port}/redirect`) eventSourceInstance.onerror = (e) => { - assert.fail('Should not have errored') + t.assert.fail('Should not have errored') } eventSourceInstance.onopen = () => { - assert.strictEqual(eventSourceInstance.url, `http://localhost:${port}/redirect`) + t.assert.strictEqual(eventSourceInstance.url, `http://localhost:${port}/redirect`) eventSourceInstance.close() server.close() } }) }) - test('Stop trying to connect when getting a 204 response', async () => { + test('Stop trying to connect when getting a 204 response', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { if (res.req.url === '/redirect') { res.writeHead(301, undefined, { Location: '/target' }) @@ -51,16 +50,16 @@ describe('EventSource - redirecting', () => { const eventSourceInstance = new EventSource(`http://localhost:${port}/redirect`) eventSourceInstance.onerror = (event) => { - assert.strictEqual(eventSourceInstance.url, `http://localhost:${port}/redirect`) - assert.strictEqual(eventSourceInstance.readyState, EventSource.CLOSED) + t.assert.strictEqual(eventSourceInstance.url, `http://localhost:${port}/redirect`) + t.assert.strictEqual(eventSourceInstance.readyState, EventSource.CLOSED) server.close() } eventSourceInstance.onopen = () => { - assert.fail('Should not have opened') + t.assert.fail('Should not have opened') } }) - test('Throw when missing a Location header', async () => { + test('Throw when missing a Location header', async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { if (res.req.url === '/redirect') { res.writeHead(301, undefined) @@ -76,13 +75,13 @@ describe('EventSource - redirecting', () => { const eventSourceInstance = new EventSource(`http://localhost:${port}/redirect`) eventSourceInstance.onerror = () => { - assert.strictEqual(eventSourceInstance.url, `http://localhost:${port}/redirect`) - assert.strictEqual(eventSourceInstance.readyState, EventSource.CLOSED) + t.assert.strictEqual(eventSourceInstance.url, `http://localhost:${port}/redirect`) + t.assert.strictEqual(eventSourceInstance.readyState, EventSource.CLOSED) server.close() } }) - test('Should set origin attribute of messages after redirecting', async () => { + test('Should set origin attribute of messages after redirecting', async (t) => { const targetServer = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { if (res.req.url === '/target') { res.writeHead(200, undefined, { 'Content-Type': 'text/event-stream' }) @@ -103,13 +102,13 @@ describe('EventSource - redirecting', () => { const eventSourceInstance = new EventSource(`http://127.0.0.1:${sourcePort}/redirect`) eventSourceInstance.onmessage = (event) => { - assert.strictEqual(event.origin, `http://127.0.0.1:${targetPort}`) + t.assert.strictEqual(event.origin, `http://127.0.0.1:${targetPort}`) eventSourceInstance.close() targetServer.close() sourceServer.close() } eventSourceInstance.onerror = (e) => { - assert.fail('Should not have errored') + t.assert.fail('Should not have errored') } }) }) diff --git a/test/eventsource/eventsource-request-status-error.js b/test/eventsource/eventsource-request-status-error.js index 12af17f7c1d..0ea0d43304a 100644 --- a/test/eventsource/eventsource-request-status-error.js +++ b/test/eventsource/eventsource-request-status-error.js @@ -1,6 +1,5 @@ 'use strict' -const assert = require('node:assert') const { once } = require('node:events') const http = require('node:http') const { test, describe } = require('node:test') @@ -8,7 +7,7 @@ const { EventSource } = require('../../lib/web/eventsource/eventsource') describe('EventSource - status error', () => { [204, 205, 210, 299, 404, 410, 503].forEach((statusCode) => { - test(`Should error on ${statusCode} status code`, async () => { + test(`Should error on ${statusCode} status code`, async (t) => { const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => { res.writeHead(statusCode, 'dummy', { 'Content-Type': 'text/event-stream' }) res.end() @@ -19,15 +18,15 @@ describe('EventSource - status error', () => { const eventSourceInstance = new EventSource(`http://localhost:${port}`) eventSourceInstance.onerror = (e) => { - assert.strictEqual(this.readyState, this.CLOSED) + t.assert.strictEqual(this.readyState, this.CLOSED) eventSourceInstance.close() server.close() } eventSourceInstance.onmessage = () => { - assert.fail('Should not have received a message') + t.assert.fail('Should not have received a message') } eventSourceInstance.onopen = () => { - assert.fail('Should not have opened') + t.assert.fail('Should not have opened') } }) }) diff --git a/test/eventsource/eventsource-stream-bom.js b/test/eventsource/eventsource-stream-bom.js index b447832a124..c37d163fec4 100644 --- a/test/eventsource/eventsource-stream-bom.js +++ b/test/eventsource/eventsource-stream-bom.js @@ -1,19 +1,18 @@ 'use strict' -const assert = require('node:assert') const { test, describe } = require('node:test') const { EventSourceStream } = require('../../lib/web/eventsource/eventsource-stream') describe('EventSourceStream - handle BOM', () => { - test('Remove BOM from the beginning of the stream. 1 byte chunks', () => { + test('Remove BOM from the beginning of the stream. 1 byte chunks', (t) => { const dataField = 'data: Hello' const content = Buffer.from(`\uFEFF${dataField}`, 'utf8') const stream = new EventSourceStream() stream.parseLine = function (line) { - assert.strictEqual(line.byteLength, dataField.length) - assert.strictEqual(line.toString(), dataField) + t.assert.strictEqual(line.byteLength, dataField.length) + t.assert.strictEqual(line.toString(), dataField) } for (let i = 0; i < content.length; i++) { @@ -21,15 +20,15 @@ describe('EventSourceStream - handle BOM', () => { } }) - test('Remove BOM from the beginning of the stream. 2 byte chunks', () => { + test('Remove BOM from the beginning of the stream. 2 byte chunks', (t) => { const dataField = 'data: Hello' const content = Buffer.from(`\uFEFF${dataField}`, 'utf8') const stream = new EventSourceStream() stream.parseLine = function (line) { - assert.strictEqual(line.byteLength, dataField.length) - assert.strictEqual(line.toString(), dataField) + t.assert.strictEqual(line.byteLength, dataField.length) + t.assert.strictEqual(line.toString(), dataField) } for (let i = 0; i < content.length; i += 2) { @@ -37,15 +36,15 @@ describe('EventSourceStream - handle BOM', () => { } }) - test('Remove BOM from the beginning of the stream. 3 byte chunks', () => { + test('Remove BOM from the beginning of the stream. 3 byte chunks', (t) => { const dataField = 'data: Hello' const content = Buffer.from(`\uFEFF${dataField}`, 'utf8') const stream = new EventSourceStream() stream.parseLine = function (line) { - assert.strictEqual(line.byteLength, dataField.length) - assert.strictEqual(line.toString(), dataField) + t.assert.strictEqual(line.byteLength, dataField.length) + t.assert.strictEqual(line.toString(), dataField) } for (let i = 0; i < content.length; i += 3) { @@ -53,15 +52,15 @@ describe('EventSourceStream - handle BOM', () => { } }) - test('Remove BOM from the beginning of the stream. 4 byte chunks', () => { + test('Remove BOM from the beginning of the stream. 4 byte chunks', (t) => { const dataField = 'data: Hello' const content = Buffer.from(`\uFEFF${dataField}`, 'utf8') const stream = new EventSourceStream() stream.parseLine = function (line) { - assert.strictEqual(line.byteLength, dataField.length) - assert.strictEqual(line.toString(), dataField) + t.assert.strictEqual(line.byteLength, dataField.length) + t.assert.strictEqual(line.toString(), dataField) } for (let i = 0; i < content.length; i += 4) { @@ -69,15 +68,15 @@ describe('EventSourceStream - handle BOM', () => { } }) - test('Not containing BOM from the beginning of the stream. 1 byte chunks', () => { + test('Not containing BOM from the beginning of the stream. 1 byte chunks', (t) => { const dataField = 'data: Hello' const content = Buffer.from(`${dataField}`, 'utf8') const stream = new EventSourceStream() stream.parseLine = function (line) { - assert.strictEqual(line.byteLength, dataField.length) - assert.strictEqual(line.toString(), dataField) + t.assert.strictEqual(line.byteLength, dataField.length) + t.assert.strictEqual(line.toString(), dataField) } for (let i = 0; i < content.length; i += 1) { @@ -85,15 +84,15 @@ describe('EventSourceStream - handle BOM', () => { } }) - test('Not containing BOM from the beginning of the stream. 2 byte chunks', () => { + test('Not containing BOM from the beginning of the stream. 2 byte chunks', (t) => { const dataField = 'data: Hello' const content = Buffer.from(`${dataField}`, 'utf8') const stream = new EventSourceStream() stream.parseLine = function (line) { - assert.strictEqual(line.byteLength, dataField.length) - assert.strictEqual(line.toString(), dataField) + t.assert.strictEqual(line.byteLength, dataField.length) + t.assert.strictEqual(line.toString(), dataField) } for (let i = 0; i < content.length; i += 2) { @@ -101,15 +100,15 @@ describe('EventSourceStream - handle BOM', () => { } }) - test('Not containing BOM from the beginning of the stream. 3 byte chunks', () => { + test('Not containing BOM from the beginning of the stream. 3 byte chunks', (t) => { const dataField = 'data: Hello' const content = Buffer.from(`${dataField}`, 'utf8') const stream = new EventSourceStream() stream.parseLine = function (line) { - assert.strictEqual(line.byteLength, dataField.length) - assert.strictEqual(line.toString(), dataField) + t.assert.strictEqual(line.byteLength, dataField.length) + t.assert.strictEqual(line.toString(), dataField) } for (let i = 0; i < content.length; i += 3) { @@ -117,15 +116,15 @@ describe('EventSourceStream - handle BOM', () => { } }) - test('Not containing BOM from the beginning of the stream. 4 byte chunks', () => { + test('Not containing BOM from the beginning of the stream. 4 byte chunks', (t) => { const dataField = 'data: Hello' const content = Buffer.from(`${dataField}`, 'utf8') const stream = new EventSourceStream() stream.parseLine = function (line) { - assert.strictEqual(line.byteLength, dataField.length) - assert.strictEqual(line.toString(), dataField) + t.assert.strictEqual(line.byteLength, dataField.length) + t.assert.strictEqual(line.toString(), dataField) } for (let i = 0; i < content.length; i += 4) { diff --git a/test/eventsource/eventsource-stream-parse-line.js b/test/eventsource/eventsource-stream-parse-line.js index 45069a2752d..cbe66bef286 100644 --- a/test/eventsource/eventsource-stream-parse-line.js +++ b/test/eventsource/eventsource-stream-parse-line.js @@ -1,6 +1,5 @@ 'use strict' -const assert = require('node:assert') const { test, describe } = require('node:test') const { EventSourceStream } = require('../../lib/web/eventsource/eventsource-stream') @@ -10,7 +9,7 @@ describe('EventSourceStream - parseLine', () => { reconnectionTime: 1000 } - test('Should push an unmodified event when line is empty', () => { + test('Should push an unmodified event when line is empty', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -21,15 +20,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 0) - assert.strictEqual(event.data, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 0) + t.assert.strictEqual(event.data, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('Should set the data field with empty string if not containing data', () => { + test('Should set the data field with empty string if not containing data', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -40,15 +39,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('data:', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 1) - assert.strictEqual(event.data, '') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 1) + t.assert.strictEqual(event.data, '') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('Should set the data field with empty string if not containing data (containing space after colon)', () => { + test('Should set the data field with empty string if not containing data (containing space after colon)', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -59,15 +58,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('data: ', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 1) - assert.strictEqual(event.data, '') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 1) + t.assert.strictEqual(event.data, '') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('Should set the data field with a string containing space if having more than one space after colon', () => { + test('Should set the data field with a string containing space if having more than one space after colon', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -78,15 +77,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('data: ', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 1) - assert.strictEqual(event.data, ' ') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 1) + t.assert.strictEqual(event.data, ' ') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('Should set value properly, even if the line contains multiple colons', () => { + test('Should set value properly, even if the line contains multiple colons', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -97,15 +96,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('data: : ', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 1) - assert.strictEqual(event.data, ': ') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 1) + t.assert.strictEqual(event.data, ': ') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('Should set the data field when containing data', () => { + test('Should set the data field when containing data', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -116,15 +115,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('data: Hello', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 1) - assert.strictEqual(event.data, 'Hello') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 1) + t.assert.strictEqual(event.data, 'Hello') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('Should ignore comments', () => { + test('Should ignore comments', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -135,15 +134,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from(':comment', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 0) - assert.strictEqual(event.data, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 0) + t.assert.strictEqual(event.data, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('Should set retry field', () => { + test('Should set retry field', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -154,15 +153,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('retry: 1000', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 1) - assert.strictEqual(event.data, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, '1000') + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 1) + t.assert.strictEqual(event.data, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, '1000') }) - test('Should set id field', () => { + test('Should set id field', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -173,15 +172,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('id: 1234', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 1) - assert.strictEqual(event.data, undefined) - assert.strictEqual(event.id, '1234') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 1) + t.assert.strictEqual(event.data, undefined) + t.assert.strictEqual(event.id, '1234') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('Should set id field', () => { + test('Should set id field', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -192,15 +191,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('event: custom', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 1) - assert.strictEqual(event.data, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, 'custom') - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 1) + t.assert.strictEqual(event.data, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, 'custom') + t.assert.strictEqual(event.retry, undefined) }) - test('Should ignore invalid field', () => { + test('Should ignore invalid field', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -211,15 +210,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from('comment: invalid', 'utf8'), event) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 0) - assert.strictEqual(event.data, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 0) + t.assert.strictEqual(event.data, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('bogus retry', () => { + test('bogus retry', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -231,15 +230,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from(line, 'utf8'), event) }) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 2) - assert.strictEqual(event.data, 'x') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, '3000') + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 2) + t.assert.strictEqual(event.data, 'x') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, '3000') }) - test('bogus id', () => { + test('bogus id', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -251,15 +250,15 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from(line, 'utf8'), event) }) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 2) - assert.strictEqual(event.data, 'x') - assert.strictEqual(event.id, '3000') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 2) + t.assert.strictEqual(event.data, 'x') + t.assert.strictEqual(event.id, '3000') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) - test('empty event', () => { + test('empty event', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -271,11 +270,11 @@ describe('EventSourceStream - parseLine', () => { stream.parseLine(Buffer.from(line, 'utf8'), event) }) - assert.strictEqual(typeof event, 'object') - assert.strictEqual(Object.keys(event).length, 1) - assert.strictEqual(event.data, 'data') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(Object.keys(event).length, 1) + t.assert.strictEqual(event.data, 'data') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.retry, undefined) }) }) diff --git a/test/eventsource/eventsource-stream-process-event.js b/test/eventsource/eventsource-stream-process-event.js index a0452ad36dc..9a53ae4f9a5 100644 --- a/test/eventsource/eventsource-stream-process-event.js +++ b/test/eventsource/eventsource-stream-process-event.js @@ -1,6 +1,5 @@ 'use strict' -const assert = require('node:assert') const { test, describe } = require('node:test') const { EventSourceStream } = require('../../lib/web/eventsource/eventsource-stream') @@ -10,7 +9,7 @@ describe('EventSourceStream - processEvent', () => { reconnectionTime: 1000 } - test('Should set the defined origin as the origin of the MessageEvent', () => { + test('Should set the defined origin as the origin of the MessageEvent', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -18,22 +17,22 @@ describe('EventSourceStream - processEvent', () => { }) stream.on('data', (event) => { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.type, 'message') - assert.strictEqual(event.options.data, null) - assert.strictEqual(event.options.lastEventId, undefined) - assert.strictEqual(event.options.origin, 'example.com') - assert.strictEqual(stream.state.reconnectionTime, 1000) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.type, 'message') + t.assert.strictEqual(event.options.data, null) + t.assert.strictEqual(event.options.lastEventId, undefined) + t.assert.strictEqual(event.options.origin, 'example.com') + t.assert.strictEqual(stream.state.reconnectionTime, 1000) }) stream.on('error', (error) => { - assert.fail(error) + t.assert.fail(error) }) stream.processEvent({}) }) - test('Should set reconnectionTime to 4000 if event contains retry field', () => { + test('Should set reconnectionTime to 4000 if event contains retry field', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -44,10 +43,10 @@ describe('EventSourceStream - processEvent', () => { retry: '4000' }) - assert.strictEqual(stream.state.reconnectionTime, 4000) + t.assert.strictEqual(stream.state.reconnectionTime, 4000) }) - test('Dispatches a MessageEvent with data', () => { + test('Dispatches a MessageEvent with data', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -55,16 +54,16 @@ describe('EventSourceStream - processEvent', () => { }) stream.on('data', (event) => { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.type, 'message') - assert.strictEqual(event.options.data, 'Hello') - assert.strictEqual(event.options.lastEventId, undefined) - assert.strictEqual(event.options.origin, 'example.com') - assert.strictEqual(stream.state.reconnectionTime, 1000) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.type, 'message') + t.assert.strictEqual(event.options.data, 'Hello') + t.assert.strictEqual(event.options.lastEventId, undefined) + t.assert.strictEqual(event.options.origin, 'example.com') + t.assert.strictEqual(stream.state.reconnectionTime, 1000) }) stream.on('error', (error) => { - assert.fail(error) + t.assert.fail(error) }) stream.processEvent({ @@ -72,7 +71,7 @@ describe('EventSourceStream - processEvent', () => { }) }) - test('Dispatches a MessageEvent with lastEventId, when event contains id field', () => { + test('Dispatches a MessageEvent with lastEventId, when event contains id field', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -80,12 +79,12 @@ describe('EventSourceStream - processEvent', () => { }) stream.on('data', (event) => { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.type, 'message') - assert.strictEqual(event.options.data, null) - assert.strictEqual(event.options.lastEventId, '1234') - assert.strictEqual(event.options.origin, 'example.com') - assert.strictEqual(stream.state.reconnectionTime, 1000) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.type, 'message') + t.assert.strictEqual(event.options.data, null) + t.assert.strictEqual(event.options.lastEventId, '1234') + t.assert.strictEqual(event.options.origin, 'example.com') + t.assert.strictEqual(stream.state.reconnectionTime, 1000) }) stream.processEvent({ @@ -93,7 +92,7 @@ describe('EventSourceStream - processEvent', () => { }) }) - test('Dispatches a MessageEvent with lastEventId, reusing the persisted', () => { + test('Dispatches a MessageEvent with lastEventId, reusing the persisted', (t) => { // lastEventId const stream = new EventSourceStream({ eventSourceSettings: { @@ -103,18 +102,18 @@ describe('EventSourceStream - processEvent', () => { }) stream.on('data', (event) => { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.type, 'message') - assert.strictEqual(event.options.data, null) - assert.strictEqual(event.options.lastEventId, '1234') - assert.strictEqual(event.options.origin, 'example.com') - assert.strictEqual(stream.state.reconnectionTime, 1000) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.type, 'message') + t.assert.strictEqual(event.options.data, null) + t.assert.strictEqual(event.options.lastEventId, '1234') + t.assert.strictEqual(event.options.origin, 'example.com') + t.assert.strictEqual(stream.state.reconnectionTime, 1000) }) stream.processEvent({}) }) - test('Dispatches a MessageEvent with type custom, when event contains type field', () => { + test('Dispatches a MessageEvent with type custom, when event contains type field', (t) => { const stream = new EventSourceStream({ eventSourceSettings: { ...defaultEventSourceSettings @@ -122,12 +121,12 @@ describe('EventSourceStream - processEvent', () => { }) stream.on('data', (event) => { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.type, 'custom') - assert.strictEqual(event.options.data, null) - assert.strictEqual(event.options.lastEventId, undefined) - assert.strictEqual(event.options.origin, 'example.com') - assert.strictEqual(stream.state.reconnectionTime, 1000) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.type, 'custom') + t.assert.strictEqual(event.options.data, null) + t.assert.strictEqual(event.options.lastEventId, undefined) + t.assert.strictEqual(event.options.origin, 'example.com') + t.assert.strictEqual(stream.state.reconnectionTime, 1000) }) stream.processEvent({ diff --git a/test/eventsource/eventsource-stream.js b/test/eventsource/eventsource-stream.js index 8a74f539d8e..7763f46ba29 100644 --- a/test/eventsource/eventsource-stream.js +++ b/test/eventsource/eventsource-stream.js @@ -1,30 +1,29 @@ 'use strict' -const assert = require('node:assert') const { test, describe } = require('node:test') const { EventSourceStream } = require('../../lib/web/eventsource/eventsource-stream') describe('EventSourceStream', () => { - test('ignore empty chunks', () => { + test('ignore empty chunks', (t) => { const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.fail() + t.assert.fail() } stream.write(Buffer.alloc(0)) }) - test('Simple event with data field.', () => { + test('Simple event with data field.', (t) => { const content = Buffer.from('data: Hello\n\n', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, 'Hello') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, 'Hello') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) } for (let i = 0; i < content.length; i++) { @@ -32,17 +31,17 @@ describe('EventSourceStream', () => { } }) - test('Should also process CR as EOL.', () => { + test('Should also process CR as EOL.', (t) => { const content = Buffer.from('data: Hello\r\r', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, 'Hello') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, 'Hello') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) } for (let i = 0; i < content.length; i++) { @@ -50,17 +49,17 @@ describe('EventSourceStream', () => { } }) - test('Should also process CRLF as EOL.', () => { + test('Should also process CRLF as EOL.', (t) => { const content = Buffer.from('data: Hello\r\n\r\n', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, 'Hello') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, 'Hello') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) } for (let i = 0; i < content.length; i++) { @@ -68,17 +67,17 @@ describe('EventSourceStream', () => { } }) - test('Should also process mixed CR and CRLF as EOL.', () => { + test('Should also process mixed CR and CRLF as EOL.', (t) => { const content = Buffer.from('data: Hello\r\r\n', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, 'Hello') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, 'Hello') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) } for (let i = 0; i < content.length; i++) { @@ -86,17 +85,17 @@ describe('EventSourceStream', () => { } }) - test('Should also process mixed LF and CRLF as EOL.', () => { + test('Should also process mixed LF and CRLF as EOL.', (t) => { const content = Buffer.from('data: Hello\n\r\n', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, 'Hello') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, 'Hello') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) } for (let i = 0; i < content.length; i++) { @@ -104,17 +103,17 @@ describe('EventSourceStream', () => { } }) - test('Should ignore comments', () => { + test('Should ignore comments', (t) => { const content = Buffer.from(':data: Hello\n\n', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) } for (let i = 0; i < content.length; i++) { @@ -122,7 +121,7 @@ describe('EventSourceStream', () => { } }) - test('Should fire two events.', () => { + test('Should fire two events.', (t) => { // @see https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation const content = Buffer.from('data\n\ndata\ndata\n\ndata:', 'utf8') const stream = new EventSourceStream() @@ -131,19 +130,19 @@ describe('EventSourceStream', () => { stream.processEvent = function (event) { switch (count) { case 0: { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, '') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, '') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) break } case 1: { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, '\n') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, '\n') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) } } count++ @@ -154,17 +153,17 @@ describe('EventSourceStream', () => { } }) - test('Should fire two identical events.', () => { + test('Should fire two identical events.', (t) => { // @see https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation const content = Buffer.from('data:test\n\ndata: test\n\n', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, 'test') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, 'test') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) } for (let i = 0; i < content.length; i++) { @@ -172,7 +171,7 @@ describe('EventSourceStream', () => { } }) - test('ignores empty comments', () => { + test('ignores empty comments', (t) => { const content = Buffer.from('data: Hello\n\n:\n\ndata: World\n\n', 'utf8') const stream = new EventSourceStream() @@ -181,23 +180,23 @@ describe('EventSourceStream', () => { stream.processEvent = function (event) { switch (count) { case 0: { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, 'Hello') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, 'Hello') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) break } case 1: { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, 'World') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, 'World') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) break } default: { - assert.fail() + t.assert.fail() } } count++ @@ -206,47 +205,47 @@ describe('EventSourceStream', () => { stream.write(content) }) - test('comment fest', () => { + test('comment fest', (t) => { const longstring = new Array(2 * 1024 + 1).join('x') const content = Buffer.from(`data:1\r:\0\n:\r\ndata:2\n:${longstring}\rdata:3\n:data:fail\r:${longstring}\ndata:4\n\n`, 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.data, '1\n2\n3\n4') - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.data, '1\n2\n3\n4') + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) } stream.write(content) }) - test('comment fest', () => { + test('comment fest', (t) => { const content = Buffer.from('data:\n\ndata\ndata\n\ndata:test\n\n', 'utf8') const stream = new EventSourceStream() let count = 0 stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) switch (count) { case 0: { - assert.strictEqual(event.data, '') + t.assert.strictEqual(event.data, '') break } case 1: { - assert.strictEqual(event.data, '\n') + t.assert.strictEqual(event.data, '\n') break } case 2: { - assert.strictEqual(event.data, 'test') + t.assert.strictEqual(event.data, 'test') break } default: { - assert.fail() + t.assert.fail() } } count++ @@ -254,44 +253,44 @@ describe('EventSourceStream', () => { stream.write(content) }) - test('newline test', () => { + test('newline test', (t) => { const content = Buffer.from('data:test\r\ndata\ndata:test\r\n\r\n', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) - assert.strictEqual(event.data, 'test\n\ntest') + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(event.data, 'test\n\ntest') } stream.write(content) }) - test('newline test', () => { + test('newline test', (t) => { const content = Buffer.from('data:test\n data\ndata\nfoobar:xxx\njustsometext\n:thisisacommentyay\ndata:test\n\n', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) - assert.strictEqual(event.data, 'test\n\ntest') + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(event.data, 'test\n\ntest') } stream.write(content) }) - test('newline test', () => { + test('newline test', (t) => { const content = Buffer.from('data:test\n data\ndata\nfoobar:xxx\njustsometext\n:thisisacommentyay\ndata:test\n\n', 'utf8') const stream = new EventSourceStream() stream.processEvent = function (event) { - assert.strictEqual(typeof event, 'object') - assert.strictEqual(event.event, undefined) - assert.strictEqual(event.id, undefined) - assert.strictEqual(event.retry, undefined) - assert.strictEqual(event.data, 'test\n\ntest') + t.assert.strictEqual(typeof event, 'object') + t.assert.strictEqual(event.event, undefined) + t.assert.strictEqual(event.id, undefined) + t.assert.strictEqual(event.retry, undefined) + t.assert.strictEqual(event.data, 'test\n\ntest') } stream.write(content) }) diff --git a/test/eventsource/eventsource.js b/test/eventsource/eventsource.js index f7b62831449..3a8ebc2d3a8 100644 --- a/test/eventsource/eventsource.js +++ b/test/eventsource/eventsource.js @@ -1,14 +1,13 @@ 'use strict' -const assert = require('node:assert') const { test, describe } = require('node:test') const { EventSource } = require('../../lib/web/eventsource/eventsource') describe('EventSource - constructor', () => { - test('Not providing url argument should throw', () => { - assert.throws(() => new EventSource(), TypeError) + test('Not providing url argument should throw', (t) => { + t.assert.throws(() => new EventSource(), TypeError) }) - test('Throw DOMException if URL is invalid', () => { - assert.throws(() => new EventSource('http:'), { message: /Invalid URL/ }) + test('Throw DOMException if URL is invalid', (t) => { + t.assert.throws(() => new EventSource('http:'), { message: /Invalid URL/ }) }) }) diff --git a/test/eventsource/util.js b/test/eventsource/util.js index fa6c854a43a..fdc4aeb691b 100644 --- a/test/eventsource/util.js +++ b/test/eventsource/util.js @@ -1,18 +1,17 @@ 'use strict' -const assert = require('node:assert') const { test } = require('node:test') const { isASCIINumber, isValidLastEventId } = require('../../lib/web/eventsource/util') -test('isValidLastEventId', () => { - assert.strictEqual(isValidLastEventId('valid'), true) - assert.strictEqual(isValidLastEventId('in\u0000valid'), false) - assert.strictEqual(isValidLastEventId('in\x00valid'), false) - assert.strictEqual(isValidLastEventId('…'), true) +test('isValidLastEventId', (t) => { + t.assert.strictEqual(isValidLastEventId('valid'), true) + t.assert.strictEqual(isValidLastEventId('in\u0000valid'), false) + t.assert.strictEqual(isValidLastEventId('in\x00valid'), false) + t.assert.strictEqual(isValidLastEventId('…'), true) }) -test('isASCIINumber', () => { - assert.strictEqual(isASCIINumber('123'), true) - assert.strictEqual(isASCIINumber(''), false) - assert.strictEqual(isASCIINumber('123a'), false) +test('isASCIINumber', (t) => { + t.assert.strictEqual(isASCIINumber('123'), true) + t.assert.strictEqual(isASCIINumber(''), false) + t.assert.strictEqual(isASCIINumber('123a'), false) })