http-request
Advanced tools
Comparing version 0.6.2 to 0.6.3
@@ -0,1 +1,4 @@ | ||
## v0.6.3 | ||
* New option: auth. Defines the HTTP Authentication parameters. Currently it only supports Basic authentication. | ||
## v0.6.2 | ||
@@ -2,0 +5,0 @@ * Closes [#24](https://github.com/SaltwaterC/http-request/issues/24). Keeps the host header specified into the request headers when the location header contains a relative path. |
@@ -31,2 +31,4 @@ 'use strict'; | ||
* @throws {options.progress} Expecting a function as progress callback. | ||
* @throws {options.auth} Expecting an Object for the auth option. | ||
* @throws {options.auth.type} Unknown type for the auth option. | ||
*/ | ||
@@ -94,2 +96,17 @@ function Request(options) { | ||
if (this.options.auth) { | ||
if (typeof this.options.auth !== 'object') { | ||
throw new Error('Expecting an Object for the auth option.'); | ||
} | ||
switch (this.options.auth.type) { | ||
case 'basic': | ||
this.options.headers.authorization = 'Basic ' + new Buffer(this.options.auth.username + ':' + this.options.auth.password).toString('base64'); | ||
break; | ||
default: | ||
throw new Error('Unknown type for the auth option.'); | ||
} | ||
} | ||
// this is needed for making sure there aren't any race conditions | ||
@@ -762,2 +779,3 @@ // due to unexpected node.js behavior | ||
* @property {Buffer|Stream} reqBody Optional; the contents of the request body for PUT, POST, etc. requests. Must be a Buffer or a Readable Stream instance. For Buffers, the content-length is Buffer.lenght. For a Readable Stream you must pass a content-length header, unless you're building a POST request with the [form-data](https://github.com/felixge/node-form-data) module. The library makes no assumptions about the content-type header, unless you're building a form with form-data | ||
* @property {module:request~auth} auth Optional; an object describing the authentication information. Currently it only implements HTTP Basic Authentication. The HTTP Basic Authentication was already supported by passing the username:password to the URL itself. This option provides a more API-friendly approach and it adds the basics for implementing HTTP Digest Authentication. | ||
*/ | ||
@@ -820,3 +838,3 @@ | ||
* @typedef module:request~sendResult | ||
* @type {Options} | ||
* @type {Object} | ||
* @property {Stream} response The HTTP response of node's http.request | ||
@@ -826,1 +844,11 @@ * @property {Stream} stream The HTTP response body. Uncompressed if it was encoded with gzip or deflate. Equal to response otherwise. | ||
*/ | ||
/** | ||
* Describes the auth Object | ||
* | ||
* @typedef module:request~auth | ||
* @type {Object} | ||
* @property {String} type The HTTP Authentication type. Currently the only accepted value is: basic. | ||
* @property {String} username The username | ||
* @property {String} password The password | ||
*/ |
@@ -91,3 +91,4 @@ 'use strict'; | ||
exports.urlEncode = function(str) { | ||
var i, obj = {}, key, value, idx, curr; | ||
var i, obj = {}, | ||
key, value, idx, curr; | ||
@@ -94,0 +95,0 @@ str = str.split('&'); |
{ | ||
"name": "http-request", | ||
"version": "0.6.2", | ||
"version": "0.6.3", | ||
"main": "./lib/main.js", | ||
@@ -5,0 +5,0 @@ "description": "General purpose HTTP / HTTPS client for node.js. Supports transparent gzip / deflate decoding.", |
@@ -109,2 +109,64 @@ 'use strict'; | ||
describe('GET Basic Authentication with auth option', function() { | ||
it('should pass back the basic authentication', function(done) { | ||
var basicAuth = { | ||
type: 'basic', | ||
username: 'user@example.com', | ||
password: 'pass#word' | ||
}; | ||
client.get({ | ||
url: 'http://127.0.0.1:' + common.options.port + '/basic-auth', | ||
auth: basicAuth | ||
}, function(err, res) { | ||
assert.isNull(err, 'we have an error'); | ||
assert.strictEqual(res.headers['x-http-method'], 'GET', 'the method is GET'); | ||
var auth = JSON.parse(res.buffer.toString()); | ||
assert.strictEqual(auth.username, basicAuth.username); | ||
assert.strictEqual(auth.password, basicAuth.password); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('GET invalid type for the auth option', function() { | ||
it('should throw an Error', function(done) { | ||
var throws = function() { | ||
client.get({ | ||
url: 'http://127.0.0.1:' + common.options.port + '/basic-auth', | ||
auth: 'foo' | ||
}, function(err) { | ||
assert.ifError(err); | ||
}); | ||
}; | ||
assert.throws(throws, Error, 'Expecting an Object for the auth option.'); | ||
done(); | ||
}); | ||
}); | ||
describe('GET invalid option.auth.type', function() { | ||
it('should throw an Error', function(done) { | ||
var throws = function() { | ||
client.get({ | ||
url: 'http://127.0.0.1:' + common.options.port + '/basic-auth', | ||
auth: { | ||
type: 'foo' | ||
} | ||
}, function(err) { | ||
assert.ifError(err); | ||
}); | ||
}; | ||
assert.throws(throws, Error, 'Unknown type for the auth option.'); | ||
done(); | ||
}); | ||
}); | ||
describe('GET broken DNS name over HTTP', function() { | ||
@@ -546,4 +608,4 @@ it('should fail with an error passed back to the completion callback', function(done) { | ||
// On Travis CI this response comes back short at 1043957 bytes | ||
// Therefore, this test always fails under node 0.8.25 | ||
if (process.env.TRAVIS_NODE_VERSION !== '0.8') { | ||
// Therefore, this test always fails under Travis CI | ||
if (!process.env.TRAVIS_NODE_VERSION) { | ||
describe('GET with overflowing error document', function() { | ||
@@ -550,0 +612,0 @@ it('should detect the situation and act accordingly', function(done) { |
1965124
4149