]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/helpers/request.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / tests / helpers / request.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import { expect } from 'chai'
5 import { pathExists, remove } from 'fs-extra'
6 import { join } from 'path'
7 import { root, wait } from '@shared/core-utils'
8 import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
9 import { FIXTURE_URLS, Mock429 } from '../shared'
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 {
17 await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 3 })
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 {
27 await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath1, { bodyKBLimit: 3 })
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
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
52 it('Should succeed if the file is below the limit', async function () {
53 await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 5 })
54 await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath2, { bodyKBLimit: 5 })
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 })