Skip to content
Browse files

added client-side basic auth

  • Loading branch information...
1 parent f8fc259 commit c9632bac1cb81e20c5e2e30dcc67d30cf95a6966 @matthewmueller matthewmueller committed Apr 2, 2013
Showing with 70 additions and 4 deletions.
  1. +34 −4 build/build.js
  2. +15 −0 lib/client.js
  3. +10 −0 test/server.js
  4. +11 −0 test/test.request.js
View
38 build/build.js
@@ -825,6 +825,7 @@ function Request(method, url) {
this.method = method;
this.url = url;
this.header = {};
+ this._header = {};
this.set('X-Requested-With', 'XMLHttpRequest');
this.on('end', function(){
var res = new Response(self.xhr);
@@ -909,11 +910,24 @@ Request.prototype.set = function(field, val){
}
return this;
}
- this.header[field.toLowerCase()] = val;
+ this._header[field.toLowerCase()] = val;
+ this.header[field] = val;
return this;
};
/**
+ * Get case-insensitive header `field` value.
+ *
+ * @param {String} field
+ * @return {String}
+ * @api private
+ */
+
+Request.prototype.getHeader = function(field){
+ return this._header[field.toLowerCase()];
+};
+
+/**
* Set Content-Type to `type`, mapping values from `request.types`.
*
* Examples:
@@ -941,6 +955,21 @@ Request.prototype.type = function(type){
};
/**
+ * 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 = btoa(user + ':' + pass);
+ this.set('Authorization', 'Basic ' + str);
+ return this;
+};
+
+/**
* Add query-string `val`.
*
* Examples:
@@ -1013,7 +1042,7 @@ Request.prototype.query = function(val){
Request.prototype.send = function(data){
var obj = isObject(data);
- var type = this.header['content-type'];
+ var type = this.getHeader('Content-Type');
// merge
if (obj && isObject(this._data)) {
@@ -1022,7 +1051,7 @@ Request.prototype.send = function(data){
}
} else if ('string' == typeof data) {
if (!type) this.type('form');
- type = this.header['content-type'];
+ type = this.getHeader('Content-Type');
if ('application/x-www-form-urlencoded' == type) {
this._data = this._data
? this._data + '&' + data
@@ -1157,12 +1186,13 @@ Request.prototype.end = function(fn){
// body
if ('GET' != this.method && 'HEAD' != this.method && 'string' != typeof data) {
// serialize stuff
- var serialize = request.serialize[this.header['content-type']];
+ var serialize = request.serialize[this.getHeader('Content-Type')];
if (serialize) data = serialize(data);
}
// set header fields
for (var field in this.header) {
+ if (null == this.header[field]) continue;
xhr.setRequestHeader(field, this.header[field]);
}
View
15 lib/client.js
@@ -542,6 +542,21 @@ Request.prototype.type = function(type){
};
/**
+ * 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 = btoa(user + ':' + pass);
+ this.set('Authorization', 'Basic ' + str);
+ return this;
+};
+
+/**
* Add query-string `val`.
*
* Examples:
View
10 test/server.js
@@ -103,6 +103,16 @@ app.get('/foo', function(req, res){
.send('foo=bar');
});
+app.post('/auth', function(req, res) {
+ var auth = req.headers.authorization,
+ parts = auth.split(' '),
+ credentials = new Buffer(parts[1], 'base64').toString().split(':'),
+ user = credentials[0],
+ pass = credentials[1];
+
+ res.send({ user : user, pass : pass });
+});
+
app.use(express.static(__dirname + '/../'));
app.listen(4000);
View
11 test/test.request.js
@@ -491,3 +491,14 @@ test('x-domain failure', function(next){
next();
});
});
+
+test('basic auth', function(next){
+ request
+ .post('/auth')
+ .auth('foo', 'bar')
+ .end(function(res){
+ assert('foo' == res.body.user);
+ assert('bar' == res.body.pass);
+ next();
+ });
+});

0 comments on commit c9632ba

Please sign in to comment.
Something went wrong with that request. Please try again.