forked from expressjs/express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.options.js
115 lines (91 loc) · 2.75 KB
/
app.options.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var express = require('../')
, request = require('supertest');
describe('OPTIONS', function(){
it('should default to the routes defined', function(done){
var app = express();
app.del('/', function(){});
app.get('/users', function(req, res){});
app.put('/users', function(req, res){});
request(app)
.options('/users')
.expect('Allow', 'GET,HEAD,PUT')
.expect(200, 'GET,HEAD,PUT', done);
})
it('should only include each method once', function(done){
var app = express();
app.del('/', function(){});
app.get('/users', function(req, res){});
app.put('/users', function(req, res){});
app.get('/users', function(req, res){});
request(app)
.options('/users')
.expect('Allow', 'GET,HEAD,PUT')
.expect(200, 'GET,HEAD,PUT', done);
})
it('should not be affected by app.all', function(done){
var app = express();
app.get('/', function(){});
app.get('/users', function(req, res){});
app.put('/users', function(req, res){});
app.all('/users', function(req, res, next){
res.setHeader('x-hit', '1');
next();
});
request(app)
.options('/users')
.expect('x-hit', '1')
.expect('Allow', 'GET,HEAD,PUT')
.expect(200, 'GET,HEAD,PUT', done);
})
it('should not respond if the path is not defined', function(done){
var app = express();
app.get('/users', function(req, res){});
request(app)
.options('/other')
.expect(404, done);
})
it('should forward requests down the middleware chain', function(done){
var app = express();
var router = new express.Router();
router.get('/users', function(req, res){});
app.use(router);
app.get('/other', function(req, res){});
request(app)
.options('/other')
.expect('Allow', 'GET,HEAD')
.expect(200, 'GET,HEAD', done);
})
describe('when error occurs in response handler', function () {
it('should pass error to callback', function (done) {
var app = express();
var router = express.Router();
router.get('/users', function(req, res){});
app.use(function (req, res, next) {
res.writeHead(200);
next();
});
app.use(router);
app.use(function (err, req, res, next) {
res.end('true');
});
request(app)
.options('/users')
.expect(200, 'true', done)
})
})
})
describe('app.options()', function(){
it('should override the default behavior', function(done){
var app = express();
app.options('/users', function(req, res){
res.set('Allow', 'GET');
res.send('GET');
});
app.get('/users', function(req, res){});
app.put('/users', function(req, res){});
request(app)
.options('/users')
.expect('GET')
.expect('Allow', 'GET', done);
})
})