Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8ec6881

Browse files
mscdexaddaleax
authored andcommittedJun 20, 2017
test: improve http test reliability
Fake the socket handle to ensure an immediate error is returned uniformly across all platforms. PR-URL: #13693 Reviewed-By: James M Snell <[email protected]>
1 parent b8271a4 commit 8ec6881

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed
 

‎test/parallel/test-http-client-immediate-error.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,36 @@
55

66
const common = require('../common');
77
const assert = require('assert');
8+
const net = require('net');
89
const http = require('http');
9-
const req = http.get({ host: '127.0.0.1', port: 1 });
10-
req.on('error', common.mustCall((err) => {
11-
assert.strictEqual(err.code, 'ECONNREFUSED');
10+
const uv = process.binding('uv');
11+
const { async_id_symbol } = process.binding('async_wrap');
12+
const { newUid } = require('async_hooks');
13+
14+
const agent = new http.Agent();
15+
agent.createConnection = common.mustCall((cfg) => {
16+
const sock = new net.Socket();
17+
18+
// Fake the handle so we can enforce returning an immediate error
19+
sock._handle = {
20+
connect: common.mustCall((req, addr, port) => {
21+
return uv.UV_ENETUNREACH;
22+
}),
23+
readStart() {},
24+
close() {}
25+
};
26+
27+
// Simulate just enough socket handle initialization
28+
sock[async_id_symbol] = newUid();
29+
30+
sock.connect(cfg);
31+
return sock;
32+
});
33+
34+
http.get({
35+
host: '127.0.0.1',
36+
port: 1,
37+
agent
38+
}).on('error', common.mustCall((err) => {
39+
assert.strictEqual(err.code, 'ENETUNREACH');
1240
}));

0 commit comments

Comments
 (0)
Please sign in to comment.