2011-11-08 18:36:27 -08:00
|
|
|
|
|
|
|
|
var express = require('../')
|
|
|
|
|
, request = require('./support/http');
|
|
|
|
|
|
|
|
|
|
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('/')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2011-11-08 18:36:27 -08:00
|
|
|
res.statusCode.should.equal(302);
|
|
|
|
|
res.headers.should.have.property('location', 'http://google.com');
|
|
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
2012-07-18 09:29:11 -07:00
|
|
|
|
|
|
|
|
describe('with leading //', function(){
|
|
|
|
|
it('should pass through scheme-relative urls', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
|
|
|
|
res.redirect('//cuteoverload.com');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.set('Host', 'example.com')
|
|
|
|
|
.end(function(err, res){
|
|
|
|
|
res.headers.should.have.property('location', '//cuteoverload.com');
|
|
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2011-11-08 18:46:23 -08:00
|
|
|
|
2011-12-06 16:21:04 -08:00
|
|
|
describe('with leading /', function(){
|
2012-07-18 09:07:21 -07:00
|
|
|
it('should construct scheme-relative urls', function(done){
|
2011-11-08 18:46:23 -08:00
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
|
|
|
|
res.redirect('/login');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.set('Host', 'example.com')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2012-07-18 09:07:21 -07:00
|
|
|
res.headers.should.have.property('location', '//example.com/login');
|
2011-11-08 18:46:23 -08:00
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2011-12-06 16:32:48 -08:00
|
|
|
|
|
|
|
|
describe('with leading ./', function(){
|
|
|
|
|
it('should construct path-relative urls', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
|
|
|
|
res.redirect('./edit');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/post/1')
|
|
|
|
|
.set('Host', 'example.com')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2012-07-18 09:07:21 -07:00
|
|
|
res.headers.should.have.property('location', '//example.com/post/1/./edit');
|
2011-12-06 16:44:15 -08:00
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('with leading ../', function(){
|
|
|
|
|
it('should construct path-relative urls', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
|
|
|
|
res.redirect('../new');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/post/1')
|
|
|
|
|
.set('Host', 'example.com')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2012-07-18 09:07:21 -07:00
|
|
|
res.headers.should.have.property('location', '//example.com/post/1/../new');
|
2011-12-06 16:32:48 -08:00
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2011-11-08 18:59:40 -08:00
|
|
|
|
2011-12-06 16:21:04 -08:00
|
|
|
describe('without leading /', function(){
|
|
|
|
|
it('should construct mount-point relative urls', function(done){
|
2011-12-06 15:53:17 -08:00
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
|
|
|
|
res.redirect('login');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.set('Host', 'example.com')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2012-07-18 09:07:21 -07:00
|
|
|
res.headers.should.have.property('location', '//example.com/login');
|
2011-12-06 15:53:17 -08:00
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2011-11-08 18:59:40 -08:00
|
|
|
describe('when mounted', function(){
|
2011-12-06 15:50:08 -08:00
|
|
|
describe('deeply', function(){
|
|
|
|
|
it('should respect the mount-point', function(done){
|
|
|
|
|
var app = express()
|
|
|
|
|
, blog = express()
|
|
|
|
|
, admin = express();
|
2011-11-08 18:59:40 -08:00
|
|
|
|
2011-12-06 15:50:08 -08:00
|
|
|
admin.use(function(req, res){
|
|
|
|
|
res.redirect('login');
|
|
|
|
|
});
|
2011-11-08 18:59:40 -08:00
|
|
|
|
2011-12-06 15:50:08 -08:00
|
|
|
app.use('/blog', blog);
|
|
|
|
|
blog.use('/admin', admin);
|
2011-11-08 18:59:40 -08:00
|
|
|
|
2011-12-06 15:50:08 -08:00
|
|
|
request(app)
|
|
|
|
|
.get('/blog/admin')
|
|
|
|
|
.set('Host', 'example.com')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2012-07-18 09:07:21 -07:00
|
|
|
res.headers.should.have.property('location', '//example.com/blog/admin/login');
|
2011-12-06 15:50:08 -08:00
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('omitting leading /', function(){
|
|
|
|
|
it('should respect the mount-point', function(done){
|
|
|
|
|
var app = express()
|
|
|
|
|
, admin = express();
|
|
|
|
|
|
|
|
|
|
admin.use(function(req, res){
|
|
|
|
|
res.redirect('admin/login');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.use('/blog', admin);
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/blog')
|
|
|
|
|
.set('Host', 'example.com')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2012-07-18 09:07:21 -07:00
|
|
|
res.headers.should.have.property('location', '//example.com/blog/admin/login');
|
2011-12-06 15:50:08 -08:00
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('providing leading /', function(){
|
|
|
|
|
it('should ignore mount-point', function(done){
|
|
|
|
|
var app = express()
|
|
|
|
|
, admin = express();
|
|
|
|
|
|
|
|
|
|
admin.use(function(req, res){
|
|
|
|
|
res.redirect('/admin/login');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.use('/blog', admin);
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/blog')
|
|
|
|
|
.set('Host', 'example.com')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2012-07-18 09:07:21 -07:00
|
|
|
res.headers.should.have.property('location', '//example.com/admin/login');
|
2011-12-06 15:50:08 -08:00
|
|
|
done();
|
|
|
|
|
})
|
2011-11-08 18:59:40 -08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
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('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();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2011-11-08 18:41:26 -08: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){
|
2011-11-08 18:41:26 -08:00
|
|
|
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>');
|
2011-11-08 18:41:26 -08:00
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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('/')
|
2012-02-22 19:58:36 -08:00
|
|
|
.set('Accept', 'text/plain, */*')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2011-11-08 18:41:26 -08:00
|
|
|
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');
|
2011-11-08 18:41:26 -08:00
|
|
|
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', 'foo/bar')
|
2012-06-26 17:14:07 -07:00
|
|
|
.end(function(err, res){
|
2012-06-01 09:14:27 -07:00
|
|
|
res.should.have.status(302);
|
|
|
|
|
res.headers.should.have.property('location', 'http://google.com');
|
|
|
|
|
res.headers.should.not.have.property('content-type');
|
|
|
|
|
res.headers.should.have.property('content-length', '0');
|
2012-06-26 17:14:07 -07:00
|
|
|
res.text.should.equal('');
|
2012-06-01 09:14:27 -07:00
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2011-11-08 18:36:27 -08:00
|
|
|
})
|