Skip to content

Commit

Permalink
Merge pull request #580 from stripe/ob-fix-579
Browse files Browse the repository at this point in the history
Fix support for HTTPS proxies
  • Loading branch information
ob-stripe authored Mar 14, 2019
2 parents 44fd7d3 + e67dd8c commit dbcd7d1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 47 deletions.
8 changes: 7 additions & 1 deletion lib/StripeResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
17 changes: 2 additions & 15 deletions lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 0 additions & 31 deletions test/stripe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var stripe = require('../lib/stripe')(
);

var http = require('http');
var https = require('https');

var expect = require('chai').expect;

Expand All @@ -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) {
Expand Down

0 comments on commit dbcd7d1

Please sign in to comment.