From 606c8c4effa1dcbfef48148090ecb4ddd6c35235 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 29 Dec 2023 12:50:02 +0100 Subject: [PATCH 1/8] feat: get --- test/diagnostics-channel/get.js | 214 ++++++++++++++++---------------- 1 file changed, 108 insertions(+), 106 deletions(-) diff --git a/test/diagnostics-channel/get.js b/test/diagnostics-channel/get.js index 9d868c3d897..631def6b843 100644 --- a/test/diagnostics-channel/get.js +++ b/test/diagnostics-channel/get.js @@ -1,141 +1,143 @@ 'use strict' -const t = require('tap') +const {test, skip, after} = require('node:test') +const assert = require('node:assert') let diagnosticsChannel try { diagnosticsChannel = require('diagnostics_channel') } catch { - t.skip('missing diagnostics_channel') + skip('missing diagnostics_channel') process.exit(0) } const { Client } = require('../..') const { createServer } = require('http') -t.plan(32) +test('Diagnostics channel - get', (t) => { + const server = createServer((req, res) => { + res.setHeader('Content-Type', 'text/plain') + res.setHeader('trailer', 'foo') + res.write('hello') + res.addTrailers({ + foo: 'oof' + }) + res.end() + }) -const server = createServer((req, res) => { - res.setHeader('Content-Type', 'text/plain') - res.setHeader('trailer', 'foo') - res.write('hello') - res.addTrailers({ - foo: 'oof' + const reqHeaders = { + foo: undefined, + bar: 'bar' + } + + let _req + diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { + _req = request + assert.equal(request.origin, `http://localhost:${server.address().port}`) + assert.equal(request.completed, false) + assert.equal(request.method, 'GET') + assert.equal(request.path, '/') + assert.equal(request.headers, 'bar: bar\r\n') + request.addHeader('hello', 'world') + assert.equal(request.headers, 'bar: bar\r\nhello: world\r\n') }) - res.end() -}) -t.teardown(server.close.bind(server)) -const reqHeaders = { - foo: undefined, - bar: 'bar' -} + let _connector + diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { + _connector = connector -let _req -diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { - _req = request - t.equal(request.origin, `http://localhost:${server.address().port}`) - t.equal(request.completed, false) - t.equal(request.method, 'GET') - t.equal(request.path, '/') - t.equal(request.headers, 'bar: bar\r\n') - request.addHeader('hello', 'world') - t.equal(request.headers, 'bar: bar\r\nhello: world\r\n') -}) + assert.equal(typeof _connector, 'function') + assert.equal(Object.keys(connectParams).length, 6) -let _connector -diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { - _connector = connector + const { host, hostname, protocol, port, servername } = connectParams - t.equal(typeof _connector, 'function') - t.equal(Object.keys(connectParams).length, 6) + assert.equal(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) - const { host, hostname, protocol, port, servername } = connectParams + let _socket + diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams, socket, connector }) => { + _socket = socket - t.equal(host, `localhost:${server.address().port}`) - t.equal(hostname, 'localhost') - t.equal(port, String(server.address().port)) - t.equal(protocol, 'http:') - t.equal(servername, null) -}) + assert.equal(_connector, connector) + assert.equal(Object.keys(connectParams).length, 6) -let _socket -diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams, socket, connector }) => { - _socket = socket + const { host, hostname, protocol, port, servername } = connectParams - t.equal(_connector, connector) - t.equal(Object.keys(connectParams).length, 6) + assert.equal(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) - const { host, hostname, protocol, port, servername } = connectParams + diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { + assert.equal(_req, request) + assert.equal(_socket, socket) - t.equal(host, `localhost:${server.address().port}`) - t.equal(hostname, 'localhost') - t.equal(port, String(server.address().port)) - t.equal(protocol, 'http:') - t.equal(servername, null) -}) + const expectedHeaders = [ + 'GET / HTTP/1.1', + `host: localhost:${server.address().port}`, + 'connection: keep-alive', + 'bar: bar', + 'hello: world' + ] -diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { - t.equal(_req, request) - t.equal(_socket, socket) + assert.equal(headers, expectedHeaders.join('\r\n') + '\r\n') + }) - const expectedHeaders = [ - 'GET / HTTP/1.1', - `host: localhost:${server.address().port}`, - 'connection: keep-alive', - 'bar: bar', - 'hello: world' - ] + diagnosticsChannel.channel('undici:request:headers').subscribe(({ request, response }) => { + assert.equal(_req, request) + assert.equal(response.statusCode, 200) + const expectedHeaders = [ + Buffer.from('Content-Type'), + Buffer.from('text/plain'), + Buffer.from('trailer'), + Buffer.from('foo'), + Buffer.from('Date'), + response.headers[5], // This is a date + Buffer.from('Connection'), + Buffer.from('keep-alive'), + Buffer.from('Keep-Alive'), + Buffer.from('timeout=5'), + Buffer.from('Transfer-Encoding'), + Buffer.from('chunked') + ] + assert.deepStrictEqual(response.headers, expectedHeaders) + assert.equal(response.statusText, 'OK') + }) - t.equal(headers, expectedHeaders.join('\r\n') + '\r\n') -}) + let endEmitted = false + diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { + assert.equal(request.completed, true) + assert.equal(_req, request) + // This event is emitted after the last chunk has been added to the body stream, + // not when it was consumed by the application + assert.equal(endEmitted, false) + assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) + }) -diagnosticsChannel.channel('undici:request:headers').subscribe(({ request, response }) => { - t.equal(_req, request) - t.equal(response.statusCode, 200) - const expectedHeaders = [ - Buffer.from('Content-Type'), - Buffer.from('text/plain'), - Buffer.from('trailer'), - Buffer.from('foo'), - Buffer.from('Date'), - response.headers[5], // This is a date - Buffer.from('Connection'), - Buffer.from('keep-alive'), - Buffer.from('Keep-Alive'), - Buffer.from('timeout=5'), - Buffer.from('Transfer-Encoding'), - Buffer.from('chunked') - ] - t.same(response.headers, expectedHeaders) - t.equal(response.statusText, 'OK') -}) + after(server.close.bind(server)) -let endEmitted = false -diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { - t.equal(request.completed, true) - t.equal(_req, request) - // This event is emitted after the last chunk has been added to the body stream, - // not when it was consumed by the application - t.equal(endEmitted, false) - t.same(trailers, [Buffer.from('foo'), Buffer.from('oof')]) -}) + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) -server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { - keepAliveTimeout: 300e3 - }) - t.teardown(client.close.bind(client)) - - client.request({ - path: '/', - method: 'GET', - headers: reqHeaders - }, (err, data) => { - t.error(err) - data.body.on('end', function () { - endEmitted = true + client.request({ + path: '/', + method: 'GET', + headers: reqHeaders + }, (err, data) => { + assert.ok(!err) + client.close() + data.body.on('end', function () { + endEmitted = true + }) }) }) }) From 6483ca57174b2a51aedfcd65bb2c10a78c911a09 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 29 Dec 2023 12:51:59 +0100 Subject: [PATCH 2/8] feat: connect error --- test/diagnostics-channel/connect-error.js | 75 ++++++++++++----------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/test/diagnostics-channel/connect-error.js b/test/diagnostics-channel/connect-error.js index f7e842dbe28..b1c0a6746d9 100644 --- a/test/diagnostics-channel/connect-error.js +++ b/test/diagnostics-channel/connect-error.js @@ -1,61 +1,62 @@ 'use strict' -const t = require('tap') +const {test, skip} = require('node:test') +const assert = require('node:assert') let diagnosticsChannel try { diagnosticsChannel = require('diagnostics_channel') } catch { - t.skip('missing diagnostics_channel') + skip('missing diagnostics_channel') process.exit(0) } const { Client } = require('../..') -t.plan(16) +test('Diagnostics channel - connect error', () => { + const connectError = new Error('custom error') -const connectError = new Error('custom error') + let _connector + diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { + _connector = connector -let _connector -diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { - _connector = connector + assert.equal(typeof _connector, 'function') + assert.equal(Object.keys(connectParams).length, 6) - t.equal(typeof _connector, 'function') - t.equal(Object.keys(connectParams).length, 6) + const { host, hostname, protocol, port, servername } = connectParams - const { host, hostname, protocol, port, servername } = connectParams + assert.equal(host, 'localhost:1234') + assert.equal(hostname, 'localhost') + assert.equal(port, '1234') + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) - t.equal(host, 'localhost:1234') - t.equal(hostname, 'localhost') - t.equal(port, '1234') - t.equal(protocol, 'http:') - t.equal(servername, null) -}) - -diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => { - t.equal(Object.keys(connectParams).length, 6) - t.equal(_connector, connector) + diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => { + assert.equal(Object.keys(connectParams).length, 6) + assert.equal(_connector, connector) - const { host, hostname, protocol, port, servername } = connectParams + const { host, hostname, protocol, port, servername } = connectParams - t.equal(error, connectError) - t.equal(host, 'localhost:1234') - t.equal(hostname, 'localhost') - t.equal(port, '1234') - t.equal(protocol, 'http:') - t.equal(servername, null) -}) + assert.equal(error, connectError) + assert.equal(host, 'localhost:1234') + assert.equal(hostname, 'localhost') + assert.equal(port, '1234') + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) -const client = new Client('http://localhost:1234', { - connect: (_, cb) => { cb(connectError, null) } -}) + const client = new Client('http://localhost:1234', { + connect: (_, cb) => { cb(connectError, null) } + }) -t.teardown(client.close.bind(client)) + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + assert.equal(err, connectError) + client.close() + }) -client.request({ - path: '/', - method: 'GET' -}, (err, data) => { - t.equal(err, connectError) }) From e27bb14e37722eb3e8d3e83fa3c0f9f62dea8df0 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 29 Dec 2023 12:54:09 +0100 Subject: [PATCH 3/8] feat: error --- test/diagnostics-channel/error.js | 65 ++++++++++++++++--------------- test/diagnostics-channel/get.js | 4 +- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/test/diagnostics-channel/error.js b/test/diagnostics-channel/error.js index 1f350b1dd49..8d3db7370ff 100644 --- a/test/diagnostics-channel/error.js +++ b/test/diagnostics-channel/error.js @@ -1,52 +1,53 @@ 'use strict' -const t = require('tap') +const {test, skip, after} = require('node:test') +const assert = require('node:assert') let diagnosticsChannel try { diagnosticsChannel = require('diagnostics_channel') } catch { - t.skip('missing diagnostics_channel') + skip('missing diagnostics_channel') process.exit(0) } const { Client } = require('../..') const { createServer } = require('http') -t.plan(3) +test('Diagnostics channel - error', () => { + const server = createServer((req, res) => { + res.destroy() + }) + after(server.close.bind(server)) -const server = createServer((req, res) => { - res.destroy() -}) -t.teardown(server.close.bind(server)) + const reqHeaders = { + foo: undefined, + bar: 'bar' + } -const reqHeaders = { - foo: undefined, - bar: 'bar' -} - -let _req -diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { - _req = request -}) - -diagnosticsChannel.channel('undici:request:error').subscribe(({ request, error }) => { - t.equal(_req, request) - t.equal(error.code, 'UND_ERR_SOCKET') -}) + let _req + diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { + _req = request + }) -server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { - keepAliveTimeout: 300e3 + diagnosticsChannel.channel('undici:request:error').subscribe(({ request, error }) => { + assert.equal(_req, request) + assert.equal(error.code, 'UND_ERR_SOCKET') }) - t.teardown(client.close.bind(client)) - - client.request({ - path: '/', - method: 'GET', - headers: reqHeaders - }, (err, data) => { - t.equal(err.code, 'UND_ERR_SOCKET') + + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) + + client.request({ + path: '/', + method: 'GET', + headers: reqHeaders + }, (err, data) => { + assert.equal(err.code, 'UND_ERR_SOCKET') + client.close() + }) }) }) diff --git a/test/diagnostics-channel/get.js b/test/diagnostics-channel/get.js index 631def6b843..71141271529 100644 --- a/test/diagnostics-channel/get.js +++ b/test/diagnostics-channel/get.js @@ -26,6 +26,8 @@ test('Diagnostics channel - get', (t) => { res.end() }) + after(server.close.bind(server)) + const reqHeaders = { foo: undefined, bar: 'bar' @@ -121,8 +123,6 @@ test('Diagnostics channel - get', (t) => { assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) }) - after(server.close.bind(server)) - server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`, { keepAliveTimeout: 300e3 From b56838b931047638a8a1ea20eec68707f5385788 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 29 Dec 2023 12:56:17 +0100 Subject: [PATCH 4/8] fix: post stream --- test/diagnostics-channel/post-stream.js | 229 ++++++++++++------------ 1 file changed, 115 insertions(+), 114 deletions(-) diff --git a/test/diagnostics-channel/post-stream.js b/test/diagnostics-channel/post-stream.js index 236b9bba131..69f13a5e799 100644 --- a/test/diagnostics-channel/post-stream.js +++ b/test/diagnostics-channel/post-stream.js @@ -1,6 +1,7 @@ 'use strict' -const t = require('tap') +const {test, skip, after} = require('node:test') +const assert = require('node:assert') const { Readable } = require('stream') let diagnosticsChannel @@ -8,142 +9,142 @@ let diagnosticsChannel try { diagnosticsChannel = require('diagnostics_channel') } catch { - t.skip('missing diagnostics_channel') + skip('missing diagnostics_channel') process.exit(0) } const { Client } = require('../..') const { createServer } = require('http') -t.plan(33) - -const server = createServer((req, res) => { - req.resume() - res.setHeader('Content-Type', 'text/plain') - res.setHeader('trailer', 'foo') - res.write('hello') - res.addTrailers({ - foo: 'oof' +test('Diagnostics channel - post stream', (t) => { + const server = createServer((req, res) => { + req.resume() + res.setHeader('Content-Type', 'text/plain') + res.setHeader('trailer', 'foo') + res.write('hello') + res.addTrailers({ + foo: 'oof' + }) + res.end() + }) + after(server.close.bind(server)) + + const reqHeaders = { + foo: undefined, + bar: 'bar' + } + const body = Readable.from(['hello', ' ', 'world']) + + let _req + diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { + _req = request + assert.equal(request.completed, false) + assert.equal(request.method, 'POST') + assert.equal(request.path, '/') + assert.equal(request.headers, 'bar: bar\r\n') + request.addHeader('hello', 'world') + assert.equal(request.headers, 'bar: bar\r\nhello: world\r\n') + assert.deepStrictEqual(request.body, body) }) - res.end() -}) -t.teardown(server.close.bind(server)) -const reqHeaders = { - foo: undefined, - bar: 'bar' -} -const body = Readable.from(['hello', ' ', 'world']) - -let _req -diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { - _req = request - t.equal(request.completed, false) - t.equal(request.method, 'POST') - t.equal(request.path, '/') - t.equal(request.headers, 'bar: bar\r\n') - request.addHeader('hello', 'world') - t.equal(request.headers, 'bar: bar\r\nhello: world\r\n') - t.same(request.body, body) -}) + let _connector + diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { + _connector = connector -let _connector -diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { - _connector = connector + assert.equal(typeof _connector, 'function') + assert.equal(Object.keys(connectParams).length, 6) - t.equal(typeof _connector, 'function') - t.equal(Object.keys(connectParams).length, 6) + const { host, hostname, protocol, port, servername } = connectParams - const { host, hostname, protocol, port, servername } = connectParams + assert.equal(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) - t.equal(host, `localhost:${server.address().port}`) - t.equal(hostname, 'localhost') - t.equal(port, String(server.address().port)) - t.equal(protocol, 'http:') - t.equal(servername, null) -}) + let _socket + diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams, socket, connector }) => { + _socket = socket -let _socket -diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams, socket, connector }) => { - _socket = socket + assert.equal(Object.keys(connectParams).length, 6) + assert.equal(_connector, connector) - t.equal(Object.keys(connectParams).length, 6) - t.equal(_connector, connector) + const { host, hostname, protocol, port, servername } = connectParams - const { host, hostname, protocol, port, servername } = connectParams + assert.equal(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) - t.equal(host, `localhost:${server.address().port}`) - t.equal(hostname, 'localhost') - t.equal(port, String(server.address().port)) - t.equal(protocol, 'http:') - t.equal(servername, null) -}) + diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { + assert.equal(_req, request) + assert.equal(_socket, socket) -diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { - t.equal(_req, request) - t.equal(_socket, socket) + const expectedHeaders = [ + 'POST / HTTP/1.1', + `host: localhost:${server.address().port}`, + 'connection: keep-alive', + 'bar: bar', + 'hello: world' + ] - const expectedHeaders = [ - 'POST / HTTP/1.1', - `host: localhost:${server.address().port}`, - 'connection: keep-alive', - 'bar: bar', - 'hello: world' - ] + assert.equal(headers, expectedHeaders.join('\r\n') + '\r\n') + }) - t.equal(headers, expectedHeaders.join('\r\n') + '\r\n') -}) + diagnosticsChannel.channel('undici:request:headers').subscribe(({ request, response }) => { + assert.equal(_req, request) + assert.equal(response.statusCode, 200) + const expectedHeaders = [ + Buffer.from('Content-Type'), + Buffer.from('text/plain'), + Buffer.from('trailer'), + Buffer.from('foo'), + Buffer.from('Date'), + response.headers[5], // This is a date + Buffer.from('Connection'), + Buffer.from('keep-alive'), + Buffer.from('Keep-Alive'), + Buffer.from('timeout=5'), + Buffer.from('Transfer-Encoding'), + Buffer.from('chunked') + ] + assert.deepStrictEqual(response.headers, expectedHeaders) + assert.equal(response.statusText, 'OK') + }) -diagnosticsChannel.channel('undici:request:headers').subscribe(({ request, response }) => { - t.equal(_req, request) - t.equal(response.statusCode, 200) - const expectedHeaders = [ - Buffer.from('Content-Type'), - Buffer.from('text/plain'), - Buffer.from('trailer'), - Buffer.from('foo'), - Buffer.from('Date'), - response.headers[5], // This is a date - Buffer.from('Connection'), - Buffer.from('keep-alive'), - Buffer.from('Keep-Alive'), - Buffer.from('timeout=5'), - Buffer.from('Transfer-Encoding'), - Buffer.from('chunked') - ] - t.same(response.headers, expectedHeaders) - t.equal(response.statusText, 'OK') -}) + diagnosticsChannel.channel('undici:request:bodySent').subscribe(({ request }) => { + assert.equal(_req, request) + }) -diagnosticsChannel.channel('undici:request:bodySent').subscribe(({ request }) => { - t.equal(_req, request) -}) + let endEmitted = false + diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { + assert.equal(request.completed, true) + assert.equal(_req, request) + // This event is emitted after the last chunk has been added to the body stream, + // not when it was consumed by the application + assert.equal(endEmitted, false) + assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) + }) -let endEmitted = false -diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { - t.equal(request.completed, true) - t.equal(_req, request) - // This event is emitted after the last chunk has been added to the body stream, - // not when it was consumed by the application - t.equal(endEmitted, false) - t.same(trailers, [Buffer.from('foo'), Buffer.from('oof')]) -}) + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) -server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { - keepAliveTimeout: 300e3 - }) - t.teardown(client.close.bind(client)) - - client.request({ - path: '/', - method: 'POST', - headers: reqHeaders, - body - }, (err, data) => { - t.error(err) - data.body.on('end', function () { - endEmitted = true + client.request({ + path: '/', + method: 'POST', + headers: reqHeaders, + body + }, (err, data) => { + assert.ok(!err) + client.close() + data.body.on('end', function () { + endEmitted = true + }) }) }) }) From a256cd2c3c28a4f31b8460034f784fe2cde40794 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 29 Dec 2023 12:58:24 +0100 Subject: [PATCH 5/8] fix: post --- test/diagnostics-channel/post.js | 227 ++++++++++++++++--------------- 1 file changed, 114 insertions(+), 113 deletions(-) diff --git a/test/diagnostics-channel/post.js b/test/diagnostics-channel/post.js index fc02eb55d8c..579af40443a 100644 --- a/test/diagnostics-channel/post.js +++ b/test/diagnostics-channel/post.js @@ -1,147 +1,148 @@ 'use strict' -const t = require('tap') +const { test, skip, after } = require('node:test') +const assert = require('node:assert') let diagnosticsChannel try { diagnosticsChannel = require('diagnostics_channel') } catch { - t.skip('missing diagnostics_channel') + skip('missing diagnostics_channel') process.exit(0) } const { Client } = require('../..') const { createServer } = require('http') -t.plan(33) - -const server = createServer((req, res) => { - req.resume() - res.setHeader('Content-Type', 'text/plain') - res.setHeader('trailer', 'foo') - res.write('hello') - res.addTrailers({ - foo: 'oof' +test('Diagnostics channel - post', (t) => { + const server = createServer((req, res) => { + req.resume() + res.setHeader('Content-Type', 'text/plain') + res.setHeader('trailer', 'foo') + res.write('hello') + res.addTrailers({ + foo: 'oof' + }) + res.end() + }) + after(server.close.bind(server)) + + const reqHeaders = { + foo: undefined, + bar: 'bar' + } + + let _req + diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { + _req = request + assert.equal(request.completed, false) + assert.equal(request.method, 'POST') + assert.equal(request.path, '/') + assert.equal(request.headers, 'bar: bar\r\n') + request.addHeader('hello', 'world') + assert.equal(request.headers, 'bar: bar\r\nhello: world\r\n') + assert.deepStrictEqual(request.body, Buffer.from('hello world')) }) - res.end() -}) -t.teardown(server.close.bind(server)) -const reqHeaders = { - foo: undefined, - bar: 'bar' -} + let _connector + diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { + _connector = connector -let _req -diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { - _req = request - t.equal(request.completed, false) - t.equal(request.method, 'POST') - t.equal(request.path, '/') - t.equal(request.headers, 'bar: bar\r\n') - request.addHeader('hello', 'world') - t.equal(request.headers, 'bar: bar\r\nhello: world\r\n') - t.same(request.body, Buffer.from('hello world')) -}) + assert.equal(typeof _connector, 'function') + assert.equal(Object.keys(connectParams).length, 6) -let _connector -diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { - _connector = connector + const { host, hostname, protocol, port, servername } = connectParams - t.equal(typeof _connector, 'function') - t.equal(Object.keys(connectParams).length, 6) - - const { host, hostname, protocol, port, servername } = connectParams + assert.equal(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) - t.equal(host, `localhost:${server.address().port}`) - t.equal(hostname, 'localhost') - t.equal(port, String(server.address().port)) - t.equal(protocol, 'http:') - t.equal(servername, null) -}) + let _socket + diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams, socket, connector }) => { + _socket = socket -let _socket -diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams, socket, connector }) => { - _socket = socket + assert.equal(Object.keys(connectParams).length, 6) + assert.equal(_connector, connector) - t.equal(Object.keys(connectParams).length, 6) - t.equal(_connector, connector) + const { host, hostname, protocol, port, servername } = connectParams - const { host, hostname, protocol, port, servername } = connectParams + assert.equal(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) - t.equal(host, `localhost:${server.address().port}`) - t.equal(hostname, 'localhost') - t.equal(port, String(server.address().port)) - t.equal(protocol, 'http:') - t.equal(servername, null) -}) + diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { + assert.equal(_req, request) + assert.equal(_socket, socket) -diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { - t.equal(_req, request) - t.equal(_socket, socket) + const expectedHeaders = [ + 'POST / HTTP/1.1', + `host: localhost:${server.address().port}`, + 'connection: keep-alive', + 'bar: bar', + 'hello: world' + ] - const expectedHeaders = [ - 'POST / HTTP/1.1', - `host: localhost:${server.address().port}`, - 'connection: keep-alive', - 'bar: bar', - 'hello: world' - ] + assert.equal(headers, expectedHeaders.join('\r\n') + '\r\n') + }) - t.equal(headers, expectedHeaders.join('\r\n') + '\r\n') -}) + diagnosticsChannel.channel('undici:request:headers').subscribe(({ request, response }) => { + assert.equal(_req, request) + assert.equal(response.statusCode, 200) + const expectedHeaders = [ + Buffer.from('Content-Type'), + Buffer.from('text/plain'), + Buffer.from('trailer'), + Buffer.from('foo'), + Buffer.from('Date'), + response.headers[5], // This is a date + Buffer.from('Connection'), + Buffer.from('keep-alive'), + Buffer.from('Keep-Alive'), + Buffer.from('timeout=5'), + Buffer.from('Transfer-Encoding'), + Buffer.from('chunked') + ] + assert.deepStrictEqual(response.headers, expectedHeaders) + assert.equal(response.statusText, 'OK') + }) -diagnosticsChannel.channel('undici:request:headers').subscribe(({ request, response }) => { - t.equal(_req, request) - t.equal(response.statusCode, 200) - const expectedHeaders = [ - Buffer.from('Content-Type'), - Buffer.from('text/plain'), - Buffer.from('trailer'), - Buffer.from('foo'), - Buffer.from('Date'), - response.headers[5], // This is a date - Buffer.from('Connection'), - Buffer.from('keep-alive'), - Buffer.from('Keep-Alive'), - Buffer.from('timeout=5'), - Buffer.from('Transfer-Encoding'), - Buffer.from('chunked') - ] - t.same(response.headers, expectedHeaders) - t.equal(response.statusText, 'OK') -}) + diagnosticsChannel.channel('undici:request:bodySent').subscribe(({ request }) => { + assert.equal(_req, request) + }) -diagnosticsChannel.channel('undici:request:bodySent').subscribe(({ request }) => { - t.equal(_req, request) -}) + let endEmitted = false + diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { + assert.equal(request.completed, true) + assert.equal(_req, request) + // This event is emitted after the last chunk has been added to the body stream, + // not when it was consumed by the application + assert.equal(endEmitted, false) + assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) + }) -let endEmitted = false -diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { - t.equal(request.completed, true) - t.equal(_req, request) - // This event is emitted after the last chunk has been added to the body stream, - // not when it was consumed by the application - t.equal(endEmitted, false) - t.same(trailers, [Buffer.from('foo'), Buffer.from('oof')]) -}) + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) -server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { - keepAliveTimeout: 300e3 - }) - t.teardown(client.close.bind(client)) - - client.request({ - path: '/', - method: 'POST', - headers: reqHeaders, - body: 'hello world' - }, (err, data) => { - t.error(err) - data.body.on('end', function () { - endEmitted = true + client.request({ + path: '/', + method: 'POST', + headers: reqHeaders, + body: 'hello world' + }, (err, data) => { + assert.ok(!err) + client.close() + data.body.on('end', function () { + endEmitted = true + }) }) }) }) From a857f1d20b2abe4dc09291bba071a1cc2b6082c5 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 29 Dec 2023 12:58:56 +0100 Subject: [PATCH 6/8] fix: lint --- test/diagnostics-channel/connect-error.js | 3 +-- test/diagnostics-channel/error.js | 2 +- test/diagnostics-channel/get.js | 2 +- test/diagnostics-channel/post-stream.js | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/test/diagnostics-channel/connect-error.js b/test/diagnostics-channel/connect-error.js index b1c0a6746d9..76f4fac6bf4 100644 --- a/test/diagnostics-channel/connect-error.js +++ b/test/diagnostics-channel/connect-error.js @@ -1,6 +1,6 @@ 'use strict' -const {test, skip} = require('node:test') +const { test, skip } = require('node:test') const assert = require('node:assert') let diagnosticsChannel @@ -58,5 +58,4 @@ test('Diagnostics channel - connect error', () => { assert.equal(err, connectError) client.close() }) - }) diff --git a/test/diagnostics-channel/error.js b/test/diagnostics-channel/error.js index 8d3db7370ff..e9962fb83b5 100644 --- a/test/diagnostics-channel/error.js +++ b/test/diagnostics-channel/error.js @@ -1,6 +1,6 @@ 'use strict' -const {test, skip, after} = require('node:test') +const { test, skip, after } = require('node:test') const assert = require('node:assert') let diagnosticsChannel diff --git a/test/diagnostics-channel/get.js b/test/diagnostics-channel/get.js index 71141271529..c5c05b3ef3f 100644 --- a/test/diagnostics-channel/get.js +++ b/test/diagnostics-channel/get.js @@ -1,6 +1,6 @@ 'use strict' -const {test, skip, after} = require('node:test') +const { test, skip, after } = require('node:test') const assert = require('node:assert') let diagnosticsChannel diff --git a/test/diagnostics-channel/post-stream.js b/test/diagnostics-channel/post-stream.js index 69f13a5e799..cc7e3e4d3d4 100644 --- a/test/diagnostics-channel/post-stream.js +++ b/test/diagnostics-channel/post-stream.js @@ -1,6 +1,6 @@ 'use strict' -const {test, skip, after} = require('node:test') +const { test, skip, after } = require('node:test') const assert = require('node:assert') const { Readable } = require('stream') From 7c665befc48f22eb0ce3caa1f9375ab343bdb272 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 29 Dec 2023 14:21:38 +0100 Subject: [PATCH 7/8] fix: test execution assertion --- test/diagnostics-channel/connect-error.js | 15 +++++--- test/diagnostics-channel/error.js | 27 +++++++------ test/diagnostics-channel/get.js | 45 ++++++++++++---------- test/diagnostics-channel/post-stream.js | 46 ++++++++++++---------- test/diagnostics-channel/post.js | 47 +++++++++++++---------- 5 files changed, 100 insertions(+), 80 deletions(-) diff --git a/test/diagnostics-channel/connect-error.js b/test/diagnostics-channel/connect-error.js index 76f4fac6bf4..5c64c19b254 100644 --- a/test/diagnostics-channel/connect-error.js +++ b/test/diagnostics-channel/connect-error.js @@ -51,11 +51,14 @@ test('Diagnostics channel - connect error', () => { connect: (_, cb) => { cb(connectError, null) } }) - client.request({ - path: '/', - method: 'GET' - }, (err, data) => { - assert.equal(err, connectError) - client.close() + return new Promise((resolve) => { + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + assert.equal(err, connectError) + client.close() + resolve() + }) }) }) diff --git a/test/diagnostics-channel/error.js b/test/diagnostics-channel/error.js index e9962fb83b5..789ac33e702 100644 --- a/test/diagnostics-channel/error.js +++ b/test/diagnostics-channel/error.js @@ -36,18 +36,21 @@ test('Diagnostics channel - error', () => { assert.equal(error.code, 'UND_ERR_SOCKET') }) - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { - keepAliveTimeout: 300e3 - }) - - client.request({ - path: '/', - method: 'GET', - headers: reqHeaders - }, (err, data) => { - assert.equal(err.code, 'UND_ERR_SOCKET') - client.close() + return new Promise((resolve) => { + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) + + client.request({ + path: '/', + method: 'GET', + headers: reqHeaders + }, (err, data) => { + assert.equal(err.code, 'UND_ERR_SOCKET') + client.close() + resolve() + }) }) }) }) diff --git a/test/diagnostics-channel/get.js b/test/diagnostics-channel/get.js index c5c05b3ef3f..fe40335de23 100644 --- a/test/diagnostics-channel/get.js +++ b/test/diagnostics-channel/get.js @@ -114,29 +114,34 @@ test('Diagnostics channel - get', (t) => { }) let endEmitted = false - diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { - assert.equal(request.completed, true) - assert.equal(_req, request) - // This event is emitted after the last chunk has been added to the body stream, - // not when it was consumed by the application - assert.equal(endEmitted, false) - assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) - }) - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { - keepAliveTimeout: 300e3 + return new Promise((resolve) => { + diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { + assert.equal(request.completed, true) + assert.equal(_req, request) + // This event is emitted after the last chunk has been added to the body stream, + // not when it was consumed by the application + assert.equal(endEmitted, false) + assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) + resolve() }) - client.request({ - path: '/', - method: 'GET', - headers: reqHeaders - }, (err, data) => { - assert.ok(!err) - client.close() - data.body.on('end', function () { - endEmitted = true + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) + + client.request({ + path: '/', + method: 'GET', + headers: reqHeaders + }, (err, data) => { + assert.ok(!err) + client.close() + + data.body.on('end', function () { + endEmitted = true + }) }) }) }) diff --git a/test/diagnostics-channel/post-stream.js b/test/diagnostics-channel/post-stream.js index cc7e3e4d3d4..edcc90f6abd 100644 --- a/test/diagnostics-channel/post-stream.js +++ b/test/diagnostics-channel/post-stream.js @@ -120,30 +120,34 @@ test('Diagnostics channel - post stream', (t) => { }) let endEmitted = false - diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { - assert.equal(request.completed, true) - assert.equal(_req, request) - // This event is emitted after the last chunk has been added to the body stream, - // not when it was consumed by the application - assert.equal(endEmitted, false) - assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) - }) - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { - keepAliveTimeout: 300e3 + return new Promise((resolve) => { + diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { + assert.equal(request.completed, true) + assert.equal(_req, request) + // This event is emitted after the last chunk has been added to the body stream, + // not when it was consumed by the application + assert.equal(endEmitted, false) + assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) + resolve() }) - client.request({ - path: '/', - method: 'POST', - headers: reqHeaders, - body - }, (err, data) => { - assert.ok(!err) - client.close() - data.body.on('end', function () { - endEmitted = true + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) + + client.request({ + path: '/', + method: 'POST', + headers: reqHeaders, + body + }, (err, data) => { + assert.ok(!err) + client.close() + data.body.on('end', function () { + endEmitted = true + }) }) }) }) diff --git a/test/diagnostics-channel/post.js b/test/diagnostics-channel/post.js index 579af40443a..2fcb00c93fa 100644 --- a/test/diagnostics-channel/post.js +++ b/test/diagnostics-channel/post.js @@ -118,30 +118,35 @@ test('Diagnostics channel - post', (t) => { }) let endEmitted = false - diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { - assert.equal(request.completed, true) - assert.equal(_req, request) - // This event is emitted after the last chunk has been added to the body stream, - // not when it was consumed by the application - assert.equal(endEmitted, false) - assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) - }) - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { - keepAliveTimeout: 300e3 + return new Promise((resolve) => { + diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => { + assert.equal(request.completed, true) + assert.equal(_req, request) + // This event is emitted after the last chunk has been added to the body stream, + // not when it was consumed by the application + assert.equal(endEmitted, false) + assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')]) + resolve() }) - client.request({ - path: '/', - method: 'POST', - headers: reqHeaders, - body: 'hello world' - }, (err, data) => { - assert.ok(!err) - client.close() - data.body.on('end', function () { - endEmitted = true + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) + + client.request({ + path: '/', + method: 'POST', + headers: reqHeaders, + body: 'hello world' + }, (err, data) => { + assert.ok(!err) + client.close() + + data.body.on('end', function () { + endEmitted = true + }) }) }) }) From 2d02e901f32f0ee5e9792ccee13acaca8af4ff4b Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Fri, 29 Dec 2023 14:49:32 +0100 Subject: [PATCH 8/8] feat: use tspl --- package.json | 1 + test/diagnostics-channel/connect-error.js | 5 +++-- test/diagnostics-channel/error.js | 5 +++-- test/diagnostics-channel/get.js | 3 ++- test/diagnostics-channel/post-stream.js | 3 ++- test/diagnostics-channel/post.js | 3 ++- 6 files changed, 13 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index e88fcf07be1..3bc4666bc40 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,7 @@ "fuzz": "jsfuzz test/fuzzing/fuzz.js corpus" }, "devDependencies": { + "@matteo.collina/tspl": "^0.1.0", "@sinonjs/fake-timers": "^11.1.0", "@types/node": "^18.0.3", "abort-controller": "^3.0.0", diff --git a/test/diagnostics-channel/connect-error.js b/test/diagnostics-channel/connect-error.js index 5c64c19b254..a3bf2a45de7 100644 --- a/test/diagnostics-channel/connect-error.js +++ b/test/diagnostics-channel/connect-error.js @@ -1,7 +1,7 @@ 'use strict' const { test, skip } = require('node:test') -const assert = require('node:assert') +const { tspl } = require('@matteo.collina/tspl') let diagnosticsChannel @@ -14,8 +14,9 @@ try { const { Client } = require('../..') -test('Diagnostics channel - connect error', () => { +test('Diagnostics channel - connect error', (t) => { const connectError = new Error('custom error') + const assert = tspl(t, { plan: 16 }) let _connector diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { diff --git a/test/diagnostics-channel/error.js b/test/diagnostics-channel/error.js index 789ac33e702..2a737d70b33 100644 --- a/test/diagnostics-channel/error.js +++ b/test/diagnostics-channel/error.js @@ -1,7 +1,7 @@ 'use strict' const { test, skip, after } = require('node:test') -const assert = require('node:assert') +const { tspl } = require('@matteo.collina/tspl') let diagnosticsChannel @@ -15,7 +15,8 @@ try { const { Client } = require('../..') const { createServer } = require('http') -test('Diagnostics channel - error', () => { +test('Diagnostics channel - error', (t) => { + const assert = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.destroy() }) diff --git a/test/diagnostics-channel/get.js b/test/diagnostics-channel/get.js index fe40335de23..e0feeb2fe56 100644 --- a/test/diagnostics-channel/get.js +++ b/test/diagnostics-channel/get.js @@ -1,7 +1,7 @@ 'use strict' const { test, skip, after } = require('node:test') -const assert = require('node:assert') +const { tspl } = require('@matteo.collina/tspl') let diagnosticsChannel @@ -16,6 +16,7 @@ const { Client } = require('../..') const { createServer } = require('http') test('Diagnostics channel - get', (t) => { + const assert = tspl(t, { plan: 32 }) const server = createServer((req, res) => { res.setHeader('Content-Type', 'text/plain') res.setHeader('trailer', 'foo') diff --git a/test/diagnostics-channel/post-stream.js b/test/diagnostics-channel/post-stream.js index edcc90f6abd..3510e74993b 100644 --- a/test/diagnostics-channel/post-stream.js +++ b/test/diagnostics-channel/post-stream.js @@ -1,7 +1,7 @@ 'use strict' const { test, skip, after } = require('node:test') -const assert = require('node:assert') +const { tspl } = require('@matteo.collina/tspl') const { Readable } = require('stream') let diagnosticsChannel @@ -17,6 +17,7 @@ const { Client } = require('../..') const { createServer } = require('http') test('Diagnostics channel - post stream', (t) => { + const assert = tspl(t, { plan: 33 }) const server = createServer((req, res) => { req.resume() res.setHeader('Content-Type', 'text/plain') diff --git a/test/diagnostics-channel/post.js b/test/diagnostics-channel/post.js index 2fcb00c93fa..30203a7e7c7 100644 --- a/test/diagnostics-channel/post.js +++ b/test/diagnostics-channel/post.js @@ -1,7 +1,7 @@ 'use strict' const { test, skip, after } = require('node:test') -const assert = require('node:assert') +const { tspl } = require('@matteo.collina/tspl') let diagnosticsChannel @@ -16,6 +16,7 @@ const { Client } = require('../..') const { createServer } = require('http') test('Diagnostics channel - post', (t) => { + const assert = tspl(t, { plan: 33 }) const server = createServer((req, res) => { req.resume() res.setHeader('Content-Type', 'text/plain')