Skip to content

Commit

Permalink
Merge pull request request#690 from diversario/i681-blank-pass-basic-…
Browse files Browse the repository at this point in the history
…auth

Handle blank password in basic auth.
  • Loading branch information
mikeal committed Oct 24, 2013
2 parents 034e84e + cabe5a6 commit 7fd5498
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
10 changes: 7 additions & 3 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
28 changes: 28 additions & 0 deletions tests/test-basic-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -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();
})
}
];

Expand Down

0 comments on commit 7fd5498

Please sign in to comment.