2 var assert = require('assert')
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('.redirect(url, status)', function(){
65 it('should set the response status', function(done){
68 app.use(function(req, res){
69 res.redirect('http://google.com', 303);
74 .expect('Location', 'http://google.com')
79 describe('when the request method is HEAD', function(){
80 it('should ignore the body', function(done){
83 app.use(function(req, res){
84 res.redirect('http://google.com');
90 .expect('Location', 'http://google.com')
91 .expect(shouldNotHaveBody())
96 describe('when accepting html', function(){
97 it('should respond with html', function(done){
100 app.use(function(req, res){
101 res.redirect('http://google.com');
106 .set('Accept', 'text/html')
107 .expect('Content-Type', /html/)
108 .expect('Location', 'http://google.com')
109 .expect(302, '<p>Found. Redirecting to <a href="http://google.com">http://google.com</a></p>', done)
112 it('should escape the url', function(done){
115 app.use(function(req, res){
116 res.redirect('<la\'me>');
121 .set('Host', 'http://example.com')
122 .set('Accept', 'text/html')
123 .expect('Content-Type', /html/)
124 .expect('Location', '%3Cla\'me%3E')
125 .expect(302, '<p>Found. Redirecting to <a href="%3Cla'me%3E">%3Cla'me%3E</a></p>', done)
128 it('should include the redirect type', function(done){
131 app.use(function(req, res){
132 res.redirect(301, 'http://google.com');
137 .set('Accept', 'text/html')
138 .expect('Content-Type', /html/)
139 .expect('Location', 'http://google.com')
140 .expect(301, '<p>Moved Permanently. Redirecting to <a href="http://google.com">http://google.com</a></p>', done);
144 describe('when accepting text', function(){
145 it('should respond with text', function(done){
148 app.use(function(req, res){
149 res.redirect('http://google.com');
154 .set('Accept', 'text/plain, */*')
155 .expect('Content-Type', /plain/)
156 .expect('Location', 'http://google.com')
157 .expect(302, 'Found. Redirecting to http://google.com', done)
160 it('should encode the url', function(done){
163 app.use(function(req, res){
164 res.redirect('http://example.com/?param=<script>alert("hax");</script>');
169 .set('Host', 'http://example.com')
170 .set('Accept', 'text/plain, */*')
171 .expect('Content-Type', /plain/)
172 .expect('Location', 'http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E')
173 .expect(302, 'Found. Redirecting to http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E', done)
176 it('should include the redirect type', function(done){
179 app.use(function(req, res){
180 res.redirect(301, 'http://google.com');
185 .set('Accept', 'text/plain, */*')
186 .expect('Content-Type', /plain/)
187 .expect('Location', 'http://google.com')
188 .expect(301, 'Moved Permanently. Redirecting to http://google.com', done);
192 describe('when accepting neither text or html', function(){
193 it('should respond with an empty body', function(done){
196 app.use(function(req, res){
197 res.redirect('http://google.com');
202 .set('Accept', 'application/octet-stream')
204 .expect('location', 'http://google.com')
205 .expect('content-length', '0')
206 .expect(utils.shouldNotHaveHeader('Content-Type'))
207 .expect(shouldNotHaveBody())
213 function shouldNotHaveBody () {
214 return function (res) {
215 assert.ok(res.text === '' || res.text === undefined)