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