error-sender.spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {Writable} from 'stream';
  2. import {expect} from 'chai';
  3. import HttpErrors from 'http-errors';
  4. import {ErrorSender} from './error-sender.js';
  5. import {createRequestMock} from '../utils/index.js';
  6. import {createResponseMock} from '../utils/index.js';
  7. import {EXPOSED_ERROR_PROPERTIES} from './error-sender.js';
  8. describe('ErrorSender', function () {
  9. describe('send', function () {
  10. it('sends error as utf-8 JSON', function (done) {
  11. const error = HttpErrors.Unauthorized();
  12. const req = createRequestMock();
  13. const res = createResponseMock();
  14. const writable = new Writable();
  15. const chunks = [];
  16. writable._write = function (chunk, encoding, done) {
  17. chunks.push(chunk);
  18. done();
  19. };
  20. writable._final = function (callback) {
  21. const json = Buffer.concat(chunks).toString('utf-8');
  22. const data = JSON.parse(json);
  23. expect(data).to.be.eql({error: {message: 'Unauthorized'}});
  24. expect(res.statusCode).to.be.eq(401);
  25. const ct = res.getHeader('content-type');
  26. expect(ct).to.be.eq('application/json; charset=utf-8');
  27. callback();
  28. done();
  29. };
  30. res.pipe(writable);
  31. const s = new ErrorSender();
  32. s.send(req, res, error);
  33. });
  34. it('exposes only specified properties of the given error', function (done) {
  35. const error = HttpErrors.Unauthorized();
  36. EXPOSED_ERROR_PROPERTIES.forEach(name => (error[name] = name));
  37. error.shouldNotBeExposedProp = 'shouldNotBeExposedProp';
  38. const req = createRequestMock();
  39. const res = createResponseMock();
  40. const writable = new Writable();
  41. const chunks = [];
  42. writable._write = function (chunk, encoding, done) {
  43. chunks.push(chunk);
  44. done();
  45. };
  46. writable._final = function (callback) {
  47. const json = Buffer.concat(chunks).toString('utf-8');
  48. const data = JSON.parse(json);
  49. const expectedData = {error: {message: 'Unauthorized'}};
  50. EXPOSED_ERROR_PROPERTIES.forEach(
  51. name => (expectedData.error[name] = name),
  52. );
  53. expect(data.error).not.to.have.property('shouldNotBeExposedProp');
  54. expect(data).to.be.eql(expectedData);
  55. expect(res.statusCode).to.be.eq(401);
  56. const ct = res.getHeader('content-type');
  57. expect(ct).to.be.eq('application/json; charset=utf-8');
  58. callback();
  59. done();
  60. };
  61. res.pipe(writable);
  62. const s = new ErrorSender();
  63. s.send(req, res, error);
  64. });
  65. });
  66. describe('send404', function () {
  67. it('sends plain text', function (done) {
  68. const req = createRequestMock();
  69. const res = createResponseMock();
  70. const writable = new Writable();
  71. const chunks = [];
  72. writable._write = function (chunk, encoding, done) {
  73. chunks.push(chunk);
  74. done();
  75. };
  76. writable._final = function (callback) {
  77. const body = Buffer.concat(chunks).toString('utf-8');
  78. expect(body).to.be.eql('404 Not Found');
  79. expect(res.statusCode).to.be.eq(404);
  80. const ct = res.getHeader('content-type');
  81. expect(ct).to.be.eq('text/plain; charset=utf-8');
  82. callback();
  83. done();
  84. };
  85. res.pipe(writable);
  86. const s = new ErrorSender();
  87. s.send404(req, res);
  88. });
  89. });
  90. });