|
| 1 | + |
| 2 | +var express = require('../') |
| 3 | + , request = require('supertest') |
| 4 | + , assert = require('assert'); |
| 5 | + |
| 6 | +describe('req', function(){ |
| 7 | + describe('.hostname', function(){ |
| 8 | + it('should return the Host when present', function(done){ |
| 9 | + var app = express(); |
| 10 | + |
| 11 | + app.use(function(req, res){ |
| 12 | + res.end(req.hostname); |
| 13 | + }); |
| 14 | + |
| 15 | + request(app) |
| 16 | + .post('/') |
| 17 | + .set('Host', 'example.com') |
| 18 | + .expect('example.com', done); |
| 19 | + }) |
| 20 | + |
| 21 | + it('should strip port number', function(done){ |
| 22 | + var app = express(); |
| 23 | + |
| 24 | + app.use(function(req, res){ |
| 25 | + res.end(req.hostname); |
| 26 | + }); |
| 27 | + |
| 28 | + request(app) |
| 29 | + .post('/') |
| 30 | + .set('Host', 'example.com:3000') |
| 31 | + .expect('example.com', done); |
| 32 | + }) |
| 33 | + |
| 34 | + it('should return undefined otherwise', function(done){ |
| 35 | + var app = express(); |
| 36 | + |
| 37 | + app.use(function(req, res){ |
| 38 | + req.headers.host = null; |
| 39 | + res.end(String(req.hostname)); |
| 40 | + }); |
| 41 | + |
| 42 | + request(app) |
| 43 | + .post('/') |
| 44 | + .expect('undefined', done); |
| 45 | + }) |
| 46 | + |
| 47 | + it('should work with IPv6 Host', function(done){ |
| 48 | + var app = express(); |
| 49 | + |
| 50 | + app.use(function(req, res){ |
| 51 | + res.end(req.hostname); |
| 52 | + }); |
| 53 | + |
| 54 | + request(app) |
| 55 | + .post('/') |
| 56 | + .set('Host', '[::1]') |
| 57 | + .expect('[::1]', done); |
| 58 | + }) |
| 59 | + |
| 60 | + it('should work with IPv6 Host and port', function(done){ |
| 61 | + var app = express(); |
| 62 | + |
| 63 | + app.use(function(req, res){ |
| 64 | + res.end(req.hostname); |
| 65 | + }); |
| 66 | + |
| 67 | + request(app) |
| 68 | + .post('/') |
| 69 | + .set('Host', '[::1]:3000') |
| 70 | + .expect('[::1]', done); |
| 71 | + }) |
| 72 | + |
| 73 | + describe('when "trust proxy" is enabled', function(){ |
| 74 | + it('should respect X-Forwarded-Host', function(done){ |
| 75 | + var app = express(); |
| 76 | + |
| 77 | + app.enable('trust proxy'); |
| 78 | + |
| 79 | + app.use(function(req, res){ |
| 80 | + res.end(req.hostname); |
| 81 | + }); |
| 82 | + |
| 83 | + request(app) |
| 84 | + .get('/') |
| 85 | + .set('Host', 'localhost') |
| 86 | + .set('X-Forwarded-Host', 'example.com:3000') |
| 87 | + .expect('example.com', done); |
| 88 | + }) |
| 89 | + |
| 90 | + it('should ignore X-Forwarded-Host if socket addr not trusted', function(done){ |
| 91 | + var app = express(); |
| 92 | + |
| 93 | + app.set('trust proxy', '10.0.0.1'); |
| 94 | + |
| 95 | + app.use(function(req, res){ |
| 96 | + res.end(req.hostname); |
| 97 | + }); |
| 98 | + |
| 99 | + request(app) |
| 100 | + .get('/') |
| 101 | + .set('Host', 'localhost') |
| 102 | + .set('X-Forwarded-Host', 'example.com') |
| 103 | + .expect('localhost', done); |
| 104 | + }) |
| 105 | + |
| 106 | + it('should default to Host', function(done){ |
| 107 | + var app = express(); |
| 108 | + |
| 109 | + app.enable('trust proxy'); |
| 110 | + |
| 111 | + app.use(function(req, res){ |
| 112 | + res.end(req.hostname); |
| 113 | + }); |
| 114 | + |
| 115 | + request(app) |
| 116 | + .get('/') |
| 117 | + .set('Host', 'example.com') |
| 118 | + .expect('example.com', done); |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + describe('when "trust proxy" is disabled', function(){ |
| 123 | + it('should ignore X-Forwarded-Host', function(done){ |
| 124 | + var app = express(); |
| 125 | + |
| 126 | + app.use(function(req, res){ |
| 127 | + res.end(req.hostname); |
| 128 | + }); |
| 129 | + |
| 130 | + request(app) |
| 131 | + .get('/') |
| 132 | + .set('Host', 'localhost') |
| 133 | + .set('X-Forwarded-Host', 'evil') |
| 134 | + .expect('localhost', done); |
| 135 | + }) |
| 136 | + }) |
| 137 | + }) |
| 138 | +}) |
0 commit comments