From e29231d7febed05a9189d788bdbea92f2a62c42e Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Sun, 31 Dec 2023 00:28:02 +0100 Subject: [PATCH] feat: port diagnostic-channel tests to node test runner (#2559) --- package.json | 1 + test/diagnostics-channel/connect-error.js | 98 ++++----- test/diagnostics-channel/error.js | 69 ++++--- test/diagnostics-channel/get.js | 224 ++++++++++---------- test/diagnostics-channel/post-stream.js | 236 +++++++++++----------- test/diagnostics-channel/post.js | 235 ++++++++++----------- 6 files changed, 447 insertions(+), 416 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 f7e842dbe28..a3bf2a45de7 100644 --- a/test/diagnostics-channel/connect-error.js +++ b/test/diagnostics-channel/connect-error.js @@ -1,61 +1,65 @@ 'use strict' -const t = require('tap') +const { test, skip } = require('node:test') +const { tspl } = require('@matteo.collina/tspl') 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) - -const connectError = new Error('custom error') - -let _connector -diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { - _connector = connector - - t.equal(typeof _connector, 'function') - t.equal(Object.keys(connectParams).length, 6) - - const { host, hostname, protocol, port, servername } = connectParams - - 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) - - 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) -}) - -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) => { - t.equal(err, connectError) +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 }) => { + _connector = connector + + assert.equal(typeof _connector, 'function') + assert.equal(Object.keys(connectParams).length, 6) + + 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) + }) + + 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 + + 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) } + }) + + 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 1f350b1dd49..2a737d70b33 100644 --- a/test/diagnostics-channel/error.js +++ b/test/diagnostics-channel/error.js @@ -1,52 +1,57 @@ 'use strict' -const t = require('tap') +const { test, skip, after } = require('node:test') +const { tspl } = require('@matteo.collina/tspl') 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', (t) => { + const assert = tspl(t, { plan: 3 }) + 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') + + 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 9d868c3d897..e0feeb2fe56 100644 --- a/test/diagnostics-channel/get.js +++ b/test/diagnostics-channel/get.js @@ -1,141 +1,149 @@ 'use strict' -const t = require('tap') +const { test, skip, after } = require('node:test') +const { tspl } = require('@matteo.collina/tspl') 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) - -const server = createServer((req, res) => { - res.setHeader('Content-Type', 'text/plain') - res.setHeader('trailer', 'foo') - res.write('hello') - res.addTrailers({ - foo: 'oof' +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') + res.write('hello') + res.addTrailers({ + foo: 'oof' + }) + res.end() }) - res.end() -}) -t.teardown(server.close.bind(server)) - -const reqHeaders = { - foo: undefined, - bar: 'bar' -} -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') -}) + 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.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') + }) -let _connector -diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { - _connector = connector + let _connector + diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { + _connector = connector - t.equal(typeof _connector, 'function') - t.equal(Object.keys(connectParams).length, 6) + assert.equal(typeof _connector, 'function') + assert.equal(Object.keys(connectParams).length, 6) - const { host, hostname, protocol, port, servername } = connectParams + const { host, hostname, protocol, port, servername } = connectParams - 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(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.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 - t.equal(_connector, connector) - t.equal(Object.keys(connectParams).length, 6) + assert.equal(_connector, connector) + assert.equal(Object.keys(connectParams).length, 6) - const { host, hostname, protocol, port, servername } = connectParams + const { host, hostname, protocol, port, servername } = connectParams - 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(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) -diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { - t.equal(_req, request) - t.equal(_socket, socket) + diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { + assert.equal(_req, request) + assert.equal(_socket, socket) - const expectedHeaders = [ - 'GET / HTTP/1.1', - `host: localhost:${server.address().port}`, - 'connection: keep-alive', - 'bar: bar', - 'hello: world' - ] + const expectedHeaders = [ + 'GET / HTTP/1.1', + `host: localhost:${server.address().port}`, + 'connection: keep-alive', + 'bar: bar', + 'hello: world' + ] - t.equal(headers, expectedHeaders.join('\r\n') + '\r\n') -}) + assert.equal(headers, expectedHeaders.join('\r\n') + '\r\n') + }) -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: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') + }) -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')]) -}) + let endEmitted = false + + 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() + }) -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 + 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 236b9bba131..3510e74993b 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 { tspl } = require('@matteo.collina/tspl') const { Readable } = require('stream') let diagnosticsChannel @@ -8,142 +9,147 @@ 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 assert = tspl(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' + }) + 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 - t.equal(typeof _connector, 'function') - t.equal(Object.keys(connectParams).length, 6) + assert.equal(typeof _connector, 'function') + assert.equal(Object.keys(connectParams).length, 6) - const { host, hostname, protocol, port, servername } = connectParams + const { host, hostname, protocol, port, servername } = connectParams - 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(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.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 - t.equal(Object.keys(connectParams).length, 6) - t.equal(_connector, 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(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(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) -diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { - t.equal(_req, request) - t.equal(_socket, socket) + diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { + assert.equal(_req, request) + assert.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' + ] - t.equal(headers, expectedHeaders.join('\r\n') + '\r\n') -}) + assert.equal(headers, expectedHeaders.join('\r\n') + '\r\n') + }) -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: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:bodySent').subscribe(({ request }) => { - t.equal(_req, request) -}) + diagnosticsChannel.channel('undici:request:bodySent').subscribe(({ request }) => { + assert.equal(_req, request) + }) -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')]) -}) + let endEmitted = false + + 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() + }) -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 + 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 fc02eb55d8c..30203a7e7c7 100644 --- a/test/diagnostics-channel/post.js +++ b/test/diagnostics-channel/post.js @@ -1,147 +1,154 @@ 'use strict' -const t = require('tap') +const { test, skip, after } = require('node:test') +const { tspl } = require('@matteo.collina/tspl') 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 assert = tspl(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' + }) + 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 _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')) -}) -let _connector -diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { - _connector = connector + let _connector + diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { + _connector = connector - t.equal(typeof _connector, 'function') - t.equal(Object.keys(connectParams).length, 6) + assert.equal(typeof _connector, 'function') + assert.equal(Object.keys(connectParams).length, 6) - const { host, hostname, protocol, port, servername } = connectParams + const { host, hostname, protocol, port, servername } = connectParams - 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(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.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 - t.equal(Object.keys(connectParams).length, 6) - t.equal(_connector, 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(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(host, `localhost:${server.address().port}`) + assert.equal(hostname, 'localhost') + assert.equal(port, String(server.address().port)) + assert.equal(protocol, 'http:') + assert.equal(servername, null) + }) -diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { - t.equal(_req, request) - t.equal(_socket, socket) + diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => { + assert.equal(_req, request) + assert.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' + ] - t.equal(headers, expectedHeaders.join('\r\n') + '\r\n') -}) + assert.equal(headers, expectedHeaders.join('\r\n') + '\r\n') + }) -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: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:bodySent').subscribe(({ request }) => { - t.equal(_req, request) -}) + diagnosticsChannel.channel('undici:request:bodySent').subscribe(({ request }) => { + assert.equal(_req, request) + }) -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')]) -}) + let endEmitted = false + + 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() + }) -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 + 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 + }) + }) }) }) })