|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const { expectsError, mustCall } = require('../common'); |
4 | | -const { Agent, get, createServer } = require('http'); |
| 3 | +const { mustCall } = require('../common'); |
| 4 | +const { strictEqual } = require('assert'); |
| 5 | +const { Agent, get } = require('http'); |
5 | 6 |
|
6 | | -// Test that the `'timeout'` event is emitted on the `ClientRequest` instance |
7 | | -// when the socket timeout set via the `timeout` option of the `Agent` expires. |
| 7 | +// Test that the listener that forwards the `'timeout'` event from the socket to |
| 8 | +// the `ClientRequest` instance is added to the socket when the `timeout` option |
| 9 | +// of the `Agent` is set. |
8 | 10 |
|
9 | | -const server = createServer(mustCall(() => { |
10 | | - // Never respond. |
11 | | -})); |
| 11 | +const request = get({ |
| 12 | + agent: new Agent({ timeout: 50 }), |
| 13 | + lookup: () => {} |
| 14 | +}); |
12 | 15 |
|
13 | | -server.listen(() => { |
14 | | - const request = get({ |
15 | | - agent: new Agent({ timeout: 500 }), |
16 | | - port: server.address().port |
17 | | - }); |
| 16 | +request.on('socket', mustCall((socket) => { |
| 17 | + strictEqual(socket.timeout, 50); |
18 | 18 |
|
19 | | - request.on('error', expectsError({ |
20 | | - type: Error, |
21 | | - code: 'ECONNRESET', |
22 | | - message: 'socket hang up' |
23 | | - })); |
| 19 | + const listeners = socket.listeners('timeout'); |
24 | 20 |
|
25 | | - request.on('timeout', mustCall(() => { |
26 | | - request.abort(); |
27 | | - server.close(); |
28 | | - })); |
29 | | -}); |
| 21 | + strictEqual(listeners.length, 1); |
| 22 | + strictEqual(listeners[0], request.timeoutCb); |
| 23 | +})); |
0 commit comments