req.ip.js
This file
Path
test/req.ip.js
113 lines · 2.9 KiB · JavaScript
Last changed by
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
History of this fileTake 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 back1'use strict'Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 2–24why, one step back23var express = require('../')4 , request = require('supertest');56describe('req', function(){7 describe('.ip', function(){8 describe('when X-Forwarded-For is present', function(){9 describe('when "trust proxy" is enabled', function(){10 it('should return the client addr', function(done){11 var app = express();1213 app.enable('trust proxy');1415 app.use(function(req, res, next){16 res.send(req.ip);17 });1819 request(app)20 .get('/')21 .set('X-Forwarded-For', 'client, p1, p2')22 .expect('client', done);23 })24Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 25why, one step back25 it('should return the addr after trusted proxy based on count', function (done) {Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 26–37why, one step back26 var app = express();2728 app.set('trust proxy', 2);2930 app.use(function(req, res, next){31 res.send(req.ip);32 });3334 request(app)35 .get('/')36 .set('X-Forwarded-For', 'client, p1, p2')37 .expect('p1', done);Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 38–52why, one step back38 })3940 it('should return the addr after trusted proxy based on list', function (done) {41 var app = express()4243 app.set('trust proxy', '10.0.0.1, 10.0.0.2, 127.0.0.1, ::1')4445 app.get('/', function (req, res) {46 res.send(req.ip)47 })4849 request(app)50 .get('/')51 .set('X-Forwarded-For', '10.0.0.2, 10.0.0.3, 10.0.0.1', '10.0.0.4')52 .expect('10.0.0.3', done)Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 53why, one step back53 })Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 54–70why, one step back5455 it('should return the addr after trusted proxy, from sub app', function (done) {56 var app = express();57 var sub = express();5859 app.set('trust proxy', 2);60 app.use(sub);6162 sub.use(function (req, res, next) {63 res.send(req.ip);64 });6566 request(app)67 .get('/')68 .set('X-Forwarded-For', 'client, p1, p2')69 .expect(200, 'p1', done);70 })Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 71–80why, one step back71 })7273 describe('when "trust proxy" is disabled', function(){74 it('should return the remote address', function(done){75 var app = express();7677 app.use(function(req, res, next){78 res.send(req.ip);79 });80Merge tag '3.17.2'Douglas Christopher Wilsonbefore adoption916a75c
lines 81–83why, one step back81 var test = request(app).get('/')82 test.set('X-Forwarded-For', 'client, p1, p2')83 test.expect(200, getExpectedClientAddress(test._server), done);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 84–97why, one step back84 })85 })86 })8788 describe('when X-Forwarded-For is not present', function(){89 it('should return the remote address', function(done){90 var app = express();9192 app.enable('trust proxy');9394 app.use(function(req, res, next){95 res.send(req.ip);96 });97Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 98–99why, one step back98 var test = request(app).get('/')99 test.expect(200, getExpectedClientAddress(test._server), done)Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 100–103why, one step back100 })101 })102 })103})Merge tag '3.17.2'Douglas Christopher Wilsonbefore adoption916a75c
lines 104–113why, one step back104105/**106 * Get the local client address depending on AF_NET of server107 */108109function getExpectedClientAddress(server) {110 return server.address().address === '::'111 ? '::ffff:127.0.0.1'112 : '127.0.0.1';113}