From 70fb1bd038808617e317532aa88573451e44ce26 Mon Sep 17 00:00:00 2001 From: Brian White Date: Thu, 15 Jun 2017 03:28:29 -0400 Subject: [PATCH] test: improve http test reliability Fake the socket handle to ensure an immediate error is returned uniformly across all platforms. PR-URL: https://github.com/nodejs/node/pull/13693 Reviewed-By: James M Snell --- .../test-http-client-immediate-error.js | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-http-client-immediate-error.js b/test/parallel/test-http-client-immediate-error.js index 9fbe052efd4104..6b9cacb256f927 100644 --- a/test/parallel/test-http-client-immediate-error.js +++ b/test/parallel/test-http-client-immediate-error.js @@ -5,8 +5,36 @@ const common = require('../common'); const assert = require('assert'); +const net = require('net'); const http = require('http'); -const req = http.get({ host: '127.0.0.1', port: 1 }); -req.on('error', common.mustCall((err) => { - assert.strictEqual(err.code, 'ECONNREFUSED'); +const uv = process.binding('uv'); +const { async_id_symbol } = process.binding('async_wrap'); +const { newUid } = require('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.UV_ENETUNREACH; + }), + readStart() {}, + close() {} + }; + + // Simulate just enough socket handle initialization + sock[async_id_symbol] = newUid(); + + 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'); }));