]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
bfe2ef6b | 2 | |
bfe2ef6b | 3 | import { expect } from 'chai' |
db4b15f2 C |
4 | import { pathExists, remove } from 'fs-extra' |
5 | import { join } from 'path' | |
c55e3d72 | 6 | import { root, wait } from '@shared/core-utils' |
db4b15f2 | 7 | import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' |
c55e3d72 | 8 | import { FIXTURE_URLS, Mock429 } from '../shared' |
bfe2ef6b C |
9 | |
10 | describe('Request helpers', function () { | |
11 | const destPath1 = join(root(), 'test-output-1.txt') | |
12 | const destPath2 = join(root(), 'test-output-2.txt') | |
13 | ||
14 | it('Should throw an error when the bytes limit is exceeded for request', async function () { | |
15 | try { | |
0305db28 | 16 | await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 3 }) |
bfe2ef6b C |
17 | } catch { |
18 | return | |
19 | } | |
20 | ||
21 | throw new Error('No error thrown by do request') | |
22 | }) | |
23 | ||
24 | it('Should throw an error when the bytes limit is exceeded for request and save file', async function () { | |
25 | try { | |
0305db28 | 26 | await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath1, { bodyKBLimit: 3 }) |
bfe2ef6b C |
27 | } catch { |
28 | ||
29 | await wait(500) | |
30 | expect(await pathExists(destPath1)).to.be.false | |
31 | return | |
32 | } | |
33 | ||
34 | throw new Error('No error thrown by do request and save to file') | |
35 | }) | |
36 | ||
3455c265 C |
37 | it('Should correctly retry on 429 error', async function () { |
38 | this.timeout(25000) | |
39 | ||
40 | const mock = new Mock429() | |
41 | const port = await mock.initialize() | |
42 | ||
43 | const before = new Date().getTime() | |
44 | await doRequest('http://localhost:' + port) | |
45 | ||
46 | expect(new Date().getTime() - before).to.be.greaterThan(2000) | |
47 | ||
48 | await mock.terminate() | |
49 | }) | |
50 | ||
bfe2ef6b | 51 | it('Should succeed if the file is below the limit', async function () { |
0305db28 JB |
52 | await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 5 }) |
53 | await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath2, { bodyKBLimit: 5 }) | |
bfe2ef6b C |
54 | |
55 | expect(await pathExists(destPath2)).to.be.true | |
56 | }) | |
57 | ||
58 | after(async function () { | |
59 | await remove(destPath1) | |
60 | await remove(destPath2) | |
61 | }) | |
62 | }) |