Skip to content

Commit a1d3692

Browse files
committed
fix the connect timeout does not work
1 parent 65e84c6 commit a1d3692

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
16+
node-version: [14.x, 16.x, 18.x, 20.x]
1717
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
1818

1919
steps:

lib/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ export function request(url, opts) {
167167
});
168168
}
169169

170+
request.on('timeout', () => {
171+
var err = new Error();
172+
err.name = 'RequestTimeoutError';
173+
err.message = `ConnectTimeout: Connect ${url} failed.`;
174+
request.destroy();
175+
reject(err);
176+
});
177+
170178
request.on('response', fulfilled);
171179
request.on('error', rejected);
172180
request.once('socket', function (socket) {

test/index.test.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const server = http.createServer((req, res) => {
2323
setTimeout(() => {
2424
res.writeHead(200, {'Content-Type': 'text/plain'});
2525
res.end('Hello world!');
26-
}, 200);
26+
}, 1200);
2727
} else if (req.url === '/stream') {
2828
res.writeHead(200);
2929
const buffers = [];
@@ -209,19 +209,33 @@ describe('httpx', () => {
209209

210210
it('timeout should ok', async function () {
211211
try {
212-
await make(server)('/timeout', {timeout: 100});
212+
await make(server)('/timeout', {timeout: 1000});
213213
} catch (ex) {
214214
assert.strictEqual(ex.name, 'RequestTimeoutError');
215215
const port = server.address().port;
216-
assert.strictEqual(ex.message, `ReadTimeout(100). GET http://127.0.0.1:${port}/timeout failed.`);
216+
assert.strictEqual(ex.message, `ReadTimeout(1000). GET http://127.0.0.1:${port}/timeout failed.`);
217+
return;
218+
}
219+
assert.ok(false, 'should not ok');
220+
});
221+
222+
it('timeout(connectTimeout) should ok', async function () {
223+
try {
224+
await httpx.request('http://100.100.100.200', {
225+
readTimeout: 1000,
226+
connectTimeout: 1000
227+
});
228+
} catch (ex) {
229+
assert.strictEqual(ex.name, 'RequestTimeoutError');
230+
assert.strictEqual(ex.message, 'ConnectTimeout: Connect http://100.100.100.200 failed.');
217231
return;
218232
}
219233
assert.ok(false, 'should not ok');
220234
});
221235

222236
it('timeout(readTimeout) should ok', async function () {
223237
try {
224-
await make(server)('/readTimeout', {readTimeout: 100, connectTimeout: 50});
238+
await make(server)('/readTimeout', {readTimeout: 100, connectTimeout: 1000});
225239
} catch (ex) {
226240
assert.strictEqual(ex.name, 'RequestTimeoutError');
227241
const port = server.address().port;
@@ -233,7 +247,7 @@ describe('httpx', () => {
233247

234248
it('timeout(readTimeout & timeout) should ok', async function () {
235249
try {
236-
await make(server)('/readTimeout', {readTimeout: 100, connectTimeout: 50, timeout: 300});
250+
await make(server)('/readTimeout', {readTimeout: 100, connectTimeout: 1000, timeout: 300});
237251
} catch (ex) {
238252
assert.strictEqual(ex.name, 'RequestTimeoutError');
239253
const port = server.address().port;

0 commit comments

Comments
 (0)