Skip to content

Commit

Permalink
Merge pull request #85 from jmonster/defaults
Browse files Browse the repository at this point in the history
add support for defaults
  • Loading branch information
geek committed Jun 19, 2015
2 parents 3063259 + 1a378f4 commit d86b7b6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ var method = 'GET'; // GET, POST, PUT, DELETE
var uri = 'https://google.com/';
var readableStream = Wreck.toReadableStream('foo=bar');

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 = {
payload: readableStream || 'foo=bar' || new Buffer('foo=bar'),
Expand All @@ -48,9 +57,14 @@ var optionalCallback = function (err, res) {
});
};

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]])`

Initiate an HTTP request.
Expand Down Expand Up @@ -210,7 +224,7 @@ arguments `(error, request, response, start, uri)` where:
- `uri` - the result of `Url.parse(uri)`. This will provide information about the resource requested. Also includes
the headers and method.

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
modules are depending on the same cached *wreck* module that this event can fire for each request made across all
modules. The start argument is the timestamp when the request was started. This can be useful for determining how long
Expand Down
15 changes: 12 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var internals = {

// new instance is exported as module.exports

internals.Client = function () {
internals.Client = function (defaults) {

Events.EventEmitter.call(this);

Expand All @@ -31,16 +31,24 @@ internals.Client = function () {
http: new Http.Agent({ maxSockets: Infinity }),
httpsAllowUnauthorized: new Https.Agent({ maxSockets: Infinity, rejectUnauthorized: false })
};

this._defaults = defaults || {};
};

Hoek.inherits(internals.Client, Events.EventEmitter);

internals.Client.prototype.defaults = function (options) {

options = Hoek.applyToDefaultsWithShallow(options, this._defaults, ['agent', 'payload', 'downstreamRes']);
return new internals.Client(options);
};


internals.Client.prototype.request = function (method, url, options, callback, _trace) {

var self = this;

options = options || {};
options = Hoek.applyToDefaultsWithShallow(options || {}, this._defaults, ['agent', 'payload', 'downstreamRes']);

Hoek.assert(options.payload === null || options.payload === undefined || typeof options.payload === 'string' ||
options.payload instanceof Stream || Buffer.isBuffer(options.payload),
Expand Down Expand Up @@ -222,7 +230,8 @@ internals.Client.prototype.request = function (method, url, options, callback, _

internals.Client.prototype.read = function (res, options, callback) {

options = options || {};
options = Hoek.applyToDefaultsWithShallow(options || {}, this._defaults, ['agent', 'payload', 'downstreamRes']);


// Set stream timeout

Expand Down
52 changes: 52 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1625,4 +1625,56 @@ describe('Events', function () {
});
});
});

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();
});
});

});


});
});

0 comments on commit d86b7b6

Please sign in to comment.