diff --git a/request.js b/request.js index f0284635d..48e0c06dc 100644 --- a/request.js +++ b/request.js @@ -277,10 +277,14 @@ Request.prototype.init = function (options) { } if (options.auth) { + if (Object.prototype.hasOwnProperty.call(options.auth, 'username')) options.auth.user = options.auth.username + if (Object.prototype.hasOwnProperty.call(options.auth, 'password')) options.auth.pass = options.auth.password + self.auth( - (options.auth.user==="") ? options.auth.user : (options.auth.user || options.auth.username ), - options.auth.pass || options.auth.password, - options.auth.sendImmediately) + options.auth.user, + options.auth.pass, + options.auth.sendImmediately + ) } if (self.uri.auth && !self.hasHeader('authorization')) { diff --git a/tests/test-basic-auth.js b/tests/test-basic-auth.js index 7cf1d6d82..23f4ee28c 100644 --- a/tests/test-basic-auth.js +++ b/tests/test-basic-auth.js @@ -14,6 +14,8 @@ var basicServer = http.createServer(function (req, res) { if (req.headers.authorization) { if (req.headers.authorization == 'Basic ' + new Buffer('test:testing2').toString('base64')) { ok = true; + } else if ( req.headers.authorization == 'Basic ' + new Buffer('test:').toString('base64')) { + ok = true; } else if ( req.headers.authorization == 'Basic ' + new Buffer(':apassword').toString('base64')) { ok = true; } else if ( req.headers.authorization == 'Basic ' + new Buffer('justauser').toString('base64')) { @@ -146,6 +148,32 @@ var tests = [ next(); }); }) + }, + + function (next) { + request + .get('http://localhost:6767/test/') + .auth("test","",false) + .on('response', function (res) { + assert.equal(res.statusCode, 200); + assert.equal(numBasicRequests, 12); + next(); + }) + }, + + function (next) { + request.get('http://localhost:6767/test/', + { + auth: { + user: "test", + pass: "", + sendImmediately: false + } + }, function (err, res) { + assert.equal(res.statusCode, 200); + assert.equal(numBasicRequests, 14); + next(); + }) } ];