Browse files

Added res.redirect().

  • Loading branch information...
1 parent b48c207 commit e0214da3c732ac962c6e54146c9b20794590389e @twentyrogersc committed May 15, 2012
Showing with 32 additions and 0 deletions.
  1. +7 −0 index.js
  2. +25 −0 test/teller.js
View
7 index.js
@@ -29,10 +29,17 @@ var json = function(obj, code) {
this.end(body)
}
+var redirect = function(url, code) {
+ code = code === undefined ? 302 : code
+ this.writeHead(code, { Location: url })
+ this.end()
+}
+
var server = function(req, res) {
res.json = json
res.render = render
+ res.redirect = redirect
req.url = url.parse(req.url)
crossroads.parse(req.url.pathname, [req, res])
}
View
25 test/teller.js
@@ -18,6 +18,9 @@ require('../index')
var data = { title: req.query.code }
res.render('template.html', data, req.query.code)
})
+ .get('/redirect', function(req, res) {
+ res.redirect('/render', req.query.code)
+ })
.setTemplateDir(__dirname)
.listen(1234)
@@ -91,5 +94,27 @@ describe('app', function() {
})
})
})
+
+ describe('res.redirect()', function() {
+ it('should redirect to to correct page', function(done) {
+ request('http://localhost:1234/redirect', function(err, res) {
+ should.not.exist(err)
+ should.exist(res)
+ res.request.uri.pathname.should.equal('/render')
+ done()
+ })
+ })
+ it('should redirect with correct status code', function(done) {
+ request({
+ followRedirect: false,
+ url: 'http://localhost:1234/redirect?code=303'
+ }, function(err, res) {
+ should.not.exist(err)
+ res.statusCode.should.equal(303)
+ res.headers.location.should.equal('/render')
+ done()
+ })
+ })
+ })
})

0 comments on commit e0214da

Please sign in to comment.