diff options
Diffstat (limited to 'packages/tests/src/server-helpers/request.ts')
-rw-r--r-- | packages/tests/src/server-helpers/request.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/packages/tests/src/server-helpers/request.ts b/packages/tests/src/server-helpers/request.ts new file mode 100644 index 000000000..f4b9af52e --- /dev/null +++ b/packages/tests/src/server-helpers/request.ts | |||
@@ -0,0 +1,64 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { pathExists, remove } from 'fs-extra/esm' | ||
5 | import { join } from 'path' | ||
6 | import { wait } from '@peertube/peertube-core-utils' | ||
7 | import { root } from '@peertube/peertube-node-utils' | ||
8 | import { doRequest, doRequestAndSaveToFile } from '@peertube/peertube-server/server/helpers/requests.js' | ||
9 | import { Mock429 } from '@tests/shared/mock-servers/mock-429.js' | ||
10 | import { FIXTURE_URLS } from '@tests/shared/tests.js' | ||
11 | |||
12 | describe('Request helpers', function () { | ||
13 | const destPath1 = join(root(), 'test-output-1.txt') | ||
14 | const destPath2 = join(root(), 'test-output-2.txt') | ||
15 | |||
16 | it('Should throw an error when the bytes limit is exceeded for request', async function () { | ||
17 | try { | ||
18 | await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 3 }) | ||
19 | } catch { | ||
20 | return | ||
21 | } | ||
22 | |||
23 | throw new Error('No error thrown by do request') | ||
24 | }) | ||
25 | |||
26 | it('Should throw an error when the bytes limit is exceeded for request and save file', async function () { | ||
27 | try { | ||
28 | await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath1, { bodyKBLimit: 3 }) | ||
29 | } catch { | ||
30 | |||
31 | await wait(500) | ||
32 | expect(await pathExists(destPath1)).to.be.false | ||
33 | return | ||
34 | } | ||
35 | |||
36 | throw new Error('No error thrown by do request and save to file') | ||
37 | }) | ||
38 | |||
39 | it('Should correctly retry on 429 error', async function () { | ||
40 | this.timeout(25000) | ||
41 | |||
42 | const mock = new Mock429() | ||
43 | const port = await mock.initialize() | ||
44 | |||
45 | const before = new Date().getTime() | ||
46 | await doRequest('http://127.0.0.1:' + port) | ||
47 | |||
48 | expect(new Date().getTime() - before).to.be.greaterThan(2000) | ||
49 | |||
50 | await mock.terminate() | ||
51 | }) | ||
52 | |||
53 | it('Should succeed if the file is below the limit', async function () { | ||
54 | await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 5 }) | ||
55 | await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath2, { bodyKBLimit: 5 }) | ||
56 | |||
57 | expect(await pathExists(destPath2)).to.be.true | ||
58 | }) | ||
59 | |||
60 | after(async function () { | ||
61 | await remove(destPath1) | ||
62 | await remove(destPath2) | ||
63 | }) | ||
64 | }) | ||