Files
express/test/res.redirect.js

169 lines
4.3 KiB
JavaScript
Raw Normal View History

2011-11-08 18:36:27 -08:00
var express = require('../')
2014-03-05 22:06:14 -08:00
, request = require('supertest');
2011-11-08 18:36:27 -08:00
describe('res', function(){
2011-11-08 18:40:01 -08:00
describe('.redirect(url)', function(){
2011-11-08 18:36:27 -08:00
it('should default to a 302 redirect', function(done){
var app = express();
app.use(function(req, res){
res.redirect('http://google.com');
});
request(app)
.get('/')
2014-05-29 22:53:59 -04:00
.expect('location', 'http://google.com')
.expect(302, done)
2011-11-08 18:36:27 -08:00
})
})
2011-11-08 18:46:23 -08:00
2011-11-08 18:40:01 -08:00
describe('.redirect(status, url)', function(){
it('should set the response status', function(done){
var app = express();
app.use(function(req, res){
res.redirect(303, 'http://google.com');
});
request(app)
.get('/')
2012-06-26 17:14:07 -07:00
.end(function(err, res){
2011-11-08 18:40:01 -08:00
res.statusCode.should.equal(303);
res.headers.should.have.property('location', 'http://google.com');
done();
})
})
})
describe('.redirect(url, status)', function(){
it('should set the response status', function(done){
var app = express();
app.use(function(req, res){
res.redirect('http://google.com', 303);
});
request(app)
.get('/')
.end(function(err, res){
res.statusCode.should.equal(303);
res.headers.should.have.property('location', 'http://google.com');
done();
})
})
})
2011-11-08 18:40:01 -08:00
describe('when the request method is HEAD', function(){
it('should ignore the body', function(done){
var app = express();
app.use(function(req, res){
res.redirect('http://google.com');
});
request(app)
.head('/')
2012-06-26 17:14:07 -07:00
.end(function(err, res){
2011-11-08 18:40:01 -08:00
res.headers.should.have.property('location', 'http://google.com');
2012-06-26 17:14:07 -07:00
res.text.should.equal('');
2011-11-08 18:40:01 -08:00
done();
})
})
})
2013-02-24 18:01:50 -05:00
describe('when accepting html', function(){
it('should respond with html', function(done){
var app = express();
app.use(function(req, res){
res.redirect('http://google.com');
});
request(app)
.get('/')
.set('Accept', 'text/html')
2012-06-26 17:14:07 -07:00
.end(function(err, res){
res.headers.should.have.property('location', 'http://google.com');
2012-06-26 17:14:07 -07:00
res.text.should.equal('<p>Moved Temporarily. Redirecting to <a href="http://google.com">http://google.com</a></p>');
done();
})
})
2012-08-02 19:41:37 -07:00
it('should escape the url', function(done){
var app = express();
app.use(function(req, res){
res.redirect('<lame>');
});
request(app)
.get('/')
.set('Host', 'http://example.com')
.set('Accept', 'text/html')
.end(function(err, res){
res.text.should.equal('<p>Moved Temporarily. Redirecting to <a href="&lt;lame&gt;">&lt;lame&gt;</a></p>');
2012-08-02 19:41:37 -07:00
done();
})
})
})
2013-02-24 18:01:50 -05:00
describe('when accepting text', function(){
it('should respond with text', function(done){
var app = express();
app.use(function(req, res){
res.redirect('http://google.com');
});
request(app)
.get('/')
.set('Accept', 'text/plain, */*')
2012-06-26 17:14:07 -07:00
.end(function(err, res){
res.headers.should.have.property('location', 'http://google.com');
2012-06-01 09:14:27 -07:00
res.headers.should.have.property('content-length', '51');
2012-06-26 17:14:07 -07:00
res.text.should.equal('Moved Temporarily. Redirecting to http://google.com');
done();
})
})
it('should encode the url', function(done){
var app = express();
app.use(function(req, res){
res.redirect('http://example.com/?param=<script>alert("hax");</script>');
});
request(app)
.get('/')
.set('Host', 'http://example.com')
.set('Accept', 'text/plain, */*')
.end(function(err, res){
res.text.should.equal('Moved Temporarily. Redirecting to http://example.com/?param=%3Cscript%3Ealert(%22hax%22);%3C/script%3E');
done();
})
})
})
2012-06-01 09:14:27 -07:00
describe('when accepting neither text or html', function(){
it('should respond with an empty body', function(done){
var app = express();
app.use(function(req, res){
res.redirect('http://google.com');
});
request(app)
.get('/')
.set('Accept', 'application/octet-stream')
2014-05-29 22:53:59 -04:00
.expect('location', 'http://google.com')
.expect('content-length', '0')
.expect(302, '', function(err, res){
if (err) return done(err)
2012-06-01 09:14:27 -07:00
res.headers.should.not.have.property('content-type');
done();
})
})
})
2011-11-08 18:36:27 -08:00
})