Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ export class HttpClient extends EventEmitter {
headersTimeout = args.timeout[0] ?? headersTimeout;
bodyTimeout = args.timeout[1] ?? bodyTimeout;
} else {
headersTimeout = bodyTimeout = args.timeout;
// compatible with urllib@2 timeout string format
Comment thread
fengmk2 marked this conversation as resolved.
headersTimeout = bodyTimeout = typeof args.timeout === 'string' ? parseInt(args.timeout) : args.timeout;
Comment thread
fengmk2 marked this conversation as resolved.
Comment thread
fengmk2 marked this conversation as resolved.
}
}
if (originalHeaders) {
Expand Down
21 changes: 21 additions & 0 deletions test/options.timeout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ describe('options.timeout.test.ts', () => {
});
});

it('should timeout support string format', async () => {
await assert.rejects(async () => {
await urllib.request(`${_url}?timeout=2000`, {
// @ts-expect-error check string format
timeout: '10',
});
}, (err: any) => {
// console.error(err);
assert.equal(err.name, 'HttpClientRequestTimeoutError');
assert.equal(err.message, 'Request timeout for 10 ms');
assert.equal(err.cause.name, 'HeadersTimeoutError');
assert.equal(err.cause.message, 'Headers Timeout Error');
assert.equal(err.cause.code, 'UND_ERR_HEADERS_TIMEOUT');

assert.equal(err.res.status, -1);
assert(err.res.rt > 10, `actual ${err.res.rt}`);
assert.equal(typeof err.res.rt, 'number');
return true;
});
});

it('should timeout on h2', async () => {
const httpClient = new HttpClient({
allowH2: true,
Expand Down