Skip to content

Commit

Permalink
Add support for basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
vesln committed Nov 23, 2013
1 parent e4cdca0 commit 413db5b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lib/hippie/client.js
Expand Up @@ -176,6 +176,20 @@ Client.prototype.send = function(data) {
return this;
};

/**
* Set basic auth credentials.
*
* @param {String} user
* @param {String} password
* @returns {Client} self
* @api public
*/

Client.prototype.auth = function(user, pass) {
this.options.auth = { user: user, pass: pass };
return this;
};

/**
* Sugar syntax for `method`, `url` and `end`.
*
Expand Down
10 changes: 10 additions & 0 deletions test/auth.test.js
@@ -0,0 +1,10 @@
describe('#auth', function() {
it('sets basic auth credentials', function(done) {
api()
.auth('user', 'pass')
.get('/auth', function(err, res) {
res.statusCode.should.eq(200);
done();
});
});
});
12 changes: 10 additions & 2 deletions test/support/server.js
Expand Up @@ -4,6 +4,10 @@

var express = require('express');

/**
* Server.
*/

var app = express();

app.all('/method', function(req, res) {
Expand All @@ -20,14 +24,18 @@ app.get('/qs', function(req, res) {

app.get('/slow', function() {});

app.post('/send-form', [express.urlencoded()], function(req, res) {
app.post('/send-form', express.urlencoded(), function(req, res) {
res.send(JSON.stringify(req.body));
});

app.post('/send-json', [express.json()], function(req, res) {
app.post('/send-json', express.json(), function(req, res) {
res.send(JSON.stringify(req.body));
});

app.get('/auth', express.basicAuth('user', 'pass'), function(req, res) {
res.send('ok');
});

/**
* Primary export.
*/
Expand Down

0 comments on commit 413db5b

Please sign in to comment.