ServeStaticWrapperTests.mjs 1.7 KB

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