2 var express = require('../')
3 , request = require('supertest')
5 describe('req', function(){
6 describe('.hostname', function(){
7 it('should return the Host when present', function(done){
10 app.use(function(req, res){
11 res.end(req.hostname);
16 .set('Host', 'example.com')
17 .expect('example.com', done);
20 it('should strip port number', function(done){
23 app.use(function(req, res){
24 res.end(req.hostname);
29 .set('Host', 'example.com:3000')
30 .expect('example.com', done);
33 it('should return undefined otherwise', function(done){
36 app.use(function(req, res){
37 req.headers.host = null;
38 res.end(String(req.hostname));
43 .expect('undefined', done);
46 it('should work with IPv6 Host', function(done){
49 app.use(function(req, res){
50 res.end(req.hostname);
56 .expect('[::1]', done);
59 it('should work with IPv6 Host and port', function(done){
62 app.use(function(req, res){
63 res.end(req.hostname);
68 .set('Host', '[::1]:3000')
69 .expect('[::1]', done);
72 describe('when "trust proxy" is enabled', function(){
73 it('should respect X-Forwarded-Host', function(done){
76 app.enable('trust proxy');
78 app.use(function(req, res){
79 res.end(req.hostname);
84 .set('Host', 'localhost')
85 .set('X-Forwarded-Host', 'example.com:3000')
86 .expect('example.com', done);
89 it('should ignore X-Forwarded-Host if socket addr not trusted', function(done){
92 app.set('trust proxy', '10.0.0.1');
94 app.use(function(req, res){
95 res.end(req.hostname);
100 .set('Host', 'localhost')
101 .set('X-Forwarded-Host', 'example.com')
102 .expect('localhost', done);
105 it('should default to Host', function(done){
108 app.enable('trust proxy');
110 app.use(function(req, res){
111 res.end(req.hostname);
116 .set('Host', 'example.com')
117 .expect('example.com', done);
121 describe('when "trust proxy" is disabled', function(){
122 it('should ignore X-Forwarded-Host', function(done){
125 app.use(function(req, res){
126 res.end(req.hostname);
131 .set('Host', 'localhost')
132 .set('X-Forwarded-Host', 'evil')
133 .expect('localhost', done);