index.js 15.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
'use strict';
var assert = require('chai').assert;
var sinon = require('sinon');
var assign = require('lodash/assign');
var pick = require('lodash/pick');

// Deliberate: node and 3rd party modules before global-tunnel
var EventEmitter = require('events').EventEmitter;
var net = require('net');
var tls = require('tls');
var http = require('http');
var globalHttpAgent = http.globalAgent;
var https = require('https');
var globalHttpsAgent = https.globalAgent;
var request = require('request');

// Deliberate: load after all 3rd party modules
var globalTunnel = require('../index');

function newFakeAgent() {
  var fakeAgent = {
    addRequest: sinon.stub()
  };
  return fakeAgent;
}

// This function replaces 'host' by 'hostname' in the options for http.request()
// background: http.request() allows to use either 'host' or 'hostname' to be used,
// both needs to be tested
function replaceHostByHostname(useHostname, options) {
  if (useHostname) {
    options.hostname = options.host;
    delete options.host;
  }
  return options;
}

var origEnv;
function saveEnv() {
  origEnv = process.env.http_proxy;
  delete process.env.http_proxy;
}
function restoreEnv() {
  if (origEnv !== undefined) {
    process.env.http_proxy = origEnv; // eslint-disable-line camelcase
  }
}

describe('global-proxy', function() {
  // Save and restore http_proxy environment variable (yes, it's lower-case by
  // convention).
  before(saveEnv);
  after(restoreEnv);

  // Sinon setup & teardown
  var sandbox;
  var origHttpCreateConnection;

  before(function() {
    sandbox = sinon.createSandbox();

    sandbox.stub(globalHttpAgent, 'addRequest');
    sandbox.stub(globalHttpsAgent, 'addRequest');

    assert.equal(http.Agent.prototype.addRequest, https.Agent.prototype.addRequest);
    sandbox.spy(http.Agent.prototype, 'addRequest');

    sandbox.stub(net, 'createConnection').callsFake(function() {
      return new EventEmitter();
    });
    sandbox.stub(tls, 'connect').callsFake(function() {
      return new EventEmitter();
    });

    // This is needed as at some point Node HTTP aggent implementation started
    // plucking the createConnection method from the `net` module
    // instead of doing `net.createConnection`
    origHttpCreateConnection = http.Agent.prototype.createConnection;
    http.Agent.prototype.createConnection = net.createConnection;
  });

  afterEach(function() {
    sandbox.resetHistory();
  });

  after(function() {
    sandbox.restore();
    http.Agent.prototype.createConnection = origHttpCreateConnection;
  });

  describe('invalid configs', function() {
    it('requires a host', function() {
      var conf = { host: null, port: 1234 };
      assert.throws(function() {
        globalTunnel.initialize(conf);
      }, 'upstream proxy host is required');
      globalTunnel.end();
    });

    it('requires a port', function() {
      var conf = { host: '10.2.3.4', port: 0 };
      assert.throws(function() {
        globalTunnel.initialize(conf);
      }, 'upstream proxy port is required');
      globalTunnel.end();
    });

    it('clamps tunnel types', function() {
      var conf = { host: '10.2.3.4', port: 1234, connect: 'INVALID' };
      assert.throws(function() {
        globalTunnel.initialize(conf);
      }, 'valid connect options are "neither", "https", or "both"');
      globalTunnel.end();
    });
  });

  describe('exposed config', function() {
    afterEach(function() {
      globalTunnel.end();
    });

    it('has the same params as the passed config', function() {
      var conf = {
        host: 'proxy.com',
        port: 1234,
        proxyAuth: 'user:pwd',
        protocol: 'https:'
      };
      globalTunnel.initialize(conf);
      assert.deepEqual(
        conf,
        pick(globalTunnel.proxyConfig, ['host', 'port', 'proxyAuth', 'protocol'])
      );
    });

    it('has the expected defaults', function() {
      var conf = { host: 'proxy.com', port: 1234, proxyAuth: 'user:pwd' };
      globalTunnel.initialize(conf);
      assert.equal(globalTunnel.proxyConfig.protocol, 'http:');
    });
  });

  describe('stringified config', function() {
    afterEach(function() {
      globalTunnel.end();
    });

    it('has the same params as the passed config', function() {
      var conf = {
        host: 'proxy.com',
        port: 1234,
        proxyAuth: 'user:pwd',
        protocol: 'https'
      };
      globalTunnel.initialize(conf);
      assert.equal(globalTunnel.proxyUrl, 'https://user:pwd@proxy.com:1234');
    });

    it('encodes url', function() {
      var conf = {
        host: 'proxy.com',
        port: 1234,
        proxyAuth: 'user:4P@S$W0_r-D',
        protocol: 'https'
      };
      globalTunnel.initialize(conf);
      assert.equal(globalTunnel.proxyUrl, 'https://user:4P%40S%24W0_r-D@proxy.com:1234');
    });
  });

  function proxyEnabledTests(testParams) {
    function connected(innerProto) {
      var innerSecure = innerProto === 'https:';

      var called;
      if (testParams.isHttpsProxy) {
        called = tls.connect;
        sinon.assert.notCalled(net.createConnection);
      } else {
        called = net.createConnection;
        sinon.assert.notCalled(tls.connect);
      }

      sinon.assert.calledOnce(called);
      if (typeof called.getCall(0).args[0] === 'object') {
        sinon.assert.calledWith(called, sinon.match.has('port', testParams.port));
        sinon.assert.calledWith(called, sinon.match.has('host', '10.2.3.4'));
      } else {
        sinon.assert.calledWith(called, testParams.port, '10.2.3.4');
      }

      var isCONNECT =
        testParams.connect === 'both' || (innerSecure && testParams.connect === 'https');
      if (isCONNECT) {
        var expectConnect = 'example.dev:' + (innerSecure ? 443 : 80);
        var whichAgent = innerSecure ? https.globalAgent : http.globalAgent;

        sinon.assert.calledOnce(whichAgent.request);
        sinon.assert.calledWith(whichAgent.request, sinon.match.has('method', 'CONNECT'));
        sinon.assert.calledWith(
          whichAgent.request,
          sinon.match.has('path', expectConnect)
        );
      } else {
        sinon.assert.calledOnce(http.Agent.prototype.addRequest);
        var req = http.Agent.prototype.addRequest.getCall(0).args[0];

        var method = req.method;
        assert.equal(method, 'GET');

        var path = req.path;
        if (innerSecure) {
          assert.match(path, new RegExp('^https://example\\.dev:443/'));
        } else {
          assert.match(path, new RegExp('^http://example\\.dev:80/'));
        }
      }
    }

    var localSandbox;
    beforeEach(function() {
      localSandbox = sinon.createSandbox();
      if (testParams.connect === 'both') {
        localSandbox.spy(http.globalAgent, 'request');
      }
      if (testParams.connect !== 'neither') {
        localSandbox.spy(https.globalAgent, 'request');
      }
    });
    afterEach(function() {
      localSandbox.restore();
    });

    it('(got proxying set up)', function() {
      assert.isTrue(globalTunnel.isProxying);
    });

    describe('with the request library', function() {
      it('will proxy http requests', function(done) {
        assert.isTrue(globalTunnel.isProxying);
        var dummyCb = sinon.stub();
        request.get('http://example.dev/', dummyCb);
        setImmediate(function() {
          connected('http:');
          sinon.assert.notCalled(globalHttpAgent.addRequest);
          sinon.assert.notCalled(globalHttpsAgent.addRequest);
          done();
        });
      });

      it('will proxy https requests', function(done) {
        assert.isTrue(globalTunnel.isProxying);
        var dummyCb = sinon.stub();
        request.get('https://example.dev/', dummyCb);
        setImmediate(function() {
          connected('https:');
          sinon.assert.notCalled(globalHttpAgent.addRequest);
          sinon.assert.notCalled(globalHttpsAgent.addRequest);
          done();
        });
      });
    });

    describe('using raw request interface', function() {
      function rawRequest(useHostname) {
        var req = http.request(
          replaceHostByHostname(useHostname, {
            method: 'GET',
            path: '/raw-http',
            host: 'example.dev'
          }),
          function() {}
        );
        req.end();

        connected('http:');
        sinon.assert.notCalled(globalHttpAgent.addRequest);
        sinon.assert.notCalled(globalHttpsAgent.addRequest);
      }
      it('will proxy http requests (`host`)', function() {
        rawRequest(false);
      });
      it('will proxy http requests (`hostname`)', function() {
        rawRequest(true);
      });

      it('will proxy https requests', function() {
        var req = https.request(
          replaceHostByHostname(false, {
            method: 'GET',
            path: '/raw-https',
            host: 'example.dev'
          }),
          function() {}
        );
        req.end();

        connected('https:');
        sinon.assert.notCalled(globalHttpAgent.addRequest);
        sinon.assert.notCalled(globalHttpsAgent.addRequest);
      });

      it('request respects explicit agent param', function() {
        var agent = newFakeAgent();
        var req = http.request(
          replaceHostByHostname(false, {
            method: 'GET',
            path: '/raw-http-w-agent',
            host: 'example.dev',
            agent: agent
          }),
          function() {}
        );
        req.end();

        sinon.assert.notCalled(globalHttpAgent.addRequest);
        sinon.assert.notCalled(globalHttpsAgent.addRequest);
        sinon.assert.notCalled(net.createConnection);
        sinon.assert.notCalled(tls.connect);
        sinon.assert.calledOnce(agent.addRequest);
      });

      describe('request with `null` agent and defined `createConnection`', function() {
        before(function() {
          sinon.stub(http.ClientRequest.prototype, 'onSocket');
        });
        after(function() {
          http.ClientRequest.prototype.onSocket.restore();
        });

        function noAgent(useHostname) {
          var createConnection = sinon.stub();
          var req = http.request(
            replaceHostByHostname(useHostname, {
              method: 'GET',
              path: '/no-agent',
              host: 'example.dev',
              agent: null,
              createConnection: createConnection
            }),
            function() {} // eslint-disable-line max-nested-callbacks
          );
          req.end();

          sinon.assert.notCalled(globalHttpAgent.addRequest);
          sinon.assert.notCalled(globalHttpsAgent.addRequest);
          sinon.assert.calledOnce(createConnection);
        }
        it('uses no agent (`host`)', function() {
          noAgent(false);
        });
        it('uses no agent (`hostname`)', function() {
          noAgent(true);
        });
      });
    });
  }

  function enabledBlock(conf, testParams) {
    before(function() {
      globalTunnel.initialize(conf);
    });
    after(function() {
      globalTunnel.end();
    });

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

    proxyEnabledTests(testParams);
  }

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

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

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

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

  describe('with http proxy in mixed mode', function() {
    enabledBlock({
      protocol: 'http:',
      host: '10.2.3.4',
      port: 3337
    });
  });

  describe('with https proxy in mixed mode', function() {
    enabledBlock({
      protocol: 'https:',
      host: '10.2.3.4',
      port: 3338
    });
  });

  describe('using env var', function() {
    after(function() {
      delete process.env.http_proxy;
      assert.isUndefined(process.env.http_proxy);
    });

    describe('for http', function() {
      before(function() {
        process.env.http_proxy = 'http://10.2.3.4:1234'; // eslint-disable-line camelcase
      });
      enabledBlock(null, { isHttpsProxy: false, connect: 'https', port: 1234 });
    });

    describe('for https', function() {
      before(function() {
        process.env.http_proxy = 'https://10.2.3.4:1235'; // eslint-disable-line camelcase
      });
      enabledBlock(null, { isHttpsProxy: true, connect: 'https', port: 1235 });
    });
  });

  describe('using npm config', function() {
    var expectedProxy = { isHttpsProxy: false, connect: 'https', port: 1234 };
    var npmConfig = { get: function() {} };
    var npmConfigStub = sinon.stub(npmConfig, 'get');

    function configNpm(key, value) {
      return function() {
        global.__GLOBAL_TUNNEL_DEPENDENCY_NPMCONF__ = function() {
          return npmConfig;
        };

        npmConfigStub.withArgs(key).returns(value || 'http://10.2.3.4:1234');
      };
    }

    after(function() {
      global.__GLOBAL_TUNNEL_DEPENDENCY_NPMCONF__ = undefined;
    });

    describe('https-proxy', function() {
      before(configNpm('https-proxy'));
      enabledBlock(null, expectedProxy);
    });

    describe('http-proxy', function() {
      before(configNpm('http-proxy'));
      enabledBlock(null, expectedProxy);
    });

    describe('proxy', function() {
      before(configNpm('proxy'));
      enabledBlock(null, expectedProxy);
    });
    describe('order', function() {
      before(function() {
        configNpm('proxy')();
        configNpm('https-proxy', 'http://10.2.3.4:12345')();
        configNpm('http-proxy')();
      });

      enabledBlock(null, { isHttpsProxy: false, connect: 'https', port: 12345 });
    });

    describe('also using env var', function() {
      before(function() {
        configNpm('proxy')();
        process.env.http_proxy = 'http://10.2.3.4:1234'; // eslint-disable-line camelcase
      });

      after(function() {
        delete process.env.http_proxy;
      });

      enabledBlock(null, expectedProxy);
    });
  });

  // Deliberately after the block above
  describe('with proxy disabled', function() {
    it('claims to be disabled', function() {
      assert.isFalse(globalTunnel.isProxying);
    });

    it('will NOT proxy http requests', function(done) {
      var dummyCb = sinon.stub();
      request.get('http://example.dev/', dummyCb);
      setImmediate(function() {
        sinon.assert.calledOnce(globalHttpAgent.addRequest);
        sinon.assert.notCalled(globalHttpsAgent.addRequest);
        done();
      });
    });

    it('will NOT proxy https requests', function(done) {
      var dummyCb = sinon.stub();
      request.get('https://example.dev/', dummyCb);
      setImmediate(function() {
        sinon.assert.notCalled(globalHttpAgent.addRequest);
        sinon.assert.calledOnce(globalHttpsAgent.addRequest);
        done();
      });
    });
  });
});