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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions test/node-test/diagnostics-channel/connect-error.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
'use strict'

const { test } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const diagnosticsChannel = require('node:diagnostics_channel')
const { Client } = require('../../..')

test('Diagnostics channel - connect error', (t) => {
const connectError = new Error('custom error')
const assert = tspl(t, { plan: 16 })
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, 7)
t.assert.strictEqual(typeof _connector, 'function')
t.assert.strictEqual(Object.keys(connectParams).length, 7)

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.assert.strictEqual(host, 'localhost:1234')
t.assert.strictEqual(hostname, 'localhost')
t.assert.strictEqual(port, '1234')
t.assert.strictEqual(protocol, 'http:')
t.assert.strictEqual(servername, null)
})

diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => {
assert.equal(Object.keys(connectParams).length, 7)
assert.equal(_connector, connector)
t.assert.strictEqual(Object.keys(connectParams).length, 7)
t.assert.strictEqual(_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)
t.assert.strictEqual(error, connectError)
t.assert.strictEqual(host, 'localhost:1234')
t.assert.strictEqual(hostname, 'localhost')
t.assert.strictEqual(port, '1234')
t.assert.strictEqual(protocol, 'http:')
t.assert.strictEqual(servername, null)
})

const client = new Client('http://localhost:1234', {
Expand All @@ -48,7 +47,7 @@ test('Diagnostics channel - connect error', (t) => {
path: '/',
method: 'GET'
}, (err, data) => {
assert.equal(err, connectError)
t.assert.strictEqual(err, connectError)
client.close()
resolve()
})
Expand Down
9 changes: 4 additions & 5 deletions test/node-test/diagnostics-channel/error.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict'

const { test, after } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const diagnosticsChannel = require('node:diagnostics_channel')
const { Client } = require('../../..')
const { createServer } = require('node:http')

test('Diagnostics channel - error', (t) => {
const assert = tspl(t, { plan: 3 })
t.plan(3)
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.destroy()
})
Expand All @@ -24,8 +23,8 @@ test('Diagnostics channel - error', (t) => {
})

diagnosticsChannel.channel('undici:request:error').subscribe(({ request, error }) => {
assert.equal(_req, request)
assert.equal(error.code, 'UND_ERR_SOCKET')
t.assert.strictEqual(_req, request)
t.assert.strictEqual(error.code, 'UND_ERR_SOCKET')
})

return new Promise((resolve) => {
Expand All @@ -39,7 +38,7 @@ test('Diagnostics channel - error', (t) => {
method: 'GET',
headers: reqHeaders
}, (err, data) => {
assert.equal(err.code, 'UND_ERR_SOCKET')
t.assert.strictEqual(err.code, 'UND_ERR_SOCKET')
client.close()
resolve()
})
Expand Down
35 changes: 17 additions & 18 deletions test/node-test/diagnostics-channel/get-h2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
const { createSecureServer } = require('node:http2')
const diagnosticsChannel = require('node:diagnostics_channel')
Expand All @@ -12,8 +11,8 @@ test('Diagnostics channel - get support H2', async t => {
const server = createSecureServer(pem)

server.on('stream', (stream, headers, _flags, rawHeaders) => {
t.strictEqual(headers['x-my-header'], 'foo')
t.strictEqual(headers[':method'], 'GET')
t.assert.strictEqual(headers['x-my-header'], 'foo')
t.assert.strictEqual(headers[':method'], 'GET')
stream.respond({
'content-type': 'text/plain; charset=utf-8',
'x-custom-h2': 'hello',
Expand All @@ -26,10 +25,10 @@ test('Diagnostics channel - get support H2', async t => {
await once(server, 'listening')

diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => {
t.strictEqual(request.origin, `https://localhost:${server.address().port}`)
t.strictEqual(request.completed, false)
t.strictEqual(request.method, 'GET')
t.strictEqual(request.path, '/')
t.assert.strictEqual(request.origin, `https://localhost:${server.address().port}`)
t.assert.strictEqual(request.completed, false)
t.assert.strictEqual(request.method, 'GET')
t.assert.strictEqual(request.path, '/')
})

let _socket
Expand All @@ -38,15 +37,15 @@ test('Diagnostics channel - get support H2', async t => {
})

diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ headers, socket }) => {
t.strictEqual(_socket, socket)
t.assert.strictEqual(_socket, socket)
const expectedHeaders = [
'x-my-header: foo',
`:authority: localhost:${server.address().port}`,
':method: GET',
':path: /',
':scheme: https'
]
t.strictEqual(headers, expectedHeaders.join('\r\n') + '\r\n')
t.assert.strictEqual(headers, expectedHeaders.join('\r\n') + '\r\n')
})

const client = new Client(`https://localhost:${server.address().port}`, {
Expand All @@ -56,7 +55,7 @@ test('Diagnostics channel - get support H2', async t => {
allowH2: true
})

t = tspl(t, { plan: 24 })
t.plan(24)
after(() => server.close())
after(() => client.close())

Expand All @@ -74,10 +73,10 @@ test('Diagnostics channel - get support H2', async t => {
})

await once(response.body, 'end')
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
t.strictEqual(response.headers['x-custom-h2'], 'hello')
t.strictEqual(Buffer.concat(body).toString('utf8'), 'hello h2!')
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
t.assert.strictEqual(response.headers['x-custom-h2'], 'hello')
t.assert.strictEqual(Buffer.concat(body).toString('utf8'), 'hello h2!')

// request again
body = []
Expand All @@ -94,8 +93,8 @@ test('Diagnostics channel - get support H2', async t => {
})

await once(response.body, 'end')
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
t.strictEqual(response.headers['x-custom-h2'], 'hello')
t.strictEqual(Buffer.concat(body).toString('utf8'), 'hello h2!')
t.assert.strictEqual(response.statusCode, 200)
t.assert.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
t.assert.strictEqual(response.headers['x-custom-h2'], 'hello')
t.assert.strictEqual(Buffer.concat(body).toString('utf8'), 'hello h2!')
})
77 changes: 38 additions & 39 deletions test/node-test/diagnostics-channel/get.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict'

const { test, after } = require('node:test')
const { tspl } = require('@matteo.collina/tspl')
const diagnosticsChannel = require('node:diagnostics_channel')
const { Client } = require('../../..')
const { createServer } = require('node:http')

test('Diagnostics channel - get', (t) => {
const assert = tspl(t, { plan: 36 })
t.plan(36)
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.setHeader('Content-Type', 'text/plain')
res.setHeader('trailer', 'foo')
Expand All @@ -28,50 +27,50 @@ test('Diagnostics channel - get', (t) => {
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.deepStrictEqual(request.headers, ['bar', 'bar'])
t.assert.strictEqual(request.origin, `http://localhost:${server.address().port}`)
t.assert.strictEqual(request.completed, false)
t.assert.strictEqual(request.method, 'GET')
t.assert.strictEqual(request.path, '/')
t.assert.deepStrictEqual(request.headers, ['bar', 'bar'])
request.addHeader('hello', 'world')
assert.deepStrictEqual(request.headers, ['bar', 'bar', 'hello', 'world'])
t.assert.deepStrictEqual(request.headers, ['bar', 'bar', 'hello', 'world'])
})

let _connector
diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => {
_connector = connector

assert.equal(typeof _connector, 'function')
assert.equal(Object.keys(connectParams).length, 7)
t.assert.strictEqual(typeof _connector, 'function')
t.assert.strictEqual(Object.keys(connectParams).length, 7)

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.assert.strictEqual(host, `localhost:${server.address().port}`)
t.assert.strictEqual(hostname, 'localhost')
t.assert.strictEqual(port, String(server.address().port))
t.assert.strictEqual(protocol, 'http:')
t.assert.strictEqual(servername, null)
})

let _socket
diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams, socket, connector }) => {
_socket = socket

assert.equal(_connector, connector)
assert.equal(Object.keys(connectParams).length, 7)
t.assert.strictEqual(_connector, connector)
t.assert.strictEqual(Object.keys(connectParams).length, 7)

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.assert.strictEqual(host, `localhost:${server.address().port}`)
t.assert.strictEqual(hostname, 'localhost')
t.assert.strictEqual(port, String(server.address().port))
t.assert.strictEqual(protocol, 'http:')
t.assert.strictEqual(servername, null)
})

diagnosticsChannel.channel('undici:client:sendHeaders').subscribe(({ request, headers, socket }) => {
assert.equal(_req, request)
assert.equal(_socket, socket)
t.assert.strictEqual(_req, request)
t.assert.strictEqual(_socket, socket)

const expectedHeaders = [
'GET / HTTP/1.1',
Expand All @@ -81,12 +80,12 @@ test('Diagnostics channel - get', (t) => {
'hello: world'
]

assert.deepStrictEqual(headers, expectedHeaders.join('\r\n') + '\r\n')
t.assert.deepStrictEqual(headers, expectedHeaders.join('\r\n') + '\r\n')
})

diagnosticsChannel.channel('undici:request:headers').subscribe(({ request, response }) => {
assert.equal(_req, request)
assert.equal(response.statusCode, 200)
t.assert.strictEqual(_req, request)
t.assert.strictEqual(response.statusCode, 200)
const expectedHeaders = [
Buffer.from('Content-Type'),
Buffer.from('text/plain'),
Expand All @@ -101,39 +100,39 @@ test('Diagnostics channel - get', (t) => {
Buffer.from('Transfer-Encoding'),
Buffer.from('chunked')
]
assert.deepStrictEqual(response.headers, expectedHeaders)
assert.equal(response.statusText, 'OK')
t.assert.deepStrictEqual(response.headers, expectedHeaders)
t.assert.strictEqual(response.statusText, 'OK')
})

let bodySent = false
diagnosticsChannel.channel('undici:request:bodySent').subscribe(({ request }) => {
assert.equal(_req, request)
t.assert.strictEqual(_req, request)
bodySent = true
})
diagnosticsChannel.channel('undici:request:bodyChunkSent').subscribe(() => {
assert.fail('should not emit undici:request:bodyChunkSent for GET requests')
t.assert.fail('should not emit undici:request:bodyChunkSent for GET requests')
})

let endEmitted = false

return new Promise((resolve) => {
const respChunks = []
diagnosticsChannel.channel('undici:request:bodyChunkReceived').subscribe(({ request, chunk }) => {
assert.equal(_req, request)
t.assert.strictEqual(_req, request)
respChunks.push(chunk)
})

diagnosticsChannel.channel('undici:request:trailers').subscribe(({ request, trailers }) => {
assert.equal(bodySent, true)
assert.equal(request.completed, true)
assert.equal(_req, request)
t.assert.strictEqual(bodySent, true)
t.assert.strictEqual(request.completed, true)
t.assert.strictEqual(_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')])
t.assert.strictEqual(endEmitted, false)
t.assert.deepStrictEqual(trailers, [Buffer.from('foo'), Buffer.from('oof')])

const respData = Buffer.concat(respChunks)
assert.deepStrictEqual(respData, Buffer.from('hello'))
t.assert.deepStrictEqual(respData, Buffer.from('hello'))

resolve()
})
Expand All @@ -148,7 +147,7 @@ test('Diagnostics channel - get', (t) => {
method: 'GET',
headers: reqHeaders
}, (err, data) => {
assert.ok(!err)
t.assert.ok(!err)
client.close()

data.body.on('end', function () {
Expand Down
Loading
Loading