Added redirect() helper
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent b94d4b0 · Browse files · 3 files·+86 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+86 added−0 removed
- tests:+63 added−0 removed
- other:+23 added−0 removed
Provenance
- Commit
ff293e5- Tree
43f237e- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
b94d4b0
3 files changed+86 added−0 removed
added:lib/express/plugins/redirect.js+23 added
1234567891011121314151617181920212223
// Express - Redirect - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
exports.redirect = function(uri) { header('location', uri) halt(302)}
exports.Redirect = Plugin.extend({ init: function() { this.__super__.apply(arguments) process.mixin(GLOBAL, exports) }, on: { request: function(event) { home = set('home') || set('basepath') || '/' back = header('referrer') || header('referer') } }})No newline at end of filemodified:spec/spec.node.js+1 added
30 unmodified lines313233343536373830 unmodified lines .exec('spec/spec.plugins.js') .exec('spec/spec.plugins.common-logger.js') .exec('spec/spec.plugins.content-length.js') .exec('spec/spec.plugins.method-override.js') .exec('spec/spec.plugins.redirect.js') .exec('spec/spec.plugins.hooks.js')JSpec.run({ formatter: JSpec.formatters.Terminal, failuresOnly: true })JSpec.report()added:spec/spec.plugins.redirect.js+62 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
describe 'Express' before_each reset() use(require('express/plugins/redirect').Redirect) end describe 'Redirect' describe 'redirect()' it 'should set status to 302' get('/logout', function(){ redirect('/') }) get('/logout').status.should.eql 302 get('/logout').headers['location'].should.eql '/' end end describe 'redirect(back)' it 'should check Referrer header' get('/logout', function(){ redirect(back) }) var response = get('/logout', { headers: { referrer: '/login' }}) response.headers['location'].should.eql '/login' end it 'should check referer header' get('/logout', function(){ redirect(back) }) var response = get('/logout', { headers: { referer: '/login' }}) response.headers['location'].should.eql '/login' end end describe 'redirect(home)' it 'should check set("home")' set('home', '/login') get('/logout', function(){ redirect(home) }) get('/logout').headers['location'].should.eql '/login' end it 'should check set("basepath")' set('basepath', '/web/') get('/logout', function(){ redirect(home) }) get('/logout').headers['location'].should.eql '/web/' end it 'should default to "/"' get('/logout', function(){ redirect(home) }) get('/logout').headers['location'].should.eql '/' end end endendNo newline at end of file