2012-02-07 04:39:10 -08:00
|
|
|
|
|
|
|
|
var express = require('../')
|
2014-03-05 22:06:14 -08:00
|
|
|
, request = require('supertest');
|
2012-02-07 04:39:10 -08:00
|
|
|
|
|
|
|
|
describe('req', function(){
|
2012-02-07 08:19:30 -08:00
|
|
|
describe('.protocol', function(){
|
2012-02-07 04:39:10 -08:00
|
|
|
it('should return the protocol string', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
2012-02-07 08:19:30 -08:00
|
|
|
res.end(req.protocol);
|
2012-02-07 04:39:10 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
2012-06-26 17:14:07 -07:00
|
|
|
.expect('http', done);
|
2012-02-07 04:39:10 -08:00
|
|
|
})
|
|
|
|
|
|
2012-02-07 08:19:30 -08:00
|
|
|
describe('when "trust proxy" is enabled', function(){
|
2012-02-07 04:39:10 -08:00
|
|
|
it('should respect X-Forwarded-Proto', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
2012-02-07 08:19:30 -08:00
|
|
|
app.enable('trust proxy');
|
|
|
|
|
|
2012-02-07 04:39:10 -08:00
|
|
|
app.use(function(req, res){
|
2012-02-07 08:19:30 -08:00
|
|
|
res.end(req.protocol);
|
2012-02-07 04:39:10 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.set('X-Forwarded-Proto', 'https')
|
2012-06-26 17:14:07 -07:00
|
|
|
.expect('https', done);
|
2012-02-07 04:39:10 -08:00
|
|
|
})
|
2014-07-18 11:33:16 -04:00
|
|
|
|
|
|
|
|
it('should default to the socket addr if X-Forwarded-Proto not present', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.enable('trust proxy');
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
|
|
|
|
req.connection.encrypted = true;
|
|
|
|
|
res.end(req.protocol);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.expect('https', done);
|
|
|
|
|
})
|
2012-02-07 04:39:10 -08:00
|
|
|
|
2014-05-09 00:53:47 -04:00
|
|
|
it('should ignore X-Forwarded-Proto if socket addr not trusted', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.set('trust proxy', '10.0.0.1');
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
|
|
|
|
res.end(req.protocol);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.set('X-Forwarded-Proto', 'https')
|
|
|
|
|
.expect('http', done);
|
|
|
|
|
})
|
|
|
|
|
|
2012-02-07 04:39:10 -08:00
|
|
|
it('should default to http', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
2012-02-07 08:19:30 -08:00
|
|
|
app.enable('trust proxy');
|
|
|
|
|
|
2012-02-07 04:39:10 -08:00
|
|
|
app.use(function(req, res){
|
2012-02-07 08:19:30 -08:00
|
|
|
res.end(req.protocol);
|
2012-02-07 04:39:10 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
2012-06-26 17:14:07 -07:00
|
|
|
.expect('http', done);
|
2012-02-07 04:39:10 -08:00
|
|
|
})
|
2015-02-28 17:06:37 +00:00
|
|
|
|
|
|
|
|
describe('when trusting hop count', function () {
|
|
|
|
|
it('should respect X-Forwarded-Proto', function (done) {
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.set('trust proxy', 1);
|
|
|
|
|
|
|
|
|
|
app.use(function (req, res) {
|
|
|
|
|
res.end(req.protocol);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.set('X-Forwarded-Proto', 'https')
|
|
|
|
|
.expect('https', done);
|
|
|
|
|
})
|
|
|
|
|
})
|
2012-02-07 04:39:10 -08:00
|
|
|
})
|
|
|
|
|
|
2012-02-07 08:19:30 -08:00
|
|
|
describe('when "trust proxy" is disabled', function(){
|
2012-02-07 04:39:10 -08:00
|
|
|
it('should ignore X-Forwarded-Proto', function(done){
|
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
|
|
app.use(function(req, res){
|
2012-02-07 08:19:30 -08:00
|
|
|
res.end(req.protocol);
|
2012-02-07 04:39:10 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
request(app)
|
|
|
|
|
.get('/')
|
|
|
|
|
.set('X-Forwarded-Proto', 'https')
|
2012-06-26 17:14:07 -07:00
|
|
|
.expect('http', done);
|
2012-02-07 04:39:10 -08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|