2020-01-31 16:56:52 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
2019-02-21 17:19:16 +01:00
|
|
|
|
|
|
|
import { expect } from 'chai'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { pathExists, remove } from 'fs-extra/esm'
|
2021-03-08 14:24:11 +01:00
|
|
|
import { join } from 'path'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { wait } from '@peertube/peertube-core-utils'
|
|
|
|
import { root } from '@peertube/peertube-node-utils'
|
2023-10-04 15:13:25 +02:00
|
|
|
import { doRequest, doRequestAndSaveToFile } from '@peertube/peertube-server/core/helpers/requests.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { Mock429 } from '@tests/shared/mock-servers/mock-429.js'
|
|
|
|
import { FIXTURE_URLS } from '@tests/shared/tests.js'
|
2019-02-21 17:19:16 +01:00
|
|
|
|
|
|
|
describe('Request helpers', function () {
|
|
|
|
const destPath1 = join(root(), 'test-output-1.txt')
|
|
|
|
const destPath2 = join(root(), 'test-output-2.txt')
|
|
|
|
|
|
|
|
it('Should throw an error when the bytes limit is exceeded for request', async function () {
|
|
|
|
try {
|
2021-08-17 08:26:20 +02:00
|
|
|
await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 3 })
|
2019-02-21 17:19:16 +01:00
|
|
|
} catch {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('No error thrown by do request')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should throw an error when the bytes limit is exceeded for request and save file', async function () {
|
|
|
|
try {
|
2021-08-17 08:26:20 +02:00
|
|
|
await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath1, { bodyKBLimit: 3 })
|
2019-02-21 17:19:16 +01:00
|
|
|
} catch {
|
|
|
|
|
|
|
|
await wait(500)
|
|
|
|
expect(await pathExists(destPath1)).to.be.false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('No error thrown by do request and save to file')
|
|
|
|
})
|
|
|
|
|
2021-11-16 11:17:52 +01:00
|
|
|
it('Should correctly retry on 429 error', async function () {
|
|
|
|
this.timeout(25000)
|
|
|
|
|
|
|
|
const mock = new Mock429()
|
|
|
|
const port = await mock.initialize()
|
|
|
|
|
|
|
|
const before = new Date().getTime()
|
2022-12-09 11:14:47 +01:00
|
|
|
await doRequest('http://127.0.0.1:' + port)
|
2021-11-16 11:17:52 +01:00
|
|
|
|
|
|
|
expect(new Date().getTime() - before).to.be.greaterThan(2000)
|
|
|
|
|
|
|
|
await mock.terminate()
|
|
|
|
})
|
|
|
|
|
2019-02-21 17:19:16 +01:00
|
|
|
it('Should succeed if the file is below the limit', async function () {
|
2021-08-17 08:26:20 +02:00
|
|
|
await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 5 })
|
|
|
|
await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath2, { bodyKBLimit: 5 })
|
2019-02-21 17:19:16 +01:00
|
|
|
|
|
|
|
expect(await pathExists(destPath2)).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
after(async function () {
|
|
|
|
await remove(destPath1)
|
|
|
|
await remove(destPath2)
|
|
|
|
})
|
|
|
|
})
|