Skip to content

Commit

Permalink
simplify code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emirotin committed Apr 25, 2017
1 parent 00bac91 commit 7de1270
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 66 deletions.
86 changes: 38 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,33 +128,24 @@ globalTunnel.initialize = function(conf) {
if (!conf.connect) {
conf.connect = 'https'; // just HTTPS by default
}
switch(conf.connect) {
case 'both':
conf.connectHttp = true;
conf.connectHttps = true;
break;
case 'neither':
conf.connectHttp = false;
conf.connectHttps = false;
break;
case 'https':
conf.connectHttp = false;
conf.connectHttps = true;
break;
default:

if (['both', 'neither', 'https'].indexOf(conf.connect) < 0) {
throw new Error('valid connect options are "neither", "https", or "both"');
}

var connectHttp = (conf.connect === 'both');
var connectHttps = (conf.connect !== 'neither');

if (conf.httpsOptions) {
conf.outerHttpsOpts = conf.innerHttpsOpts = conf.httpsOptions;
}

try {
http.globalAgent = globalTunnel._makeAgent(conf, 'http', conf.connectHttp);
https.globalAgent = globalTunnel._makeAgent(conf, 'https', conf.connectHttps);
http.globalAgent = globalTunnel._makeAgent(conf, 'http', connectHttp);
https.globalAgent = globalTunnel._makeAgent(conf, 'https', connectHttps);

http.request = globalTunnel._defaultedAgentRequest.bind(http, 'http');
https.request = globalTunnel._defaultedAgentRequest.bind(https, 'https');
http.request = globalTunnel._makeRequest(http, 'http');
https.request = globalTunnel._makeRequest(https, 'https');

globalTunnel.isProxying = true;
} catch (e) {
Expand Down Expand Up @@ -230,43 +221,42 @@ globalTunnel._makeAgent = function(conf, innerProtocol, useCONNECT) {
* to the global agent. Due to how node implements it in lib/http.js, the
* globalAgent we define won't get used (node uses a module-scoped variable,
* not the exports field).
* @param {string} protocol bound during initialization
* @param {string|object} options http/https request url or options
* @param {function} [cb]
* @private
*/
globalTunnel._defaultedAgentRequest = function(protocol, options, callback) {
var httpOrHttps = this;

if (typeof options === 'string') {
options = urlParse(options);
} else {
options = clone(options);
}
globalTunnel._makeRequest = function(httpOrHttps, protocol) {
return function(options, callback) {
if (typeof options === 'string') {
options = urlParse(options);
} else {
options = clone(options);
}

// Respect the default agent provided by node's lib/https.js
var defaultAgent = options._defaultAgent || httpOrHttps.globalAgent;
// repeat the logic from node's lib/http.js
var agent = options.agent;
if (agent === false) {
// Node does build the new agent with default props in this case,
// but we want to reuse the same global agent
agent = defaultAgent;
} else if ((agent === null || agent === undefined) &&
typeof options.createConnection !== 'function') {
agent = defaultAgent;
}
options.agent = agent;
// Respect the default agent provided by node's lib/https.js
var defaultAgent = options._defaultAgent || httpOrHttps.globalAgent;
// repeat the logic from node's lib/http.js
var agent = options.agent;
if (agent === false) {
// Node does build the new agent with default props in this case,
// but we want to reuse the same global agent
agent = defaultAgent;
} else if ((agent === null || agent === undefined) &&
typeof options.createConnection !== 'function') {
agent = defaultAgent;
}
options.agent = agent;

// set the default port purselves to prevent Node doing it based on the proxy agent protocol
if (options.protocol === 'https:' || (!options.protocol && protocol === 'https')) {
options.port = options.port || 443;
}
if (options.protocol === 'http:' || (!options.protocol && protocol === 'http')) {
options.port = options.port || 80;
}
// set the default port purselves to prevent Node doing it based on the proxy agent protocol
if (options.protocol === 'https:' || (!options.protocol && protocol === 'https')) {
options.port = options.port || 443;
}
if (options.protocol === 'http:' || (!options.protocol && protocol === 'http')) {
options.port = options.port || 80;
}

return ORIGINALS[protocol].request.call(httpOrHttps, options, callback);
return ORIGINALS[protocol].request.call(httpOrHttps, options, callback);
};
};

/**
Expand Down
37 changes: 19 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';
var assert = require('goinstant-assert');
var sinon = require('sinon');
var assign = require('lodash/assign');

// deliberate: node and 3rd party modules before global-tunnel
var EventEmitter = require('events').EventEmitter;
Expand Down Expand Up @@ -275,65 +276,65 @@ describe('global-proxy', function() {
globalTunnel.end();
});

testParams = assign({
port: conf && conf.port,
secure: conf && conf.protocol === 'https:',
connect: conf && conf.connect || 'https'
}, testParams);

proxyEnabledTests(testParams);
}

describe('with http proxy in intercept mode', function() {
var conf = {
enabledBlock({
connect: 'neither',
protocol: 'http:',
host: '10.2.3.4',
port: 3333
};
enabledBlock(conf, { secure: false, connect: 'neither', port: 3333 });
});
});

describe('with https proxy in intercept mode', function() {
var conf = {
enabledBlock({
connect: 'neither',
protocol: 'https:',
host: '10.2.3.4',
port: 3334
};
enabledBlock(conf, { secure: true, connect: 'neither', port: 3334 });
});
});

describe('with http proxy in CONNECT mode', function() {
var conf = {
enabledBlock({
connect: 'both',
protocol: 'http:',
host: '10.2.3.4',
port: 3335
};
enabledBlock(conf, { secure: false, connect: 'both', port: 3335 });
});
});

describe('with https proxy in CONNECT mode', function() {
var conf = {
enabledBlock({
connect: 'both',
protocol: 'https:',
host: '10.2.3.4',
port: 3336
};
enabledBlock(conf, { secure: true, connect: 'both', port: 3336 });
});
});

describe('with http proxy in mixed mode', function() {
var conf = {
enabledBlock({
protocol: 'http:',
host: '10.2.3.4',
port: 3337
};
enabledBlock(conf, { secure: false, connect: 'https', port: 3337 });
});
});

describe('with https proxy in mixed mode', function() {
var conf = {
enabledBlock({
protocol: 'https:',
host: '10.2.3.4',
port: 3338
};
enabledBlock(conf, { secure: true, connect: 'https', port: 3338 });
});
});


Expand Down

0 comments on commit 7de1270

Please sign in to comment.