error-response.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. (function () {
  2. /*global describe, it*/
  3. 'use strict';
  4. var should = require('should'),
  5. express = require('express'),
  6. supertest = require('supertest'),
  7. cors = require('../lib');
  8. var app;
  9. /* -------------------------------------------------------------------------- */
  10. app = express();
  11. app.use(cors());
  12. app.post('/five-hundred', function (req, res, next) {
  13. next(new Error('nope'));
  14. });
  15. app.post('/four-oh-one', function (req, res, next) {
  16. next(new Error('401'));
  17. });
  18. app.post('/four-oh-four', function (req, res, next) {
  19. next();
  20. });
  21. app.use(function (err, req, res, next) {
  22. if (err.message === '401') {
  23. res.status(401).send('unauthorized');
  24. } else {
  25. next(err);
  26. }
  27. });
  28. /* -------------------------------------------------------------------------- */
  29. describe('error response', function () {
  30. it('500', function (done) {
  31. supertest(app)
  32. .post('/five-hundred')
  33. .expect(500)
  34. .end(function (err, res) {
  35. should.not.exist(err);
  36. res.headers['access-control-allow-origin'].should.eql('*');
  37. res.text.should.containEql('Error: nope');
  38. done();
  39. });
  40. });
  41. it('401', function (done) {
  42. supertest(app)
  43. .post('/four-oh-one')
  44. .expect(401)
  45. .end(function (err, res) {
  46. should.not.exist(err);
  47. res.headers['access-control-allow-origin'].should.eql('*');
  48. res.text.should.eql('unauthorized');
  49. done();
  50. });
  51. });
  52. it('404', function (done) {
  53. supertest(app)
  54. .post('/four-oh-four')
  55. .expect(404)
  56. .end(function (err, res) {
  57. should.not.exist(err);
  58. res.headers['access-control-allow-origin'].should.eql('*');
  59. done();
  60. });
  61. });
  62. });
  63. }());