fix(deps)!: mime-types@^3.0.0 (#5882)
[express.git] / test / res.redirect.js
blobd57f57f2f22852aaaed0d4b4fbca8079cd4e3384
1 'use strict'
3 var express = require('..');
4 var request = require('supertest');
5 var utils = require('./support/utils');
7 describe('res', function(){
8 describe('.redirect(url)', function(){
9 it('should default to a 302 redirect', function(done){
10 var app = express();
12 app.use(function(req, res){
13 res.redirect('http://google.com');
14 });
16 request(app)
17 .get('/')
18 .expect('location', 'http://google.com')
19 .expect(302, done)
22 it('should encode "url"', function (done) {
23 var app = express()
25 app.use(function (req, res) {
26 res.redirect('https://google.com?q=\u2603 ยง10')
29 request(app)
30 .get('/')
31 .expect('Location', 'https://google.com?q=%E2%98%83%20%C2%A710')
32 .expect(302, done)
35 it('should not touch already-encoded sequences in "url"', function (done) {
36 var app = express()
38 app.use(function (req, res) {
39 res.redirect('https://google.com?q=%A710')
42 request(app)
43 .get('/')
44 .expect('Location', 'https://google.com?q=%A710')
45 .expect(302, done)
49 describe('.redirect(status, url)', function(){
50 it('should set the response status', function(done){
51 var app = express();
53 app.use(function(req, res){
54 res.redirect(303, 'http://google.com');
55 });
57 request(app)
58 .get('/')
59 .expect('Location', 'http://google.com')
60 .expect(303, done)
64 describe('when the request method is HEAD', function(){
65 it('should ignore the body', function(done){
66 var app = express();
68 app.use(function(req, res){
69 res.redirect('http://google.com');
70 });
72 request(app)
73 .head('/')
74 .expect(302)
75 .expect('Location', 'http://google.com')
76 .expect(utils.shouldNotHaveBody())
77 .end(done)
81 describe('when accepting html', function(){
82 it('should respond with html', function(done){
83 var app = express();
85 app.use(function(req, res){
86 res.redirect('http://google.com');
87 });
89 request(app)
90 .get('/')
91 .set('Accept', 'text/html')
92 .expect('Content-Type', /html/)
93 .expect('Location', 'http://google.com')
94 .expect(302, '<p>Found. Redirecting to <a href="http://google.com">http://google.com</a></p>', done)
97 it('should escape the url', function(done){
98 var app = express();
100 app.use(function(req, res){
101 res.redirect('<la\'me>');
104 request(app)
105 .get('/')
106 .set('Host', 'http://example.com')
107 .set('Accept', 'text/html')
108 .expect('Content-Type', /html/)
109 .expect('Location', '%3Cla\'me%3E')
110 .expect(302, '<p>Found. Redirecting to <a href="%3Cla&#39;me%3E">%3Cla&#39;me%3E</a></p>', done)
113 it('should include the redirect type', function(done){
114 var app = express();
116 app.use(function(req, res){
117 res.redirect(301, 'http://google.com');
120 request(app)
121 .get('/')
122 .set('Accept', 'text/html')
123 .expect('Content-Type', /html/)
124 .expect('Location', 'http://google.com')
125 .expect(301, '<p>Moved Permanently. Redirecting to <a href="http://google.com">http://google.com</a></p>', done);
129 describe('when accepting text', function(){
130 it('should respond with text', function(done){
131 var app = express();
133 app.use(function(req, res){
134 res.redirect('http://google.com');
137 request(app)
138 .get('/')
139 .set('Accept', 'text/plain, */*')
140 .expect('Content-Type', /plain/)
141 .expect('Location', 'http://google.com')
142 .expect(302, 'Found. Redirecting to http://google.com', done)
145 it('should encode the url', function(done){
146 var app = express();
148 app.use(function(req, res){
149 res.redirect('http://example.com/?param=<script>alert("hax");</script>');
152 request(app)
153 .get('/')
154 .set('Host', 'http://example.com')
155 .set('Accept', 'text/plain, */*')
156 .expect('Content-Type', /plain/)
157 .expect('Location', 'http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E')
158 .expect(302, 'Found. Redirecting to http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E', done)
161 it('should include the redirect type', function(done){
162 var app = express();
164 app.use(function(req, res){
165 res.redirect(301, 'http://google.com');
168 request(app)
169 .get('/')
170 .set('Accept', 'text/plain, */*')
171 .expect('Content-Type', /plain/)
172 .expect('Location', 'http://google.com')
173 .expect(301, 'Moved Permanently. Redirecting to http://google.com', done);
177 describe('when accepting neither text or html', function(){
178 it('should respond with an empty body', function(done){
179 var app = express();
181 app.use(function(req, res){
182 res.redirect('http://google.com');
185 request(app)
186 .get('/')
187 .set('Accept', 'application/octet-stream')
188 .expect(302)
189 .expect('location', 'http://google.com')
190 .expect('content-length', '0')
191 .expect(utils.shouldNotHaveHeader('Content-Type'))
192 .expect(utils.shouldNotHaveBody())
193 .end(done)