11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-internals
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// Make sure http.request() can catch immediate errors in
51cb0ef41Sopenharmony_ci// net.createConnection().
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst common = require('../common');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst net = require('net');
101cb0ef41Sopenharmony_ciconst http = require('http');
111cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
121cb0ef41Sopenharmony_ciconst { UV_ENETUNREACH } = internalBinding('uv');
131cb0ef41Sopenharmony_ciconst {
141cb0ef41Sopenharmony_ci  newAsyncId,
151cb0ef41Sopenharmony_ci  symbols: { async_id_symbol }
161cb0ef41Sopenharmony_ci} = require('internal/async_hooks');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst agent = new http.Agent();
191cb0ef41Sopenharmony_ciagent.createConnection = common.mustCall((cfg) => {
201cb0ef41Sopenharmony_ci  const sock = new net.Socket();
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  // Fake the handle so we can enforce returning an immediate error
231cb0ef41Sopenharmony_ci  sock._handle = {
241cb0ef41Sopenharmony_ci    connect: common.mustCall((req, addr, port) => {
251cb0ef41Sopenharmony_ci      return UV_ENETUNREACH;
261cb0ef41Sopenharmony_ci    }),
271cb0ef41Sopenharmony_ci    readStart() {},
281cb0ef41Sopenharmony_ci    close() {}
291cb0ef41Sopenharmony_ci  };
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // Simulate just enough socket handle initialization
321cb0ef41Sopenharmony_ci  sock[async_id_symbol] = newAsyncId();
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  sock.connect(cfg);
351cb0ef41Sopenharmony_ci  return sock;
361cb0ef41Sopenharmony_ci});
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cihttp.get({
391cb0ef41Sopenharmony_ci  host: '127.0.0.1',
401cb0ef41Sopenharmony_ci  port: 1,
411cb0ef41Sopenharmony_ci  agent
421cb0ef41Sopenharmony_ci}).on('error', common.mustCall((err) => {
431cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'ENETUNREACH');
441cb0ef41Sopenharmony_ci}));
45