res.redirect() supporting text/plain and text/html via Accept
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 2a40cec · Browse files · 2 files·+30 added−16 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+30 added−16 removed
- tests:+19 added−11 removed
- other:+11 added−5 removed
Provenance
- Commit
0bc7e24- Tree
2323bf5- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
2a40cec
2 files changed+30 added−16 removed
modified:lib/express/response.js+11 added-5 removed
333 unmodified lines334 * @api public335 */336 337 http.ServerResponse.prototype.redirect = function(url, status){338- var basePath = this.app.set('home') || '/';338+ var body, basePath = this.app.set('home') || '/';339 340 // Setup redirect map341 var map = {342 back: this.req.headers.referrer || this.req.headers.referer || basePath,10 unmodified lines353 354 // Perform redirect355 url = mapped || url;356 357+ // Support text/{plain,html} by default358+ if (this.req.accepts('html')) {359+ body = '<p>Redirecting to <a href="' + url + '">' + url + '</a></p>';360+ this.header('Content-Type', 'text/html');361+ } else {362+ body = 'Redirecting to ' + url;363+ this.header('Content-Type', 'text/plain');364+ }365+366 // Respond358- this.send('Redirecting to ' + url, {359- 'Content-Type': 'text/plain'360- , 'Location': url361- }, status || 302);367+ this.send(body, { Location: url }, status || 302);368 };modified:test/response.test.js+19 added-11 removed
146 unmodified lines147 app.get('/home', function(req, res){148 res.redirect('home');149 });150 151+ app.get('/html', function(req, res){152+ res.redirect('http://google.com');153+ });154+ 155 app2.get('/', function(req, res){156 res.redirect('http://google.com', 301);157 });158 12 unmodified lines171 app2.get('/user/:id', function(req, res){172 res.header('X-Foo', 'bar');173 res.redirect('blog');174 });171- 175+176+177 assert.response(app,173- { url: '/' },178+ { url: '/html', headers: { Accept: 'text/html,text/plain' }},179+ { body: '<p>Redirecting to <a href="http://google.com">http://google.com</a></p>' });180+ assert.response(app,181+ { url: '/', headers: { Accept: 'text/plain' }},182 { body: 'Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }});183 assert.response(app,176- { url: '/back' },184+ { url: '/back', headers: { Accept: 'text/plain' }},185 { body: 'Redirecting to /', status: 302, headers: { Location: '/', 'Content-Type': 'text/plain' }});186 assert.response(app,179- { url: '/back', headers: { Referer: '/foo' }},187+ { url: '/back', headers: { Referer: '/foo', Accept: 'text/plain' }},188 { body: 'Redirecting to /foo', status: 302, headers: { Location: '/foo' }});189 assert.response(app,182- { url: '/back', headers: { Referrer: '/foo' }},190+ { url: '/back', headers: { Referrer: '/foo', Accept: 'text/plain' }},191 { body: 'Redirecting to /foo', status: 302, headers: { Location: '/foo' }});192 assert.response(app,185- { url: '/home' },193+ { url: '/home', headers: { Accept: 'text/plain' } },194 { body: 'Redirecting to /', status: 302, headers: { Location: '/' }});195 196 assert.response(app2,189- { url: '/' },197+ { url: '/', headers: { Accept: 'text/plain' }},198 { body: 'Redirecting to http://google.com', status: 301, headers: { Location: 'http://google.com' }});199 assert.response(app2,192- { url: '/back' },200+ { url: '/back', headers: { Accept: 'text/plain' }},201 { body: 'Redirecting to /blog', status: 302, headers: { Location: '/blog' }});202 assert.response(app2,195- { url: '/home' },203+ { url: '/home', headers: { Accept: 'text/plain' }},204 { body: 'Redirecting to /blog', status: 302, headers: { Location: '/blog' }});205 assert.response(app2,198- { url: '/google' },206+ { url: '/google', headers: { Accept: 'text/plain' }},207 { body: 'Redirecting to http://google.com', headers: { Location: 'http://google.com' }});208 assert.response(app2,201- { url: '/user/12' },209+ { url: '/user/12', headers: { Accept: 'text/plain' }},210 { body: 'Redirecting to /user/12/blog', headers: { Location: '/user/12/blog', 'X-Foo': 'bar' }});211 },212 213 'test #sendfile()': function(assert){195 unmodified lines