aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/tests/src/server-helpers/request.ts
blob: f4b9af52e78fb15c683cc783642ab4d9b6650ef1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { pathExists, remove } from 'fs-extra/esm'
import { join } from 'path'
import { wait } from '@peertube/peertube-core-utils'
import { root } from '@peertube/peertube-node-utils'
import { doRequest, doRequestAndSaveToFile } from '@peertube/peertube-server/server/helpers/requests.js'
import { Mock429 } from '@tests/shared/mock-servers/mock-429.js'
import { FIXTURE_URLS } from '@tests/shared/tests.js'

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 {
      await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 3 })
    } 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 {
      await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath1, { bodyKBLimit: 3 })
    } 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')
  })

  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()
    await doRequest('http://127.0.0.1:' + port)

    expect(new Date().getTime() - before).to.be.greaterThan(2000)

    await mock.terminate()
  })

  it('Should succeed if the file is below the limit', async function () {
    await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 5 })
    await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath2, { bodyKBLimit: 5 })

    expect(await pathExists(destPath2)).to.be.true
  })

  after(async function () {
    await remove(destPath1)
    await remove(destPath2)
  })
})