From 887a6be7a4ea3ad710dc0a47c19f079953afdd44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=AE=E5=86=AC?= <954152927@qq.com> Date: Wed, 6 Nov 2024 18:25:02 +0800 Subject: [PATCH] fix the connect timeout does not work --- lib/index.js | 8 ++++++++ test/index.test.js | 24 +++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6719104..953a36b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -167,6 +167,14 @@ export function request(url, opts) { }); } + request.on('timeout', () => { + var err = new Error(); + err.name = 'RequestTimeoutError'; + err.message = `ConnectTimeout: Connect ${url} failed.`; + request.destroy(); + reject(err); + }); + request.on('response', fulfilled); request.on('error', rejected); request.once('socket', function (socket) { diff --git a/test/index.test.js b/test/index.test.js index 8904688..6532462 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -23,7 +23,7 @@ const server = http.createServer((req, res) => { setTimeout(() => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello world!'); - }, 200); + }, 1200); } else if (req.url === '/stream') { res.writeHead(200); const buffers = []; @@ -209,11 +209,25 @@ describe('httpx', () => { it('timeout should ok', async function () { try { - await make(server)('/timeout', {timeout: 100}); + await make(server)('/timeout', {timeout: 1000}); } catch (ex) { assert.strictEqual(ex.name, 'RequestTimeoutError'); const port = server.address().port; - assert.strictEqual(ex.message, `ReadTimeout(100). GET http://127.0.0.1:${port}/timeout failed.`); + assert.strictEqual(ex.message, `ReadTimeout(1000). GET http://127.0.0.1:${port}/timeout failed.`); + return; + } + assert.ok(false, 'should not ok'); + }); + + it('timeout(connectTimeout) should ok', async function () { + try { + await httpx.request('http://100.100.100.200', { + readTimeout: 1000, + connectTimeout: 1000 + }); + } catch (ex) { + assert.strictEqual(ex.name, 'RequestTimeoutError'); + assert.strictEqual(ex.message, 'ConnectTimeout: Connect http://100.100.100.200 failed.'); return; } assert.ok(false, 'should not ok'); @@ -221,7 +235,7 @@ describe('httpx', () => { it('timeout(readTimeout) should ok', async function () { try { - await make(server)('/readTimeout', {readTimeout: 100, connectTimeout: 50}); + await make(server)('/readTimeout', {readTimeout: 100, connectTimeout: 1000}); } catch (ex) { assert.strictEqual(ex.name, 'RequestTimeoutError'); const port = server.address().port; @@ -233,7 +247,7 @@ describe('httpx', () => { it('timeout(readTimeout & timeout) should ok', async function () { try { - await make(server)('/readTimeout', {readTimeout: 100, connectTimeout: 50, timeout: 300}); + await make(server)('/readTimeout', {readTimeout: 100, connectTimeout: 1000, timeout: 300}); } catch (ex) { assert.strictEqual(ex.name, 'RequestTimeoutError'); const port = server.address().port;