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