Skip to content

Commit

Permalink
test: improve http test reliability
Browse files Browse the repository at this point in the history
Fake the socket handle to ensure an immediate error is returned
uniformly across all platforms.

PR-URL: #13693
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
mscdex committed Jun 17, 2017
1 parent 460ee75 commit fcd82e7
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions test/parallel/test-http-client-immediate-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}));

0 comments on commit fcd82e7

Please sign in to comment.