2021-02-04 19:15:23 +01:00
|
|
|
import assert from 'assert';
|
|
|
|
import nock from 'nock';
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import getFetchWithTimeout from '../../../shared/modules/fetch-with-timeout';
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-01-19 17:41:57 +01:00
|
|
|
describe('getFetchWithTimeout', function () {
|
2020-02-11 17:51:13 +01:00
|
|
|
it('fetches a url', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
nock('https://api.infura.io').get('/money').reply(200, '{"hodl": false}');
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(30000);
|
2021-01-19 17:41:57 +01:00
|
|
|
const response = await (
|
|
|
|
await fetchWithTimeout('https://api.infura.io/money')
|
2021-02-04 19:15:23 +01:00
|
|
|
).json();
|
2019-06-18 14:17:14 +02:00
|
|
|
assert.deepEqual(response, {
|
|
|
|
hodl: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('throws when the request hits a custom timeout', async function () {
|
2019-06-18 14:17:14 +02:00
|
|
|
nock('https://api.infura.io')
|
|
|
|
.get('/moon')
|
|
|
|
.delay(2000)
|
2021-02-04 19:15:23 +01:00
|
|
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(123);
|
2019-06-18 14:17:14 +02:00
|
|
|
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
|
|
|
r.json(),
|
|
|
|
);
|
|
|
|
assert.fail('Request should throw');
|
2019-06-18 14:17:14 +02:00
|
|
|
} catch (e) {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.ok(e);
|
2019-06-18 14:17:14 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should abort the request when the custom timeout is hit', async function () {
|
2019-06-18 14:17:14 +02:00
|
|
|
nock('https://api.infura.io')
|
|
|
|
.get('/moon')
|
|
|
|
.delay(2000)
|
2021-02-04 19:15:23 +01:00
|
|
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}');
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const fetchWithTimeout = getFetchWithTimeout(123);
|
2019-06-18 14:17:14 +02:00
|
|
|
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await fetchWithTimeout('https://api.infura.io/moon').then((r) =>
|
|
|
|
r.json(),
|
|
|
|
);
|
|
|
|
assert.fail('Request should be aborted');
|
2019-06-18 14:17:14 +02:00
|
|
|
} catch (e) {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.deepEqual(e.message, 'Aborted');
|
2019-06-18 14:17:14 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-01-19 17:41:57 +01:00
|
|
|
|
|
|
|
it('throws on invalid timeout', async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.throws(() => getFetchWithTimeout(), 'should throw');
|
|
|
|
assert.throws(() => getFetchWithTimeout(-1), 'should throw');
|
|
|
|
assert.throws(() => getFetchWithTimeout({}), 'should throw');
|
|
|
|
assert.throws(() => getFetchWithTimeout(true), 'should throw');
|
|
|
|
});
|
|
|
|
});
|