2019-06-18 14:17:14 +02:00
|
|
|
import assert from 'assert'
|
|
|
|
import nock from 'nock'
|
|
|
|
|
2019-09-04 22:00:11 +02:00
|
|
|
import fetchWithTimeout from '../../../app/scripts/lib/fetch-with-timeout'
|
2019-06-18 14:17:14 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('fetchWithTimeout', function () {
|
|
|
|
it('fetches a url', async function () {
|
2019-06-18 14:17:14 +02:00
|
|
|
nock('https://api.infura.io')
|
|
|
|
.get('/money')
|
|
|
|
.reply(200, '{"hodl": false}')
|
|
|
|
|
2019-09-04 22:00:11 +02:00
|
|
|
const fetch = fetchWithTimeout()
|
2019-06-18 14:17:14 +02:00
|
|
|
const response = await (await fetch('https://api.infura.io/money')).json()
|
|
|
|
assert.deepEqual(response, {
|
|
|
|
hodl: false,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
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)
|
|
|
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
|
|
|
|
|
2019-09-04 22:00:11 +02:00
|
|
|
const fetch = fetchWithTimeout({
|
2019-06-18 14:17:14 +02:00
|
|
|
timeout: 123,
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fetch('https://api.infura.io/moon').then(r => r.json())
|
|
|
|
assert.fail('Request should throw')
|
|
|
|
} catch (e) {
|
|
|
|
assert.ok(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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)
|
|
|
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
|
|
|
|
|
2019-09-04 22:00:11 +02:00
|
|
|
const fetch = fetchWithTimeout({
|
2019-06-18 14:17:14 +02:00
|
|
|
timeout: 123,
|
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fetch('https://api.infura.io/moon').then(r => r.json())
|
|
|
|
assert.fail('Request should be aborted')
|
|
|
|
} catch (e) {
|
|
|
|
assert.deepEqual(e.message, 'Aborted')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|