ServeStaticWrapper.test.mjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { vi } from 'vitest'
  2. import { expect } from 'chai'
  3. import Path from 'node:path'
  4. import sinon from 'sinon'
  5. import MockResponse from '../helpers/MockResponse.js'
  6. import MockRequest from '../helpers/MockRequest.js'
  7. const modulePath = Path.join(
  8. import.meta.dirname,
  9. '../../../../app/src/infrastructure/ServeStaticWrapper'
  10. )
  11. describe('ServeStaticWrapperTests', function () {
  12. let error = null
  13. beforeEach(async function (ctx) {
  14. ctx.req = new MockRequest()
  15. ctx.res = new MockResponse()
  16. ctx.express = {
  17. static: () => (req, res, next) => {
  18. if (error) {
  19. next(error)
  20. } else {
  21. next()
  22. }
  23. },
  24. }
  25. vi.doMock('express', () => ({
  26. default: ctx.express,
  27. }))
  28. ctx.serveStaticWrapper = (await import(modulePath)).default
  29. })
  30. afterEach(() => {
  31. error = null
  32. })
  33. it('Premature close error thrown', async function (ctx) {
  34. error = new Error()
  35. error.code = 'ERR_STREAM_PREMATURE_CLOSE'
  36. const middleware = ctx.serveStaticWrapper('test_folder', {})
  37. const next = sinon.stub()
  38. middleware(ctx.req, ctx.res, next)
  39. expect(next.called).to.be.false
  40. })
  41. it('No error thrown', async function (ctx) {
  42. const middleware = ctx.serveStaticWrapper('test_folder', {})
  43. const next = sinon.stub()
  44. middleware(ctx.req, ctx.res, next)
  45. expect(next).to.be.calledWith()
  46. })
  47. it('Other error thrown', async function (ctx) {
  48. error = new Error()
  49. const middleware = ctx.serveStaticWrapper('test_folder', {})
  50. const next = sinon.stub()
  51. middleware(ctx.req, ctx.res, next)
  52. expect(next).to.be.calledWith(error)
  53. })
  54. })