diff --git a/lib/StripeResource.js b/lib/StripeResource.js index e004b64cb3..f374539841 100644 --- a/lib/StripeResource.js +++ b/lib/StripeResource.js @@ -10,6 +10,9 @@ var Error = require('./Error'); var hasOwn = {}.hasOwnProperty; +var defaultHttpAgent = new http.Agent({keepAlive: true}); +var defaultHttpsAgent = new https.Agent({keepAlive: true}); + // Provide extension mechanism for Stripe Resource Sub-Classes StripeResource.extend = utils.protoExtend; @@ -356,7 +359,10 @@ StripeResource.prototype = { function makeRequest(apiVersion, headers, numRetries) { var timeout = self._stripe.getApiField('timeout'); var isInsecureConnection = self._stripe.getApiField('protocol') == 'http'; - var agent = isInsecureConnection ? self._stripe.getApiField('http_agent') : self._stripe.getApiField('https_agent'); + var agent = self._stripe.getApiField('agent'); + if (agent == null) { + agent = isInsecureConnection ? defaultHttpAgent : defaultHttpsAgent; + } var req = ( isInsecureConnection ? http : https diff --git a/lib/stripe.js b/lib/stripe.js index 356a8633ca..137b2e9780 100644 --- a/lib/stripe.js +++ b/lib/stripe.js @@ -26,9 +26,6 @@ Stripe.INITIAL_NETWORK_RETRY_DELAY_SEC = 0.5; var APP_INFO_PROPERTIES = ['name', 'version', 'url', 'partner_id']; -var http = require('http'); -var https = require('https'); - var EventEmitter = require('events').EventEmitter; var exec = require('child_process').exec; var utils = require('./utils'); @@ -148,8 +145,7 @@ function Stripe(key, version) { basePath: Stripe.DEFAULT_BASE_PATH, version: Stripe.DEFAULT_API_VERSION, timeout: Stripe.DEFAULT_TIMEOUT, - http_agent: this._buildDefaultAgent('http'), - https_agent: this._buildDefaultAgent('https'), + agent: null, dev: false, maxNetworkRetries: 0, }; @@ -237,11 +233,7 @@ Stripe.prototype = { }, setHttpAgent: function(agent) { - if (agent instanceof https.Agent) { - this._setApiField('https_agent', agent); - } else { - this._setApiField('http_agent', agent); - } + this._setApiField('agent', agent); }, _setApiField: function(key, value) { @@ -344,11 +336,6 @@ Stripe.prototype = { return this._enableTelemetry; }, - _buildDefaultAgent: function(protocol) { - var httpLib = protocol === 'http' ? http : https; - return new httpLib.Agent({keepAlive: true}); - }, - _prepResources: function() { for (var name in resources) { this[utils.pascalToCamelCase(name)] = new resources[name](this); diff --git a/test/stripe.spec.js b/test/stripe.spec.js index 2546d4f3f5..99aa5da2f0 100644 --- a/test/stripe.spec.js +++ b/test/stripe.spec.js @@ -7,7 +7,6 @@ var stripe = require('../lib/stripe')( ); var http = require('http'); -var https = require('https'); var expect = require('chai').expect; @@ -26,36 +25,6 @@ describe('Stripe Module', function() { }); }); - describe('setHttpAgent', function() { - var origHttpAgent, origHttpsAgent; - beforeEach(function() { - origHttpAgent = stripe.getApiField('http_agent'); - origHttpsAgent = stripe.getApiField('https_agent'); - stripe._setApiField('http_agent', null); - stripe._setApiField('https_agent', null); - }); - afterEach(function() { - stripe._setApiField('http_agent', origHttpAgent); - stripe._setApiField('https_agent', origHttpsAgent); - }); - describe('when given an https.Agent', function() { - it('should save the agent as https_agent', function() { - var agent = new https.Agent(); - stripe.setHttpAgent(agent); - expect(stripe.getApiField('https_agent')).to.equal(agent); - expect(stripe.getApiField('http_agent')).to.be.null; - }); - }); - describe('when given an http.Agent', function() { - it('should save the agent as http_agent', function() { - var agent = new http.Agent(); - stripe.setHttpAgent(agent); - expect(stripe.getApiField('http_agent')).to.equal(agent); - expect(stripe.getApiField('https_agent')).to.be.null; - }); - }); - }); - describe('GetClientUserAgent', function() { it('Should return a user-agent serialized JSON object', function() { return expect(new Promise(function(resolve, reject) {