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
){
12 app
.use(function(req
, res
){
13 res
.redirect('http://google.com');
18 .expect('location', 'http://google.com')
22 it('should encode "url"', function (done
) {
25 app
.use(function (req
, res
) {
26 res
.redirect('https://google.com?q=\u2603 ยง10')
31 .expect('Location', 'https://google.com?q=%E2%98%83%20%C2%A710')
35 it('should not touch already-encoded sequences in "url"', function (done
) {
38 app
.use(function (req
, res
) {
39 res
.redirect('https://google.com?q=%A710')
44 .expect('Location', 'https://google.com?q=%A710')
49 describe('.redirect(status, url)', function(){
50 it('should set the response status', function(done
){
53 app
.use(function(req
, res
){
54 res
.redirect(303, 'http://google.com');
59 .expect('Location', 'http://google.com')
64 describe('when the request method is HEAD', function(){
65 it('should ignore the body', function(done
){
68 app
.use(function(req
, res
){
69 res
.redirect('http://google.com');
75 .expect('Location', 'http://google.com')
76 .expect(utils
.shouldNotHaveBody())
81 describe('when accepting html', function(){
82 it('should respond with html', function(done
){
85 app
.use(function(req
, res
){
86 res
.redirect('http://google.com');
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
){
100 app
.use(function(req
, res
){
101 res
.redirect('<la\'me>');
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'me%3E">%3Cla'me%3E</a></p>', done
)
113 it('should include the redirect type', function(done
){
116 app
.use(function(req
, res
){
117 res
.redirect(301, 'http://google.com');
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
){
133 app
.use(function(req
, res
){
134 res
.redirect('http://google.com');
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
){
148 app
.use(function(req
, res
){
149 res
.redirect('http://example.com/?param=<script>alert("hax");</script>');
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
){
164 app
.use(function(req
, res
){
165 res
.redirect(301, 'http://google.com');
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
){
181 app
.use(function(req
, res
){
182 res
.redirect('http://google.com');
187 .set('Accept', 'application/octet-stream')
189 .expect('location', 'http://google.com')
190 .expect('content-length', '0')
191 .expect(utils
.shouldNotHaveHeader('Content-Type'))
192 .expect(utils
.shouldNotHaveBody())