1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
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'
11 describe('Request helpers', function () {
12 const destPath1 = join(root(), 'test-output-1.txt')
13 const destPath2 = join(root(), 'test-output-2.txt')
15 it('Should throw an error when the bytes limit is exceeded for request', async function () {
17 await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 3 })
22 throw new Error('No error thrown by do request')
25 it('Should throw an error when the bytes limit is exceeded for request and save file', async function () {
27 await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath1, { bodyKBLimit: 3 })
31 expect(await pathExists(destPath1)).to.be.false
35 throw new Error('No error thrown by do request and save to file')
38 it('Should correctly retry on 429 error', async function () {
41 const mock = new Mock429()
42 const port = await mock.initialize()
44 const before = new Date().getTime()
45 await doRequest('http://localhost:' + port)
47 expect(new Date().getTime() - before).to.be.greaterThan(2000)
49 await mock.terminate()
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 })
56 expect(await pathExists(destPath2)).to.be.true
59 after(async function () {
60 await remove(destPath1)
61 await remove(destPath2)