1'use strict';
2// Flags: --expose-internals
3
4// Make sure http.request() can catch immediate errors in
5// net.createConnection().
6
7const common = require('../common');
8const assert = require('assert');
9const net = require('net');
10const http = require('http');
11const { internalBinding } = require('internal/test/binding');
12const { UV_ENETUNREACH } = internalBinding('uv');
13const {
14  newAsyncId,
15  symbols: { async_id_symbol }
16} = require('internal/async_hooks');
17
18const agent = new http.Agent();
19agent.createConnection = common.mustCall((cfg) => {
20  const sock = new net.Socket();
21
22  // Fake the handle so we can enforce returning an immediate error
23  sock._handle = {
24    connect: common.mustCall((req, addr, port) => {
25      return UV_ENETUNREACH;
26    }),
27    readStart() {},
28    close() {}
29  };
30
31  // Simulate just enough socket handle initialization
32  sock[async_id_symbol] = newAsyncId();
33
34  sock.connect(cfg);
35  return sock;
36});
37
38http.get({
39  host: '127.0.0.1',
40  port: 1,
41  agent
42}).on('error', common.mustCall((err) => {
43  assert.strictEqual(err.code, 'ENETUNREACH');
44}));
45