ServeStaticWrapper.test.mjs 1.6 KB

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