docs: update homepage link http to https (#5920)
[express.git] / test / app.route.js
bloba0c8696e509678b9f6c827eeb2be59b790aa71d5
1 'use strict'
3 var express = require('../');
4 var request = require('supertest');
6 var describePromises = global.Promise ? describe : describe.skip
8 describe('app.route', function(){
9 it('should return a new route', function(done){
10 var app = express();
12 app.route('/foo')
13 .get(function(req, res) {
14 res.send('get');
16 .post(function(req, res) {
17 res.send('post');
18 });
20 request(app)
21 .post('/foo')
22 .expect('post', done);
23 });
25 it('should all .VERB after .all', function(done){
26 var app = express();
28 app.route('/foo')
29 .all(function(req, res, next) {
30 next();
32 .get(function(req, res) {
33 res.send('get');
35 .post(function(req, res) {
36 res.send('post');
37 });
39 request(app)
40 .post('/foo')
41 .expect('post', done);
42 });
44 it('should support dynamic routes', function(done){
45 var app = express();
47 app.route('/:foo')
48 .get(function(req, res) {
49 res.send(req.params.foo);
50 });
52 request(app)
53 .get('/test')
54 .expect('test', done);
55 });
57 it('should not error on empty routes', function(done){
58 var app = express();
60 app.route('/:foo');
62 request(app)
63 .get('/test')
64 .expect(404, done);
65 });
67 describePromises('promise support', function () {
68 it('should pass rejected promise value', function (done) {
69 var app = express()
70 var route = app.route('/foo')
72 route.all(function createError (req, res, next) {
73 return Promise.reject(new Error('boom!'))
76 route.all(function helloWorld (req, res) {
77 res.send('hello, world!')
80 route.all(function handleError (err, req, res, next) {
81 res.status(500)
82 res.send('caught: ' + err.message)
85 request(app)
86 .get('/foo')
87 .expect(500, 'caught: boom!', done)
90 it('should pass rejected promise without value', function (done) {
91 var app = express()
92 var route = app.route('/foo')
94 route.all(function createError (req, res, next) {
95 return Promise.reject()
98 route.all(function helloWorld (req, res) {
99 res.send('hello, world!')
102 route.all(function handleError (err, req, res, next) {
103 res.status(500)
104 res.send('caught: ' + err.message)
107 request(app)
108 .get('/foo')
109 .expect(500, 'caught: Rejected promise', done)
112 it('should ignore resolved promise', function (done) {
113 var app = express()
114 var route = app.route('/foo')
116 route.all(function createError (req, res, next) {
117 res.send('saw GET /foo')
118 return Promise.resolve('foo')
121 route.all(function () {
122 done(new Error('Unexpected route invoke'))
125 request(app)
126 .get('/foo')
127 .expect(200, 'saw GET /foo', done)
130 describe('error handling', function () {
131 it('should pass rejected promise value', function (done) {
132 var app = express()
133 var route = app.route('/foo')
135 route.all(function createError (req, res, next) {
136 return Promise.reject(new Error('boom!'))
139 route.all(function handleError (err, req, res, next) {
140 return Promise.reject(new Error('caught: ' + err.message))
143 route.all(function handleError (err, req, res, next) {
144 res.status(500)
145 res.send('caught again: ' + err.message)
148 request(app)
149 .get('/foo')
150 .expect(500, 'caught again: caught: boom!', done)
153 it('should pass rejected promise without value', function (done) {
154 var app = express()
155 var route = app.route('/foo')
157 route.all(function createError (req, res, next) {
158 return Promise.reject(new Error('boom!'))
161 route.all(function handleError (err, req, res, next) {
162 return Promise.reject()
165 route.all(function handleError (err, req, res, next) {
166 res.status(500)
167 res.send('caught again: ' + err.message)
170 request(app)
171 .get('/foo')
172 .expect(500, 'caught again: Rejected promise', done)
175 it('should ignore resolved promise', function (done) {
176 var app = express()
177 var route = app.route('/foo')
179 route.all(function createError (req, res, next) {
180 return Promise.reject(new Error('boom!'))
183 route.all(function handleError (err, req, res, next) {
184 res.status(500)
185 res.send('caught: ' + err.message)
186 return Promise.resolve('foo')
189 route.all(function () {
190 done(new Error('Unexpected route invoke'))
193 request(app)
194 .get('/foo')
195 .expect(500, 'caught: boom!', done)