Skip to content
Myr

req.hostname.js

This file

Path

test/req.hostname.js

188 lines · 4.3 KiB · JavaScript

Last changed by

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

History of this file

Take it

The walk stopped after 200 commits: lines older than 2a0c35a point at it.

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 1why, one step back
1'use strict'

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 2–119why, one step back
2
3var express = require('../')4 , request = require('supertest')56describe('req', function(){7 describe('.hostname', function(){8 it('should return the Host when present', function(done){9 var app = express();1011 app.use(function(req, res){12 res.end(req.hostname);13 });1415 request(app)16 .post('/')17 .set('Host', 'example.com')18 .expect('example.com', done);19 })2021 it('should strip port number', function(done){22 var app = express();2324 app.use(function(req, res){25 res.end(req.hostname);26 });2728 request(app)29 .post('/')30 .set('Host', 'example.com:3000')31 .expect('example.com', done);32 })3334 it('should return undefined otherwise', function(done){35 var app = express();3637 app.use(function(req, res){38 req.headers.host = null;39 res.end(String(req.hostname));40 });4142 request(app)43 .post('/')44 .expect('undefined', done);45 })4647 it('should work with IPv6 Host', function(done){48 var app = express();4950 app.use(function(req, res){51 res.end(req.hostname);52 });5354 request(app)55 .post('/')56 .set('Host', '[::1]')57 .expect('[::1]', done);58 })5960 it('should work with IPv6 Host and port', function(done){61 var app = express();6263 app.use(function(req, res){64 res.end(req.hostname);65 });6667 request(app)68 .post('/')69 .set('Host', '[::1]:3000')70 .expect('[::1]', done);71 })7273 describe('when "trust proxy" is enabled', function(){74 it('should respect X-Forwarded-Host', function(done){75 var app = express();7677 app.enable('trust proxy');7879 app.use(function(req, res){80 res.end(req.hostname);81 });8283 request(app)84 .get('/')85 .set('Host', 'localhost')86 .set('X-Forwarded-Host', 'example.com:3000')87 .expect('example.com', done);88 })8990 it('should ignore X-Forwarded-Host if socket addr not trusted', function(done){91 var app = express();9293 app.set('trust proxy', '10.0.0.1');9495 app.use(function(req, res){96 res.end(req.hostname);97 });9899 request(app)100 .get('/')101 .set('Host', 'localhost')102 .set('X-Forwarded-Host', 'example.com')103 .expect('localhost', done);104 })105106 it('should default to Host', function(done){107 var app = express();108109 app.enable('trust proxy');110111 app.use(function(req, res){112 res.end(req.hostname);113 });114115 request(app)116 .get('/')117 .set('Host', 'example.com')118 .expect('example.com', done);119 })

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 120–169why, one step back
120
121 describe('when multiple X-Forwarded-Host', function () {122 it('should use the first value', function (done) {123 var app = express()124125 app.enable('trust proxy')126127 app.use(function (req, res) {128 res.send(req.hostname)129 })130131 request(app)132 .get('/')133 .set('Host', 'localhost')134 .set('X-Forwarded-Host', 'example.com, foobar.com')135 .expect(200, 'example.com', done)136 })137138 it('should remove OWS around comma', function (done) {139 var app = express()140141 app.enable('trust proxy')142143 app.use(function (req, res) {144 res.send(req.hostname)145 })146147 request(app)148 .get('/')149 .set('Host', 'localhost')150 .set('X-Forwarded-Host', 'example.com , foobar.com')151 .expect(200, 'example.com', done)152 })153154 it('should strip port number', function (done) {155 var app = express()156157 app.enable('trust proxy')158159 app.use(function (req, res) {160 res.send(req.hostname)161 })162163 request(app)164 .get('/')165 .set('Host', 'localhost')166 .set('X-Forwarded-Host', 'example.com:8080 , foobar.com:8888')167 .expect(200, 'example.com', done)168 })169 })

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 170–188why, one step back
170 })
171172 describe('when "trust proxy" is disabled', function(){173 it('should ignore X-Forwarded-Host', function(done){174 var app = express();175176 app.use(function(req, res){177 res.end(req.hostname);178 });179180 request(app)181 .get('/')182 .set('Host', 'localhost')183 .set('X-Forwarded-Host', 'evil')184 .expect('localhost', done);185 })186 })187 })188})