|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const t = require('tap') |
| 3 | +const { test, skip } = require('node:test') |
| 4 | +const { tspl } = require('@matteo.collina/tspl') |
4 | 5 |
|
5 | 6 | let diagnosticsChannel |
6 | 7 |
|
7 | 8 | try { |
8 | 9 | diagnosticsChannel = require('diagnostics_channel') |
9 | 10 | } catch { |
10 | | - t.skip('missing diagnostics_channel') |
| 11 | + skip('missing diagnostics_channel') |
11 | 12 | process.exit(0) |
12 | 13 | } |
13 | 14 |
|
14 | 15 | const { Client } = require('../..') |
15 | 16 |
|
16 | | -t.plan(16) |
17 | | - |
18 | | -const connectError = new Error('custom error') |
19 | | - |
20 | | -let _connector |
21 | | -diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { |
22 | | - _connector = connector |
23 | | - |
24 | | - t.equal(typeof _connector, 'function') |
25 | | - t.equal(Object.keys(connectParams).length, 6) |
26 | | - |
27 | | - const { host, hostname, protocol, port, servername } = connectParams |
28 | | - |
29 | | - t.equal(host, 'localhost:1234') |
30 | | - t.equal(hostname, 'localhost') |
31 | | - t.equal(port, '1234') |
32 | | - t.equal(protocol, 'http:') |
33 | | - t.equal(servername, null) |
34 | | -}) |
35 | | - |
36 | | -diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => { |
37 | | - t.equal(Object.keys(connectParams).length, 6) |
38 | | - t.equal(_connector, connector) |
39 | | - |
40 | | - const { host, hostname, protocol, port, servername } = connectParams |
41 | | - |
42 | | - t.equal(error, connectError) |
43 | | - t.equal(host, 'localhost:1234') |
44 | | - t.equal(hostname, 'localhost') |
45 | | - t.equal(port, '1234') |
46 | | - t.equal(protocol, 'http:') |
47 | | - t.equal(servername, null) |
48 | | -}) |
49 | | - |
50 | | -const client = new Client('http://localhost:1234', { |
51 | | - connect: (_, cb) => { cb(connectError, null) } |
52 | | -}) |
53 | | - |
54 | | -t.teardown(client.close.bind(client)) |
55 | | - |
56 | | -client.request({ |
57 | | - path: '/', |
58 | | - method: 'GET' |
59 | | -}, (err, data) => { |
60 | | - t.equal(err, connectError) |
| 17 | +test('Diagnostics channel - connect error', (t) => { |
| 18 | + const connectError = new Error('custom error') |
| 19 | + const assert = tspl(t, { plan: 16 }) |
| 20 | + |
| 21 | + let _connector |
| 22 | + diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectParams, connector }) => { |
| 23 | + _connector = connector |
| 24 | + |
| 25 | + assert.equal(typeof _connector, 'function') |
| 26 | + assert.equal(Object.keys(connectParams).length, 6) |
| 27 | + |
| 28 | + const { host, hostname, protocol, port, servername } = connectParams |
| 29 | + |
| 30 | + assert.equal(host, 'localhost:1234') |
| 31 | + assert.equal(hostname, 'localhost') |
| 32 | + assert.equal(port, '1234') |
| 33 | + assert.equal(protocol, 'http:') |
| 34 | + assert.equal(servername, null) |
| 35 | + }) |
| 36 | + |
| 37 | + diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => { |
| 38 | + assert.equal(Object.keys(connectParams).length, 6) |
| 39 | + assert.equal(_connector, connector) |
| 40 | + |
| 41 | + const { host, hostname, protocol, port, servername } = connectParams |
| 42 | + |
| 43 | + assert.equal(error, connectError) |
| 44 | + assert.equal(host, 'localhost:1234') |
| 45 | + assert.equal(hostname, 'localhost') |
| 46 | + assert.equal(port, '1234') |
| 47 | + assert.equal(protocol, 'http:') |
| 48 | + assert.equal(servername, null) |
| 49 | + }) |
| 50 | + |
| 51 | + const client = new Client('http://localhost:1234', { |
| 52 | + connect: (_, cb) => { cb(connectError, null) } |
| 53 | + }) |
| 54 | + |
| 55 | + return new Promise((resolve) => { |
| 56 | + client.request({ |
| 57 | + path: '/', |
| 58 | + method: 'GET' |
| 59 | + }, (err, data) => { |
| 60 | + assert.equal(err, connectError) |
| 61 | + client.close() |
| 62 | + resolve() |
| 63 | + }) |
| 64 | + }) |
61 | 65 | }) |
0 commit comments