req.ip.js
This file
Path
test/req.ip.js
97 lines · 2.4 KiB · JavaScript
Last changed by
Fix "trust proxy" setting to inherit when app is mountedDouglas Christopher Wilsonbefore adoptionb40e74d
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f4968a point at it.
3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 1–37why, one step back12var express = require('../')3 , request = require('supertest');45describe('req', function(){6 describe('.ip', function(){7 describe('when X-Forwarded-For is present', function(){8 describe('when "trust proxy" is enabled', function(){9 it('should return the client addr', function(done){10 var app = express();1112 app.enable('trust proxy');1314 app.use(function(req, res, next){15 res.send(req.ip);16 });1718 request(app)19 .get('/')20 .set('X-Forwarded-For', 'client, p1, p2')21 .expect('client', done);22 })2324 it('should return the addr after trusted proxy', function(done){25 var app = express();2627 app.set('trust proxy', 2);2829 app.use(function(req, res, next){30 res.send(req.ip);31 });3233 request(app)34 .get('/')35 .set('X-Forwarded-For', 'client, p1, p2')36 .expect('p1', done);37 })Fix "trust proxy" setting to inherit when app is mountedDouglas Christopher Wilsonbefore adoptionb40e74d
lines 38–54why, one step back3839 it('should return the addr after trusted proxy, from sub app', function (done) {40 var app = express();41 var sub = express();4243 app.set('trust proxy', 2);44 app.use(sub);4546 sub.use(function (req, res, next) {47 res.send(req.ip);48 });4950 request(app)51 .get('/')52 .set('X-Forwarded-For', 'client, p1, p2')53 .expect(200, 'p1', done);54 })3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 55–64why, one step back55 })5657 describe('when "trust proxy" is disabled', function(){58 it('should return the remote address', function(done){59 var app = express();6061 app.use(function(req, res, next){62 res.send(req.ip);63 });64tests: fix broken tests with node v0.12 branchJulien Gillibefore adoption80847d8
lines 65–67why, one step back65 var test = request(app).get('/')66 test.set('X-Forwarded-For', 'client, p1, p2')67 test.expect(200, getExpectedClientAddress(test._server), done);3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 68–81why, one step back68 })69 })70 })7172 describe('when X-Forwarded-For is not present', function(){73 it('should return the remote address', function(done){74 var app = express();7576 app.enable('trust proxy');7778 app.use(function(req, res, next){79 res.send(req.ip);80 });81tests: fix broken tests with node v0.12 branchJulien Gillibefore adoption80847d8
lines 82–83why, one step back82 var test = request(app).get('/')83 test.expect(200, getExpectedClientAddress(test._server), done);3.15.1Douglas Christopher Wilsonbefore adoption9f4968a
lines 84–87why, one step back84 })85 })86 })87})tests: fix broken tests with node v0.12 branchJulien Gillibefore adoption80847d8
lines 88–97why, one step back8889/**90 * Get the local client address depending on AF_NET of server91 */9293function getExpectedClientAddress(server) {94 return server.address().address === '::'95 ? '::ffff:127.0.0.1'96 : '127.0.0.1';97}