diff --git a/lib/client.js b/lib/client.js index 2bd58515e..24417ec17 100644 --- a/lib/client.js +++ b/lib/client.js @@ -27,7 +27,21 @@ function noop(){}; * Expose `request`. */ -var request = module.exports = require('./request').bind(null, Request); +var request = exports = module.exports = function(method, url) { + // callback + if ('function' == typeof url) { + return new exports.Request('GET', method).end(url); + } + + // url first + if (1 == arguments.length) { + return new exports.Request('GET', method); + } + + return new exports.Request(method, url); +} + +exports.Request = Request; /** * Determine XHR. @@ -890,13 +904,6 @@ Request.prototype.end = function(fn){ return this; }; - -/** - * Expose `Request`. - */ - -request.Request = Request; - /** * GET `url` with optional callback `fn(res)`. * diff --git a/lib/node/agent.js b/lib/node/agent.js index 002796356..ad2a8f61b 100644 --- a/lib/node/agent.js +++ b/lib/node/agent.js @@ -73,7 +73,7 @@ methods.forEach(function(method){ method = method.toUpperCase(); Agent.prototype[name] = function(url, fn){ - var req = request(method, url); + var req = new request.Request(method, url); req.ca(this._ca); req.key(this._key); req.pfx(this._pfx); diff --git a/lib/node/index.js b/lib/node/index.js index 923290e96..10dcbd9a7 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -25,11 +25,25 @@ var pkg = require('../../package.json'); var RequestBase = require('../request-base'); var isFunction = require('../is-function'); +var request = exports = module.exports = function(method, url) { + // callback + if ('function' == typeof url) { + return new exports.Request('GET', method).end(url); + } + + // url first + if (1 == arguments.length) { + return new exports.Request('GET', method); + } + + return new exports.Request(method, url); +} + /** - * Expose the request function. + * Expose `Request`. */ -var request = exports = module.exports = require('../request').bind(null, Request); +exports.Request = Request; /** * Expose the agent function @@ -919,12 +933,6 @@ Request.prototype._shouldUnzip = function(res){ return /^\s*(?:deflate|gzip)\s*$/.test(res.headers['content-encoding']); }; -/** - * Expose `Request`. - */ - -exports.Request = Request; - // generate HTTP verb methods if (methods.indexOf('del') == -1) { // create a copy so we don't cause conflicts with diff --git a/lib/request.js b/lib/request.js deleted file mode 100644 index 53a3e6ab8..000000000 --- a/lib/request.js +++ /dev/null @@ -1,32 +0,0 @@ -// The node and browser modules expose versions of this with the -// appropriate constructor function bound as first argument -/** - * Issue a request: - * - * Examples: - * - * request('GET', '/users').end(callback) - * request('/users').end(callback) - * request('/users', callback) - * - * @param {String} method - * @param {String|Function} url or callback - * @return {Request} - * @api public - */ - -function request(RequestConstructor, method, url) { - // callback - if ('function' == typeof url) { - return new RequestConstructor('GET', method).end(url); - } - - // url first - if (2 == arguments.length) { - return new RequestConstructor('GET', method); - } - - return new RequestConstructor(method, url); -} - -module.exports = request; diff --git a/test/use.js b/test/use.js index bd5bce021..c24232ce1 100644 --- a/test/use.js +++ b/test/use.js @@ -1,5 +1,4 @@ var setup = require('./support/setup'); -var NODE = setup.NODE; var uri = setup.uri; var assert = require('assert'); @@ -27,6 +26,58 @@ describe('request', function(){ assert.equal(res.get('X-UUID'), now); done(); }) - }) - }) + }); + }); +}) + +describe('subclass', function() { + var OriginalRequest; + beforeEach(function(){ + OriginalRequest = request.Request; + }); + afterEach(function(){ + request.Request = OriginalRequest; + }); + + it('should be an instance of Request', function(){ + var req = request.get('/'); + assert(req instanceof request.Request); + }); + + it('should use patched subclass', function(){ + assert(OriginalRequest); + + var constructorCalled, sendCalled; + function NewRequest() { + constructorCalled = true; + OriginalRequest.apply(this, arguments); + } + NewRequest.prototype = Object.create(OriginalRequest.prototype); + NewRequest.prototype.send = function() { + sendCalled = true; + return this; + }; + + request.Request = NewRequest; + + var req = request.get('/').send(); + assert(constructorCalled); + assert(sendCalled); + assert(req instanceof NewRequest); + assert(req instanceof OriginalRequest); + }); + + it('should use patched subclass in agent too', function(){ + if (!request.agent) return; // Node-only + + function NewRequest() { + OriginalRequest.apply(this, arguments); + } + NewRequest.prototype = Object.create(OriginalRequest.prototype); + request.Request = NewRequest; + + var req = request.agent().del('/'); + assert(req instanceof NewRequest); + assert(req instanceof OriginalRequest); + }); })