Skip to content
Myr

res.redirect.js

This file

Path

test/res.redirect.js

198 lines · 5.2 KiB · JavaScript

Last changed by

Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f

History of this file

Take it

The walk stopped after 200 commits: lines older than 6bc7574 point at it.

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 1–108why, one step back
1
2var express = require('../')3 , request = require('supertest');45describe('res', function(){6 describe('.redirect(url)', function(){7 it('should default to a 302 redirect', function(done){8 var app = express();910 app.use(function(req, res){11 res.redirect('http://google.com');12 });1314 request(app)15 .get('/')16 .expect('location', 'http://google.com')17 .expect(302, done)18 })19 })2021 describe('.redirect(status, url)', function(){22 it('should set the response status', function(done){23 var app = express();2425 app.use(function(req, res){26 res.redirect(303, 'http://google.com');27 });2829 request(app)30 .get('/')31 .end(function(err, res){32 res.statusCode.should.equal(303);33 res.headers.should.have.property('location', 'http://google.com');34 done();35 })36 })37 })3839 describe('.redirect(url, status)', function(){40 it('should set the response status', function(done){41 var app = express();4243 app.use(function(req, res){44 res.redirect('http://google.com', 303);45 });4647 request(app)48 .get('/')49 .end(function(err, res){50 res.statusCode.should.equal(303);51 res.headers.should.have.property('location', 'http://google.com');52 done();53 })54 })55 })5657 describe('when the request method is HEAD', function(){58 it('should ignore the body', function(done){59 var app = express();6061 app.use(function(req, res){62 res.redirect('http://google.com');63 });6465 request(app)66 .head('/')67 .end(function(err, res){68 res.headers.should.have.property('location', 'http://google.com');69 res.text.should.equal('');70 done();71 })72 })73 })7475 describe('when accepting html', function(){76 it('should respond with html', function(done){77 var app = express();7879 app.use(function(req, res){80 res.redirect('http://google.com');81 });8283 request(app)84 .get('/')85 .set('Accept', 'text/html')86 .end(function(err, res){87 res.headers.should.have.property('location', 'http://google.com');88 res.text.should.equal('<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>');89 done();90 })91 })9293 it('should escape the url', function(done){94 var app = express();9596 app.use(function(req, res){97 res.redirect('<lame>');98 });99100 request(app)101 .get('/')102 .set('Host', 'http://example.com')103 .set('Accept', 'text/html')104 .end(function(err, res){105 res.text.should.equal('<p>Moved Temporarily. Redirecting to <a href="&lt;lame&gt;">&lt;lame&gt;</a></p>');106 done();107 })108 })

Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f

lines 109–123why, one step back
109
110 it('should include the redirect type', function(done){111 var app = express();112113 app.use(function(req, res){114 res.redirect(301, 'http://google.com');115 });116117 request(app)118 .get('/')119 .set('Accept', 'text/html')120 .expect('Content-Type', /html/)121 .expect('Location', 'http://google.com')122 .expect(301, '<p>Moved Permanently. Redirecting to <a href="http://google.com">http://google.com</a></p>', done);123 })

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 124–159why, one step back
124 })
125126 describe('when accepting text', function(){127 it('should respond with text', function(done){128 var app = express();129130 app.use(function(req, res){131 res.redirect('http://google.com');132 });133134 request(app)135 .get('/')136 .set('Accept', 'text/plain, */*')137 .end(function(err, res){138 res.headers.should.have.property('location', 'http://google.com');139 res.headers.should.have.property('content-length', '51');140 res.text.should.equal('Moved Temporarily. Redirecting to http://google.com');141 done();142 })143 })144145 it('should encode the url', function(done){146 var app = express();147148 app.use(function(req, res){149 res.redirect('http://example.com/?param=<script>alert("hax");</script>');150 });151152 request(app)153 .get('/')154 .set('Host', 'http://example.com')155 .set('Accept', 'text/plain, */*')156 .end(function(err, res){157 res.text.should.equal('Moved Temporarily. Redirecting to http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E');158 done();159 })

Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f

lines 160–174why, one step back
160 })
161162 it('should include the redirect type', function(done){163 var app = express();164165 app.use(function(req, res){166 res.redirect(301, 'http://google.com');167 });168169 request(app)170 .get('/')171 .set('Accept', 'text/plain, */*')172 .expect('Content-Type', /plain/)173 .expect('Location', 'http://google.com')174 .expect(301, 'Moved Permanently. Redirecting to http://google.com', done);

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 175–198why, one step back
175 })
176 })177178 describe('when accepting neither text or html', function(){179 it('should respond with an empty body', function(done){180 var app = express();181182 app.use(function(req, res){183 res.redirect('http://google.com');184 });185186 request(app)187 .get('/')188 .set('Accept', 'application/octet-stream')189 .expect('location', 'http://google.com')190 .expect('content-length', '0')191 .expect(302, '', function(err, res){192 if (err) return done(err)193 res.headers.should.not.have.property('content-type');194 done();195 })196 })197 })198})