summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-agent-timeout-option.js
blob: 3c694a0ef7d2d243d0f3a97c6997e3e7c9354032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
'use strict';

const { expectsError, mustCall } = require('../common');
const { Agent, get, createServer } = require('http');

// Test that the `'timeout'` event is emitted on the `ClientRequest` instance
// when the socket timeout set via the `timeout` option of the `Agent` expires.

const server = createServer(mustCall(() => {
  // Never respond.
}));

server.listen(() => {
  const request = get({
    agent: new Agent({ timeout: 500 }),
    port: server.address().port
  });

  request.on('error', expectsError({
    type: Error,
    code: 'ECONNRESET',
    message: 'socket hang up'
  }));

  request.on('timeout', mustCall(() => {
    request.abort();
    server.close();
  }));
});