request.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import { pathExists, remove } from 'fs-extra'
  4. import { join } from 'path'
  5. import { root, wait } from '@shared/core-utils'
  6. import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
  7. import { FIXTURE_URLS, Mock429 } from '../shared'
  8. describe('Request helpers', function () {
  9. const destPath1 = join(root(), 'test-output-1.txt')
  10. const destPath2 = join(root(), 'test-output-2.txt')
  11. it('Should throw an error when the bytes limit is exceeded for request', async function () {
  12. try {
  13. await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 3 })
  14. } catch {
  15. return
  16. }
  17. throw new Error('No error thrown by do request')
  18. })
  19. it('Should throw an error when the bytes limit is exceeded for request and save file', async function () {
  20. try {
  21. await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath1, { bodyKBLimit: 3 })
  22. } catch {
  23. await wait(500)
  24. expect(await pathExists(destPath1)).to.be.false
  25. return
  26. }
  27. throw new Error('No error thrown by do request and save to file')
  28. })
  29. it('Should correctly retry on 429 error', async function () {
  30. this.timeout(25000)
  31. const mock = new Mock429()
  32. const port = await mock.initialize()
  33. const before = new Date().getTime()
  34. await doRequest('http://127.0.0.1:' + port)
  35. expect(new Date().getTime() - before).to.be.greaterThan(2000)
  36. await mock.terminate()
  37. })
  38. it('Should succeed if the file is below the limit', async function () {
  39. await doRequest(FIXTURE_URLS.file4K, { bodyKBLimit: 5 })
  40. await doRequestAndSaveToFile(FIXTURE_URLS.file4K, destPath2, { bodyKBLimit: 5 })
  41. expect(await pathExists(destPath2)).to.be.true
  42. })
  43. after(async function () {
  44. await remove(destPath1)
  45. await remove(destPath2)
  46. })
  47. })