From 237e80fe076680669da7f1d3b42b9cba19e91e1c Mon Sep 17 00:00:00 2001 From: 0x4139 <0x4139@gmail.com> Date: Thu, 19 Feb 2015 13:02:35 +0200 Subject: [PATCH 1/4] fixes the redirect initialization of an object when localAddress or proxy is lost due to the reinitialization without parameters of the request object --- lib/redirect.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/redirect.js b/lib/redirect.js index 2d9a9d518..ff6c32fda 100644 --- a/lib/redirect.js +++ b/lib/redirect.js @@ -138,7 +138,10 @@ Redirect.prototype.onResponse = function (response) { request.emit('redirect') - request.init() + request.init({ + localAddress:self.localAddress, + proxy:self.proxy + }) return true } From 93affc4acc2285dd6ba0c91306df21ae51580271 Mon Sep 17 00:00:00 2001 From: 0x4139 <0x4139@gmail.com> Date: Thu, 19 Feb 2015 15:08:35 +0200 Subject: [PATCH 2/4] bind to localAddress on redirect, and the afferent test --- lib/redirect.js | 6 ++-- tests/test-localAddress.js | 66 ++++++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/lib/redirect.js b/lib/redirect.js index ff6c32fda..b086c1992 100644 --- a/lib/redirect.js +++ b/lib/redirect.js @@ -138,9 +138,9 @@ Redirect.prototype.onResponse = function (response) { request.emit('redirect') - request.init({ - localAddress:self.localAddress, - proxy:self.proxy + request.init({ + localAddress:request.localAddress, + proxy:request.proxy }) return true diff --git a/tests/test-localAddress.js b/tests/test-localAddress.js index 4445001d5..648f422df 100644 --- a/tests/test-localAddress.js +++ b/tests/test-localAddress.js @@ -1,27 +1,51 @@ 'use strict' - var request = require('../index') - , tape = require('tape') + , tape = require('tape') + +tape('bind to invalid address', function (t) { + request.get({ + uri: 'http://www.google.com', + localAddress: '1.2.3.4' + }, function (err, res) { + t.notEqual(err, null) + t.equal(err.message, 'bind EADDRNOTAVAIL') + t.equal(res, undefined) + t.end() + }) +}) -tape('bind to invalid address', function(t) { - request.get({ - uri: 'http://www.google.com', - localAddress: '1.2.3.4' - }, function(err, res) { - t.notEqual(err, null) - t.equal(err.message, 'bind EADDRNOTAVAIL') - t.equal(res, undefined) - t.end() - }) +tape('bind to local address', function (t) { + request.get({ + uri: 'http://www.google.com', + localAddress: '127.0.0.1' + }, function (err, res) { + t.notEqual(err, null) + t.equal(res, undefined) + t.end() + }) }) -tape('bind to local address', function(t) { - request.get({ - uri: 'http://www.google.com', - localAddress: '127.0.0.1' - }, function(err, res) { - t.notEqual(err, null) - t.equal(res, undefined) - t.end() - }) +tape('bind to local address on redirect', function (t) { + var os = require('os') + var localInterfaces = os.networkInterfaces() + var localIPS=[] + Object.keys(localInterfaces).forEach(function (ifname) { + localInterfaces[ifname].forEach(function (iface) { + if ('IPv4' !== iface.family || iface.internal !== false) { + // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses + return; + } + localIPS.push(iface.address); + }); + }); + request.get({ + uri: 'http://google.com', //redirects to 'http://google.com' + localAddress: localIPS[0] + }, function (err, res) { + t.equal(err, null) + res.body = ""; + console.log(res.request.method); + t.equal(res.request.localAddress, localIPS[0]) + t.end() + }) }) From 92be4017f436b32634e79c68a67962e773c27534 Mon Sep 17 00:00:00 2001 From: 0x4139 <0x4139@gmail.com> Date: Thu, 19 Feb 2015 15:10:24 +0200 Subject: [PATCH 3/4] cleans up test-localAddress --- tests/test-localAddress.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test-localAddress.js b/tests/test-localAddress.js index 648f422df..325b2985a 100644 --- a/tests/test-localAddress.js +++ b/tests/test-localAddress.js @@ -43,8 +43,6 @@ tape('bind to local address on redirect', function (t) { localAddress: localIPS[0] }, function (err, res) { t.equal(err, null) - res.body = ""; - console.log(res.request.method); t.equal(res.request.localAddress, localIPS[0]) t.end() }) From 7759f6006ccfa5f0bf4182c50942f381c4a840d6 Mon Sep 17 00:00:00 2001 From: 0x4139 <0x4139@gmail.com> Date: Wed, 25 Feb 2015 15:51:27 +0200 Subject: [PATCH 4/4] fixed linting --- tests/test-localAddress.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test-localAddress.js b/tests/test-localAddress.js index 325b2985a..9d65dd157 100644 --- a/tests/test-localAddress.js +++ b/tests/test-localAddress.js @@ -28,16 +28,16 @@ tape('bind to local address', function (t) { tape('bind to local address on redirect', function (t) { var os = require('os') var localInterfaces = os.networkInterfaces() - var localIPS=[] + var localIPS = [] Object.keys(localInterfaces).forEach(function (ifname) { localInterfaces[ifname].forEach(function (iface) { - if ('IPv4' !== iface.family || iface.internal !== false) { + if (iface.family !== 'IPv4' || iface.internal !== false) { // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses - return; + return } - localIPS.push(iface.address); - }); - }); + localIPS.push(iface.address) + }) + }) request.get({ uri: 'http://google.com', //redirects to 'http://google.com' localAddress: localIPS[0]