Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for HTTPS proxies #580

Merged
merged 1 commit into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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