Skip to content

Commit

Permalink
Merge pull request #1460 from simov/local-address
Browse files Browse the repository at this point in the history
localAddress or proxy config is lost when redirecting
  • Loading branch information
simov committed Mar 6, 2015
2 parents b84a5d2 + 9ea8651 commit 8109dc0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
4 changes: 3 additions & 1 deletion request.js
Expand Up @@ -322,7 +322,9 @@ Request.prototype.init = function (options) {
if (!self.method) {
self.method = options.method || 'GET'
}
self.localAddress = options.localAddress
if (!self.localAddress) {
self.localAddress = options.localAddress
}

if (!self.qsLib) {
self.qsLib = (options.useQuerystring ? querystring : qs)
Expand Down
32 changes: 27 additions & 5 deletions tests/test-localAddress.js
@@ -1,27 +1,49 @@
'use strict'

var request = require('../index')
, tape = require('tape')

tape('bind to invalid address', function(t) {
tape('bind to invalid address', function (t) {
request.get({
uri: 'http://www.google.com',
localAddress: '1.2.3.4'
}, function(err, res) {
}, function (err, res) {
t.notEqual(err, null)
t.equal(true, /bind EADDRNOTAVAIL/.test(err.message))
t.equal(res, undefined)
t.end()
})
})

tape('bind to local address', function(t) {
tape('bind to local address', function (t) {
request.get({
uri: 'http://www.google.com',
localAddress: '127.0.0.1'
}, function(err, res) {
}, 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 (iface.family !== 'IPv4' || 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)
t.equal(res.request.localAddress, localIPS[0])
t.end()
})
})

0 comments on commit 8109dc0

Please sign in to comment.