-
Notifications
You must be signed in to change notification settings - Fork 551
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: port diagnostic-channel tests to node test runner (#2559)
- Loading branch information
Showing
6 changed files
with
447 additions
and
416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.