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