Skip to content

Commit

Permalink
Added basic auth support [node]. Closes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Dec 28, 2011
1 parent 185c212 commit 7f4ca63
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,20 @@ Request.prototype.redirect = function(res){
return this;
};

/**
* Set Authorization field value with `user` and `pass`.
*
* @param {String} user
* @param {String} pass
* @return {Request} for chaining
* @api public
*/

Request.prototype.auth = function(user, pass){
var str = new Buffer(user + ':' + pass).toString('base64');
return this.set('Authorization', 'Basic ' + str);
};

/**
* Return an http[s] request.
*
Expand Down
1 change: 1 addition & 0 deletions lib/node/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ exports.uid = function(len){
}
return buf;
};

40 changes: 40 additions & 0 deletions test/node/basic-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

var EventEmitter = require('events').EventEmitter
, request = require('../../')
, express = require('express')
, assert = require('assert')
, app = express.createServer();

app.use(express.basicAuth('tobi', 'learnboost'));

app.get('/', function(req, res){
res.end('you win!');
});

app.listen(3010);

describe('req.auth(user, pass)', function(){
describe('when valid', function(){
it('should serve the request', function(done){
request
.get('http://localhost:3010')
.auth('tobi', 'learnboost')
.end(function(res){
res.status.should.equal(200);
done();
});
})
})

describe('when invalid', function(){
it('should 401', function(done){
request
.get('http://localhost:3010')
.auth('tobis', 'learnboost')
.end(function(res){
res.status.should.equal(401);
done();
});
})
})
})

0 comments on commit 7f4ca63

Please sign in to comment.