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.
55 lines
1.3 KiB
55 lines
1.3 KiB
6 years ago
|
import assert from 'assert'
|
||
|
import nock from 'nock'
|
||
|
|
||
5 years ago
|
import fetchWithTimeout from '../../../app/scripts/lib/fetch-with-timeout'
|
||
6 years ago
|
|
||
5 years ago
|
describe('fetchWithTimeout', () => {
|
||
6 years ago
|
it('fetches a url', async () => {
|
||
|
nock('https://api.infura.io')
|
||
|
.get('/money')
|
||
|
.reply(200, '{"hodl": false}')
|
||
|
|
||
5 years ago
|
const fetch = fetchWithTimeout()
|
||
6 years ago
|
const response = await (await fetch('https://api.infura.io/money')).json()
|
||
|
assert.deepEqual(response, {
|
||
|
hodl: false,
|
||
|
})
|
||
|
})
|
||
|
|
||
|
it('throws when the request hits a custom timeout', async () => {
|
||
|
nock('https://api.infura.io')
|
||
|
.get('/moon')
|
||
|
.delay(2000)
|
||
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
|
||
|
|
||
5 years ago
|
const fetch = fetchWithTimeout({
|
||
6 years ago
|
timeout: 123,
|
||
|
})
|
||
|
|
||
|
try {
|
||
|
await fetch('https://api.infura.io/moon').then(r => r.json())
|
||
|
assert.fail('Request should throw')
|
||
|
} catch (e) {
|
||
|
assert.ok(e)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
it('should abort the request when the custom timeout is hit', async () => {
|
||
|
nock('https://api.infura.io')
|
||
|
.get('/moon')
|
||
|
.delay(2000)
|
||
|
.reply(200, '{"moon": "2012-12-21T11:11:11Z"}')
|
||
|
|
||
5 years ago
|
const fetch = fetchWithTimeout({
|
||
6 years ago
|
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')
|
||
|
}
|
||
|
})
|
||
|
})
|