summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-immediate-error.js
blob: ff29ad72bd8585441d498659ede6142dfaa77567 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
// Flags: --expose-internals

// Make sure http.request() can catch immediate errors in
// net.createConnection().

const common = require('../common');
const assert = require('assert');
const net = require('net');
const http = require('http');
const { internalBinding } = require('internal/test/binding');
const { UV_ENETUNREACH } = internalBinding('uv');
const {
  newAsyncId,
  symbols: { async_id_symbol }
} = require('internal/async_hooks');

const agent = new http.Agent();
agent.createConnection = common.mustCall((cfg) => {
  const sock = new net.Socket();

  // Fake the handle so we can enforce returning an immediate error
  sock._handle = {
    connect: common.mustCall((req, addr, port) => {
      return UV_ENETUNREACH;
    }),
    readStart() {},
    close() {}
  };

  // Simulate just enough socket handle initialization
  sock[async_id_symbol] = newAsyncId();

  sock.connect(cfg);
  return sock;
});

http.get({
  host: '127.0.0.1',
  port: 1,
  agent
}).on('error', common.mustCall((err) => {
  assert.strictEqual(err.code, 'ENETUNREACH');
}));