Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
node-version: [14.x, 16.x, 18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
8 changes: 8 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 19 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -209,19 +209,33 @@ 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');
});

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;
Expand All @@ -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;
Expand Down