Comparing version 5.5.1 to 5.6.0
@@ -18,3 +18,5 @@ | ||
var internals = {}; | ||
var internals = { | ||
jsonRegex: /^application\/[a-z.+-]*json$/ | ||
}; | ||
@@ -24,3 +26,3 @@ | ||
internals.Client = function () { | ||
internals.Client = function (defaults) { | ||
@@ -34,2 +36,4 @@ Events.EventEmitter.call(this); | ||
}; | ||
this._defaults = defaults || {}; | ||
}; | ||
@@ -40,2 +44,26 @@ | ||
internals.Client.prototype.defaults = function (options) { | ||
options = Hoek.applyToDefaultsWithShallow(options, this._defaults, ['agent', 'payload', 'downstreamRes']); | ||
return new internals.Client(options); | ||
}; | ||
internals.resolveUrl = function (baseUrl, path) { | ||
if (!path) { | ||
return baseUrl; | ||
} | ||
var parsedBase = Url.parse(baseUrl); | ||
var parsedPath = Url.parse(path); | ||
parsedBase.pathname += parsedPath.pathname; | ||
parsedBase.pathname = parsedBase.pathname.replace(/[/]{2,}/g, '/'); | ||
parsedBase.search = parsedPath.search; // Always use the querystring from the path argument | ||
return Url.format(parsedBase); | ||
}; | ||
internals.Client.prototype.request = function (method, url, options, callback, _trace) { | ||
@@ -45,3 +73,3 @@ | ||
options = options || {}; | ||
options = Hoek.applyToDefaultsWithShallow(options || {}, this._defaults, ['agent', 'payload', 'downstreamRes']); | ||
@@ -55,3 +83,5 @@ Hoek.assert(options.payload === null || options.payload === undefined || typeof options.payload === 'string' || | ||
// Setup request | ||
if (options.baseUrl) { | ||
url = internals.resolveUrl(options.baseUrl, url); | ||
} | ||
@@ -229,4 +259,5 @@ var uri = Url.parse(url); | ||
options = options || {}; | ||
options = Hoek.applyToDefaultsWithShallow(options || {}, this._defaults, ['agent', 'payload', 'downstreamRes']); | ||
// Set stream timeout | ||
@@ -278,3 +309,3 @@ | ||
if (mime !== 'application/json') { | ||
if (!internals.jsonRegex.test(mime)) { | ||
return callback(null, buffer); | ||
@@ -350,3 +381,3 @@ } | ||
var header = {}; | ||
var err = field.replace(regex, function ($0, $1, $2, $3) { | ||
var error = field.replace(regex, function ($0, $1, $2, $3) { | ||
@@ -370,3 +401,3 @@ var value = $2 || $3; | ||
return (err ? null : header); | ||
return (error ? null : header); | ||
}; | ||
@@ -402,2 +433,3 @@ | ||
// Wrapper so that shortcut can be optimized with required params | ||
internals.Client.prototype._shortcutWrap = function (method, uri /* [options], callback */) { | ||
@@ -404,0 +436,0 @@ |
{ | ||
"name": "wreck", | ||
"description": "HTTP Client Utilities", | ||
"version": "5.5.1", | ||
"version": "5.6.0", | ||
"repository": "git://github.com/hapijs/wreck", | ||
"main": "index", | ||
"main": "lib/index", | ||
"keywords": [ | ||
@@ -24,10 +24,6 @@ "utilities", | ||
"scripts": { | ||
"test": "make test-cov" | ||
"test": "lab -t 100 -L -a code", | ||
"test-cov-html": "lab -r html -o coverage.html -a code" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "BSD", | ||
"url": "http://github.com/hapijs/wreck/raw/master/LICENSE" | ||
} | ||
] | ||
"license": "BSD-3-Clause" | ||
} |
@@ -28,4 +28,16 @@ ![wreck Logo](https://raw.github.com/hapijs/wreck/master/images/wreck.png) | ||
var wreck = Wreck.defaults({ | ||
headers: { 'x-foo-bar': 123 } | ||
}); | ||
// cascading example -- does not alter `wreck` | ||
var wreckWithTimeout = wreck.defaults({ | ||
timeout: 5 | ||
}); | ||
// all attributes are optional | ||
var options = { | ||
baseUrl: fully qualified uri string used as the base url. Most useful with `request.defaults`, for example when you want to do many requests to the same domain. | ||
If `baseUrl` is `https://example.com/api/`, then requesting `/end/point?test=true` will fetch `https://example.com/api/end/point?test=true`. Any | ||
querystring in the `baseUrl` will be overwritten with the querystring in the `uri` When `baseUrl` is given, `uri` must also be a string. | ||
payload: readableStream || 'foo=bar' || new Buffer('foo=bar'), | ||
@@ -52,5 +64,10 @@ headers: { /* http headers */ }, | ||
var req = Wreck.request(method, uri, options, optionalCallback); | ||
var req = wreck.request(method, uri, options, optionalCallback); | ||
``` | ||
### `defaults(options)` | ||
Returns a *new* instance of Wreck which merges the provided `options` with those provided on a per-request basis. You can call defaults repeatedly to build up multiple http clients. | ||
- `options` - Config object containing settings for both `request` and `read` operations. | ||
### `request(method, uri, [options, [callback]])` | ||
@@ -215,3 +232,3 @@ | ||
This event is useful for logging all requests that go through *wreck*. | ||
This event is useful for logging all requests that go through *wreck*. | ||
The error and response arguments can be undefined depending on if an error occurs. Please be aware that if multiple | ||
@@ -218,0 +235,0 @@ modules are depending on the same cached *wreck* module that this event can fire for each request made across all |
@@ -10,2 +10,3 @@ // Load modules | ||
var Code = require('code'); | ||
var Hoek = require('hoek'); | ||
var Lab = require('lab'); | ||
@@ -808,2 +809,3 @@ var Wreck = require('../'); | ||
var server = Http.createServer(function (req, res) { | ||
res.statusCode = 500; | ||
@@ -965,2 +967,77 @@ res.end(); | ||
describe('options.baseUrl', function () { | ||
it('uses baseUrl option with trailing slash and uri is prefixed with a slash', function (done) { | ||
var r = Wreck.request('get', '/foo', { baseUrl: 'http://localhost/' }, function (err, res) { | ||
expect(r._headers.host).to.equal('localhost'); | ||
done(); | ||
}); | ||
}); | ||
it('uses baseUrl option without trailing slash and uri is prefixed with a slash', function (done) { | ||
var request = Wreck.request('get', '/foo', { baseUrl: 'http://localhost' }, Hoek.ignore); | ||
expect(request._headers.host).to.equal('localhost'); | ||
expect(request.path).to.equal('/foo'); | ||
done(); | ||
}); | ||
it('uses baseUrl option with trailing slash and uri is prefixed without a slash', function (done) { | ||
var request = Wreck.request('get', 'foo', { baseUrl: 'http://localhost/' }, Hoek.ignore); | ||
expect(request._headers.host).to.equal('localhost'); | ||
expect(request.path).to.equal('/foo'); | ||
done(); | ||
}); | ||
it('uses baseUrl option without trailing slash and uri is prefixed without a slash', function (done) { | ||
var request = Wreck.request('get', 'foo', { baseUrl: 'http://localhost' }, Hoek.ignore); | ||
expect(request._headers.host).to.equal('localhost'); | ||
expect(request.path).to.equal('/foo'); | ||
done(); | ||
}); | ||
it('uses baseUrl option when uri is an empty string', function (done) { | ||
var request = Wreck.request('get', '', { baseUrl: 'http://localhost' }, Hoek.ignore); | ||
expect(request._headers.host).to.equal('localhost'); | ||
expect(request.path).to.equal('/'); | ||
done(); | ||
}); | ||
it('uses baseUrl option with a path', function (done) { | ||
var request = Wreck.request('get', '/bar', { baseUrl: 'http://localhost/foo' }, Hoek.ignore); | ||
expect(request._headers.host).to.equal('localhost'); | ||
expect(request.path).to.equal('/foo/bar'); | ||
done(); | ||
}); | ||
it('uses baseUrl option with a path and removes extra slashes', function (done) { | ||
var request = Wreck.request('get', '/bar', { baseUrl: 'http://localhost/foo/' }, Hoek.ignore); | ||
expect(request._headers.host).to.equal('localhost'); | ||
expect(request.path).to.equal('/foo/bar'); | ||
done(); | ||
}); | ||
it('uses baseUrl option with a url that has a querystring', function (done) { | ||
var request = Wreck.request('get', '/bar?test=hello', { baseUrl: 'http://localhost/foo' }, Hoek.ignore); | ||
expect(request._headers.host).to.equal('localhost'); | ||
expect(request.path).to.equal('/foo/bar?test=hello'); | ||
done(); | ||
}); | ||
}); | ||
describe('read()', function () { | ||
@@ -1152,3 +1229,2 @@ | ||
}); | ||
}); | ||
@@ -1328,2 +1404,29 @@ | ||
it('json-based type requested and received', function (done) { | ||
var server = Http.createServer(function (req, res) { | ||
res.writeHead(200, { 'Content-Type': 'application/vnd.api+json' }); | ||
res.end(JSON.stringify({ foo: 'bar' })); | ||
}); | ||
server.listen(0, function () { | ||
var port = server.address().port; | ||
var options = { | ||
json: true | ||
}; | ||
Wreck.get('http://localhost:' + port, options, function (err, res, payload) { | ||
expect(err).to.not.exist(); | ||
expect(res.statusCode).to.equal(200); | ||
expect(payload).to.not.equal(null); | ||
expect(payload.foo).to.exist(); | ||
server.close(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('json requested but not received - flag is ignored', function (done) { | ||
@@ -1490,5 +1593,5 @@ | ||
var stream = Wreck.toReadableStream(); | ||
expect(stream instanceof Stream).to.be.true; | ||
expect(stream instanceof Stream).to.be.true(); | ||
var read = stream.read(); // Make sure read has no problems | ||
expect(read).to.be.null; | ||
expect(read).to.be.null(); | ||
done(); | ||
@@ -1502,3 +1605,3 @@ }); | ||
var stream = Wreck.toReadableStream(data, 'ascii'); | ||
expect(stream instanceof Stream).to.be.true; | ||
expect(stream instanceof Stream).to.be.true(); | ||
var read = stream.read(); | ||
@@ -1510,2 +1613,3 @@ expect(read.toString()).to.equal(data); | ||
it('chunks to requested size', function (done) { | ||
var buf; | ||
@@ -1564,3 +1668,3 @@ var data = new Array(101).join('0123456789'); | ||
it('response event includes error when it occurs', function (done) { | ||
it('response event includes error when it occurs', { timeout: 2500 }, function (done) { | ||
@@ -1579,5 +1683,6 @@ Wreck.once('response', function (err, req, res) { | ||
}); | ||
}); | ||
it('multiple requests execute the same response handler', function (done) { | ||
it('multiple requests execute the same response handler', { timeout: 5000 }, function (done) { | ||
@@ -1608,2 +1713,51 @@ var count = 0; | ||
}); | ||
it('rejects attempts to use defaults without an options hash', function (done) { | ||
var fn = function () { | ||
Wreck.defaults(); | ||
}; | ||
expect(fn).to.throw(); | ||
done(); | ||
}); | ||
it('respects defaults without bleeding across instances', function (done) { | ||
var req; | ||
var optionsA = { headers: { foo: 123 } }; | ||
var optionsB = { headers: { bar: 321 } }; | ||
var wreckA = Wreck.defaults(optionsA); | ||
var wreckB = Wreck.defaults(optionsB); | ||
var wreckAB = wreckA.defaults(optionsB); | ||
// var agent = new Http.Agent(); | ||
// expect(Object.keys(agent.sockets).length).to.equal(0); | ||
req = wreckA.request('get', 'http://localhost/', { headers: { banana: 911 } }, function (err, res) { | ||
expect(req._headers.banana).to.exist(); | ||
expect(req._headers.foo).to.exist(); | ||
expect(req._headers.bar).to.not.exist(); | ||
req = wreckB.request('get', 'http://localhost/', { headers: { banana: 911 } }, function (err, res) { | ||
expect(req._headers.banana).to.exist(); | ||
expect(req._headers.foo).to.not.exist(); | ||
expect(req._headers.bar).to.exist(); | ||
req = wreckAB.request('get', 'http://localhost/', { headers: { banana: 911 } }, function (err, res) { | ||
expect(req._headers.banana).to.exist(); | ||
expect(req._headers.foo).to.exist(); | ||
expect(req._headers.bar).to.exist(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115614
1623
235
12