You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.8 KiB
62 lines
1.8 KiB
4 years ago
|
import assert from 'assert';
|
||
|
import nock from 'nock';
|
||
6 years ago
|
|
||
4 years ago
|
import getFetchWithTimeout from './fetch-with-timeout';
|
||
6 years ago
|
|
||
4 years ago
|
describe('getFetchWithTimeout', function () {
|
||
5 years ago
|
it('fetches a url', async function () {
|
||
4 years ago
|
nock('https://api.infura.io').get('/money').reply(200, '{"hodl": false}');
|
||
6 years ago
|
|
||
4 years ago
|
const fetchWithTimeout = getFetchWithTimeout(30000);
|
||
4 years ago
|
const response = await (
|
||
|
await fetchWithTimeout('https://api.infura.io/money')
|
||
4 years ago
|
).json();
|
||
6 years ago
|
assert.deepEqual(response, {
|
||
|
hodl: false,
|
||
4 years ago
|
});
|
||
|
});
|
||
6 years ago
|
|
||
5 years ago
|
it('throws when the request hits a custom timeout', async function () {
|
||
6 years ago
|
nock('https://api.infura.io')
|
||
|
.get('/moon')
|
||
|
.delay(2000)
|
||
4 years ago
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
|
||
6 years ago
|
|
||
4 years ago
|
const fetchWithTimeout = getFetchWithTimeout(123);
|
||
6 years ago
|
|
||
|
try {
|
||
4 years ago
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
||
|
r.json(),
|
||
|
);
|
||
|
assert.fail('Request should throw');
|
||
6 years ago
|
} catch (e) {
|
||
4 years ago
|
assert.ok(e);
|
||
6 years ago
|
}
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
5 years ago
|
it('should abort the request when the custom timeout is hit', async function () {
|
||
6 years ago
|
nock('https://api.infura.io')
|
||
|
.get('/moon')
|
||
|
.delay(2000)
|
||
4 years ago
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
|
||
6 years ago
|
|
||
4 years ago
|
const fetchWithTimeout = getFetchWithTimeout(123);
|
||
6 years ago
|
|
||
|
try {
|
||
4 years ago
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
||
|
r.json(),
|
||
|
);
|
||
|
assert.fail('Request should be aborted');
|
||
6 years ago
|
} catch (e) {
|
||
4 years ago
|
assert.deepEqual(e.message, 'Aborted');
|
||
6 years ago
|
}
|
||
4 years ago
|
});
|
||
4 years ago
|
|
||
|
it('throws on invalid timeout', async function () {
|
||
4 years ago
|
assert.throws(() => getFetchWithTimeout(), 'should throw');
|
||
|
assert.throws(() => getFetchWithTimeout(-1), 'should throw');
|
||
|
assert.throws(() => getFetchWithTimeout({}), 'should throw');
|
||
|
assert.throws(() => getFetchWithTimeout(true), 'should throw');
|
||
|
});
|
||
|
});
|