req.ip.js
This file
Path
test/req.ip.js
80 lines · 2.0 KiB · JavaScript
Last changed by
Merge tag '3.17.2'Douglas Christopher Wilsonbefore adoption916a75c
History of this fileTake it
The walk stopped after 200 commits: lines older than 269dc53 point at it.
Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 1–47why, 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 })38 })3940 describe('when "trust proxy" is disabled', function(){41 it('should return the remote address', function(done){42 var app = express();4344 app.use(function(req, res, next){45 res.send(req.ip);46 });47Merge tag '3.17.2'Douglas Christopher Wilsonbefore adoption916a75c
lines 48–50why, one step back48 var test = request(app).get('/')49 test.set('X-Forwarded-For', 'client, p1, p2')50 test.expect(200, getExpectedClientAddress(test._server), done);Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 51–64why, one step back51 })52 })53 })5455 describe('when X-Forwarded-For is not present', function(){56 it('should return the remote address', function(done){57 var app = express();5859 app.enable('trust proxy');6061 app.use(function(req, res, next){62 res.send(req.ip);63 });64Merge tag '3.17.2'Douglas Christopher Wilsonbefore adoption916a75c
lines 65–66why, one step back65 var test = request(app).get('/')66 test.expect(200, getExpectedClientAddress(test._server), done);Invoke router.param() only when route matchesDouglas Christopher Wilsonbefore adoption269dc53
lines 67–70why, one step back67 })68 })69 })70})Merge tag '3.17.2'Douglas Christopher Wilsonbefore adoption916a75c
lines 71–80why, one step back7172/**73 * Get the local client address depending on AF_NET of server74 */7576function getExpectedClientAddress(server) {77 return server.address().address === '::'78 ? '::ffff:127.0.0.1'79 : '127.0.0.1';80}