Skip to content

Commit dbcd7d1

Browse files
authored
Merge pull request #580 from stripe/ob-fix-579
Fix support for HTTPS proxies
2 parents 44fd7d3 + e67dd8c commit dbcd7d1

File tree

3 files changed

+9
-47
lines changed

3 files changed

+9
-47
lines changed

lib/StripeResource.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ var Error = require('./Error');
1010

1111
var hasOwn = {}.hasOwnProperty;
1212

13+
var defaultHttpAgent = new http.Agent({keepAlive: true});
14+
var defaultHttpsAgent = new https.Agent({keepAlive: true});
15+
1316
// Provide extension mechanism for Stripe Resource Sub-Classes
1417
StripeResource.extend = utils.protoExtend;
1518

@@ -356,7 +359,10 @@ StripeResource.prototype = {
356359
function makeRequest(apiVersion, headers, numRetries) {
357360
var timeout = self._stripe.getApiField('timeout');
358361
var isInsecureConnection = self._stripe.getApiField('protocol') == 'http';
359-
var agent = isInsecureConnection ? self._stripe.getApiField('http_agent') : self._stripe.getApiField('https_agent');
362+
var agent = self._stripe.getApiField('agent');
363+
if (agent == null) {
364+
agent = isInsecureConnection ? defaultHttpAgent : defaultHttpsAgent;
365+
}
360366

361367
var req = (
362368
isInsecureConnection ? http : https

lib/stripe.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ Stripe.INITIAL_NETWORK_RETRY_DELAY_SEC = 0.5;
2626

2727
var APP_INFO_PROPERTIES = ['name', 'version', 'url', 'partner_id'];
2828

29-
var http = require('http');
30-
var https = require('https');
31-
3229
var EventEmitter = require('events').EventEmitter;
3330
var exec = require('child_process').exec;
3431
var utils = require('./utils');
@@ -148,8 +145,7 @@ function Stripe(key, version) {
148145
basePath: Stripe.DEFAULT_BASE_PATH,
149146
version: Stripe.DEFAULT_API_VERSION,
150147
timeout: Stripe.DEFAULT_TIMEOUT,
151-
http_agent: this._buildDefaultAgent('http'),
152-
https_agent: this._buildDefaultAgent('https'),
148+
agent: null,
153149
dev: false,
154150
maxNetworkRetries: 0,
155151
};
@@ -237,11 +233,7 @@ Stripe.prototype = {
237233
},
238234

239235
setHttpAgent: function(agent) {
240-
if (agent instanceof https.Agent) {
241-
this._setApiField('https_agent', agent);
242-
} else {
243-
this._setApiField('http_agent', agent);
244-
}
236+
this._setApiField('agent', agent);
245237
},
246238

247239
_setApiField: function(key, value) {
@@ -344,11 +336,6 @@ Stripe.prototype = {
344336
return this._enableTelemetry;
345337
},
346338

347-
_buildDefaultAgent: function(protocol) {
348-
var httpLib = protocol === 'http' ? http : https;
349-
return new httpLib.Agent({keepAlive: true});
350-
},
351-
352339
_prepResources: function() {
353340
for (var name in resources) {
354341
this[utils.pascalToCamelCase(name)] = new resources[name](this);

test/stripe.spec.js

-31
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var stripe = require('../lib/stripe')(
77
);
88

99
var http = require('http');
10-
var https = require('https');
1110

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

@@ -26,36 +25,6 @@ describe('Stripe Module', function() {
2625
});
2726
});
2827

29-
describe('setHttpAgent', function() {
30-
var origHttpAgent, origHttpsAgent;
31-
beforeEach(function() {
32-
origHttpAgent = stripe.getApiField('http_agent');
33-
origHttpsAgent = stripe.getApiField('https_agent');
34-
stripe._setApiField('http_agent', null);
35-
stripe._setApiField('https_agent', null);
36-
});
37-
afterEach(function() {
38-
stripe._setApiField('http_agent', origHttpAgent);
39-
stripe._setApiField('https_agent', origHttpsAgent);
40-
});
41-
describe('when given an https.Agent', function() {
42-
it('should save the agent as https_agent', function() {
43-
var agent = new https.Agent();
44-
stripe.setHttpAgent(agent);
45-
expect(stripe.getApiField('https_agent')).to.equal(agent);
46-
expect(stripe.getApiField('http_agent')).to.be.null;
47-
});
48-
});
49-
describe('when given an http.Agent', function() {
50-
it('should save the agent as http_agent', function() {
51-
var agent = new http.Agent();
52-
stripe.setHttpAgent(agent);
53-
expect(stripe.getApiField('http_agent')).to.equal(agent);
54-
expect(stripe.getApiField('https_agent')).to.be.null;
55-
});
56-
});
57-
});
58-
5928
describe('GetClientUserAgent', function() {
6029
it('Should return a user-agent serialized JSON object', function() {
6130
return expect(new Promise(function(resolve, reject) {

0 commit comments

Comments
 (0)