-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #431 from podium-lib/cherry_pick_timeout_fix
Cherry pick timeout fix
- Loading branch information
Showing
3 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { test } from 'node:test'; | ||
import { rejects } from 'node:assert'; | ||
import HTTP from '../lib/http.js'; | ||
|
||
test('should abort the request if it takes longer than the timeout', async () => { | ||
// Mock the undici.Client's request method | ||
const mockRequestFn = async (url, { signal }) => { | ||
return new Promise((resolve, reject) => { | ||
// Simulate a delay longer than the timeout | ||
setTimeout(() => { | ||
if (signal.aborted) { | ||
const abortError = new Error( | ||
'Request aborted due to timeout', | ||
); | ||
abortError.name = 'AbortError'; | ||
reject(abortError); | ||
} else { | ||
resolve({ | ||
statusCode: 200, | ||
headers: {}, | ||
body: 'OK', | ||
}); | ||
} | ||
}, 2000); // 2 seconds delay | ||
}); | ||
}; | ||
|
||
// @ts-ignore | ||
const http = new HTTP(mockRequestFn); | ||
const url = 'https://example.com/test'; | ||
const options = { | ||
method: /** @type {'GET'} */ ('GET'), | ||
timeout: 1000, // 1 second timeout | ||
}; | ||
|
||
// Assert that the request is rejected with an AbortError | ||
await rejects( | ||
http.request(url, options), | ||
(/** @type {Error} */ err) => | ||
err.name === 'AbortError' && | ||
err.message === 'Request aborted due to timeout', | ||
'Expected request to be aborted due to timeout', | ||
); | ||
}); |