Added tests for res.redirect()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent c77ecbb · Browse files · 2 files·+62 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+62 added−5 removed
- tests:+58 added−0 removed
- other:+4 added−5 removed
Provenance
- Commit
7c2673f- Tree
8237f17- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
c77ecbb
2 files changed+62 added−5 removed
modified:lib/express/response.js+4 added-5 removed
130 unmodified linesExpand all131132133134135136135136137138140141139140141142143144146145146147148130 unmodified linesExpand all : this.headers[name] = val;};
http.ServerResponse.prototype.redirect = function(url, code){ var home = this.app.set('home') || '/', basePath = this.app.set('basepath'); var basePath = this.app.set('home') || '/';
// Setup redirect map var map = { back: req.headers.referrer || req.headers.referer || home, home: home back: this.req.headers.referrer || this.req.headers.referer || basePath, home: basePath };
// Perform redirect this.writeHead(code || 302, { 'Location': map[url] || basePath || url 'Location': map[url] || url }); this.end();};modified:test/response.test.js+58 added
88 unmodified lines89 { body: 'whatever', headers: { 'Content-Disposition': 'attachment; filename="jquery.js"' }});90 assert.response(app,91 { url: '/style.css' },92 { body: 'some stylezzz', headers: { 'Content-Disposition': 'attachment' }});93+ },94+ 95+ '#redirect()': function(assert){96+ var app = express.createServer(),97+ app2 = express.createServer();98+ 99+ app2.set('home', '/blog');100+101+ app.get('/', function(req, res){102+ res.redirect('http://google.com', 301);103+ });104+ 105+ app.get('/back', function(req, res){106+ res.redirect('back');107+ });108+ 109+ app.get('/home', function(req, res){110+ res.redirect('home');111+ });112+ 113+ app2.get('/', function(req, res){114+ res.redirect('http://google.com', 301);115+ });116+ 117+ app2.get('/back', function(req, res){118+ res.redirect('back');119+ });120+ 121+ app2.get('/home', function(req, res){122+ res.redirect('home');123+ });124+ 125+ assert.response(app,126+ { url: '/' },127+ { body: '', status: 301, headers: { Location: 'http://google.com' }});128+ assert.response(app,129+ { url: '/back' },130+ { body: '', status: 302, headers: { Location: '/' }});131+ assert.response(app,132+ { url: '/back', headers: { Referer: '/foo' }},133+ { body: '', status: 302, headers: { Location: '/foo' }});134+ assert.response(app,135+ { url: '/back', headers: { Referrer: '/foo' }},136+ { body: '', status: 302, headers: { Location: '/foo' }});137+ assert.response(app,138+ { url: '/home' },139+ { body: '', status: 302, headers: { Location: '/' }});140+141+ assert.response(app2,142+ { url: '/' },143+ { body: '', status: 301, headers: { Location: 'http://google.com' }});144+ assert.response(app2,145+ { url: '/back' },146+ { body: '', status: 302, headers: { Location: '/blog' }});147+ assert.response(app2,148+ { url: '/home' },149+ { body: '', status: 302, headers: { Location: '/blog' }});150+ 151 }152 };