diff options
author | Rebecca Turner <me@re-becca.org> | 2018-05-24 23:31:26 +0300 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2018-05-24 23:31:38 +0300 |
commit | 0483f5c5deaf18c968a128657923103e49f4e67a (patch) | |
tree | 210f154da2f60938471d96a844dcb32cbda09e2a /node_modules/http-proxy-agent | |
parent | 6c294614d800cb95a49ef1c422fbdde65b0731a2 (diff) |
Flatten dependencies and add dev deps to git
Diffstat (limited to 'node_modules/http-proxy-agent')
-rw-r--r-- | node_modules/http-proxy-agent/.travis.yml | 22 | ||||
-rw-r--r-- | node_modules/http-proxy-agent/History.md | 101 | ||||
-rw-r--r-- | node_modules/http-proxy-agent/README.md | 74 | ||||
-rw-r--r-- | node_modules/http-proxy-agent/index.js | 111 | ||||
-rw-r--r-- | node_modules/http-proxy-agent/package.json | 67 | ||||
-rw-r--r-- | node_modules/http-proxy-agent/test/ssl-cert-snakeoil.key | 15 | ||||
-rw-r--r-- | node_modules/http-proxy-agent/test/ssl-cert-snakeoil.pem | 12 | ||||
-rw-r--r-- | node_modules/http-proxy-agent/test/test.js | 303 |
8 files changed, 705 insertions, 0 deletions
diff --git a/node_modules/http-proxy-agent/.travis.yml b/node_modules/http-proxy-agent/.travis.yml new file mode 100644 index 000000000..805d3d50d --- /dev/null +++ b/node_modules/http-proxy-agent/.travis.yml @@ -0,0 +1,22 @@ +sudo: false + +language: node_js + +node_js: + - "4" + - "5" + - "6" + - "7" + - "8" + +install: + - PATH="`npm bin`:`npm bin -g`:$PATH" + # Install dependencies and build + - npm install + +script: + # Output useful info for debugging + - node --version + - npm --version + # Run tests + - npm test diff --git a/node_modules/http-proxy-agent/History.md b/node_modules/http-proxy-agent/History.md new file mode 100644 index 000000000..7e3e1e783 --- /dev/null +++ b/node_modules/http-proxy-agent/History.md @@ -0,0 +1,101 @@ + +2.1.0 / 2018-03-03 +================== + + * Add "engines" to package.json + * Use `Buffer.from()` + * Update package.json - outdated debug version (#7) + +2.0.0 / 2017-06-27 +================== + + * drop support for Node.js < v4 + * update "mocha" to v3 + * update to "agent-base" v4 + * rename http-proxy-agent.js to index.js + * remove `extend` dependency + * test Node.js 4, 5, 6, 7 and 8 on Travis-CI + +1.0.0 / 2015-07-10 +================== + + * http-proxy-agent: use %o debug() formatter + * http-proxy-agent: remove `defaults` merging logic + * package: update "agent-base" to v2 + * test: add an assert() call + * test: use ssl-cert-snakeoil self-signed SSL certs + * README: add note about node-https-proxy-agent + +0.2.7 / 2015-07-06 +================== + + * travis: ensure latest npm before testing + * travis: test node v0.8, v0.10, and v0.12 + * README: use SVG for Travis-CI badge + * package: update "extend" to v3 + * package: update "mocha" to v2 + * package: update "debug" to v2 + +0.2.6 / 2014-06-11 +================== + + * package: update "debug" to v1.0.0 + +0.2.5 / 2014-04-09 +================== + + * package: update outdated deps + +0.2.4 / 2014-01-12 +================== + + * http-proxy-agent: fix using the agent after the first tick of creating the ClientRequest + * http-proxy-agent: use "debug" module + * History: fix whitespace + +0.2.3 / 2013-11-18 +================== + + * https-proxy-agent: allow "https" without trailing colon + +0.2.2 / 2013-11-16 +================== + + * http-proxy-agent: delete the `port` if it matches default port + * http-proxy-agent: don't mix in the `proxy` opts to the endpoint opts + * http-proxy-agent: delete `pathname` from the proxy opts as well + +0.2.1 / 2013-10-28 +================== + + * http-proxy-agent: properly proxy the query-string on request URLs (GH-1) + +0.2.0 / 2013-09-16 +================== + + * http-proxy-agent: update to `agent-base` v1.0.0 API + * http-proxy-agent: rename `secure` option to `secureProxy` + * http-proxy-agent: default the "port" to 80 if not set + * http-proxy-agent: use "extend" module + * test: refactor tests + * test: add 407 auth test + * test: add bad proxy info test + * test: add "secureProxy" option tests + +0.1.0 / 2013-09-03 +================== + + * Add initial "Proxy-Authorization" Basic authentication support + +0.0.2 / 2013-07-11 +================== + + * test: make tests pass, ensure valid IP addresses are returned + * test: add tests + * throw an Error when no proxy info is given + * add support for passing options to net/tls .connect() + +0.0.1 / 2013-07-09 +================== + + * Initial release diff --git a/node_modules/http-proxy-agent/README.md b/node_modules/http-proxy-agent/README.md new file mode 100644 index 000000000..4f1fc372b --- /dev/null +++ b/node_modules/http-proxy-agent/README.md @@ -0,0 +1,74 @@ +http-proxy-agent +================ +### An HTTP(s) proxy `http.Agent` implementation for HTTP +[![Build Status](https://travis-ci.org/TooTallNate/node-http-proxy-agent.svg?branch=master)](https://travis-ci.org/TooTallNate/node-http-proxy-agent) + +This module provides an `http.Agent` implementation that connects to a specified +HTTP or HTTPS proxy server, and can be used with the built-in `http` module. + +__Note:__ For HTTP proxy usage with the `https` module, check out +[`node-https-proxy-agent`](https://github.com/TooTallNate/node-https-proxy-agent). + +Installation +------------ + +Install with `npm`: + +``` bash +$ npm install http-proxy-agent +``` + + +Example +------- + +``` js +var url = require('url'); +var http = require('http'); +var HttpProxyAgent = require('http-proxy-agent'); + +// HTTP/HTTPS proxy to connect to +var proxy = process.env.http_proxy || 'http://168.63.76.32:3128'; +console.log('using proxy server %j', proxy); + +// HTTP endpoint for the proxy to connect to +var endpoint = process.argv[2] || 'http://nodejs.org/api/'; +console.log('attempting to GET %j', endpoint); +var opts = url.parse(endpoint); + +// create an instance of the `HttpProxyAgent` class with the proxy server information +var agent = new HttpProxyAgent(proxy); +opts.agent = agent; + +http.get(opts, function (res) { + console.log('"response" event!', res.headers); + res.pipe(process.stdout); +}); +``` + + +License +------- + +(The MIT License) + +Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net> + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/http-proxy-agent/index.js b/node_modules/http-proxy-agent/index.js new file mode 100644 index 000000000..f90a5297c --- /dev/null +++ b/node_modules/http-proxy-agent/index.js @@ -0,0 +1,111 @@ + +/** + * Module dependencies. + */ + +var net = require('net'); +var tls = require('tls'); +var url = require('url'); +var Agent = require('agent-base'); +var inherits = require('util').inherits; +var debug = require('debug')('http-proxy-agent'); + +/** + * Module exports. + */ + +module.exports = HttpProxyAgent; + +/** + * The `HttpProxyAgent` implements an HTTP Agent subclass that connects to the + * specified "HTTP proxy server" in order to proxy HTTP requests. + * + * @api public + */ + +function HttpProxyAgent (opts) { + if (!(this instanceof HttpProxyAgent)) return new HttpProxyAgent(opts); + if ('string' == typeof opts) opts = url.parse(opts); + if (!opts) throw new Error('an HTTP(S) proxy server `host` and `port` must be specified!'); + debug('creating new HttpProxyAgent instance: %o', opts); + Agent.call(this, opts); + + var proxy = Object.assign({}, opts); + + // if `true`, then connect to the proxy server over TLS. defaults to `false`. + this.secureProxy = proxy.protocol ? /^https:?$/i.test(proxy.protocol) : false; + + // prefer `hostname` over `host`, and set the `port` if needed + proxy.host = proxy.hostname || proxy.host; + proxy.port = +proxy.port || (this.secureProxy ? 443 : 80); + + if (proxy.host && proxy.path) { + // if both a `host` and `path` are specified then it's most likely the + // result of a `url.parse()` call... we need to remove the `path` portion so + // that `net.connect()` doesn't attempt to open that as a unix socket file. + delete proxy.path; + delete proxy.pathname; + } + + this.proxy = proxy; +} +inherits(HttpProxyAgent, Agent); + +/** + * Called when the node-core HTTP client library is creating a new HTTP request. + * + * @api public + */ + +HttpProxyAgent.prototype.callback = function connect (req, opts, fn) { + var proxy = this.proxy; + + // change the `http.ClientRequest` instance's "path" field + // to the absolute path of the URL that will be requested + var parsed = url.parse(req.path); + if (null == parsed.protocol) parsed.protocol = 'http:'; + if (null == parsed.hostname) parsed.hostname = opts.hostname || opts.host; + if (null == parsed.port) parsed.port = opts.port; + if (parsed.port == 80) { + // if port is 80, then we can remove the port so that the + // ":80" portion is not on the produced URL + delete parsed.port; + } + var absolute = url.format(parsed); + req.path = absolute; + + // inject the `Proxy-Authorization` header if necessary + if (proxy.auth) { + req.setHeader( + 'Proxy-Authorization', + 'Basic ' + Buffer.from(proxy.auth).toString('base64') + ); + } + + // create a socket connection to the proxy server + var socket; + if (this.secureProxy) { + socket = tls.connect(proxy); + } else { + socket = net.connect(proxy); + } + + // at this point, the http ClientRequest's internal `_header` field might have + // already been set. If this is the case then we'll need to re-generate the + // string since we just changed the `req.path` + if (req._header) { + debug('regenerating stored HTTP header string for request'); + req._header = null; + req._implicitHeader(); + if (req.output && req.output.length > 0) { + debug('patching connection write() output buffer with updated header'); + // the _header has already been queued to be written to the socket + var first = req.output[0]; + var endOfHeaders = first.indexOf('\r\n\r\n') + 4; + req.output[0] = req._header + first.substring(endOfHeaders); + debug('output buffer: %o', req.output); + } + } + + fn(null, socket); +}; diff --git a/node_modules/http-proxy-agent/package.json b/node_modules/http-proxy-agent/package.json new file mode 100644 index 000000000..99e28fdc6 --- /dev/null +++ b/node_modules/http-proxy-agent/package.json @@ -0,0 +1,67 @@ +{ + "_from": "http-proxy-agent@^2.1.0", + "_id": "http-proxy-agent@2.1.0", + "_inBundle": false, + "_integrity": "sha512-qwHbBLV7WviBl0rQsOzH6o5lwyOIvwp/BdFnvVxXORldu5TmjFfjzBcWUWS5kWAZhmv+JtiDhSuQCp4sBfbIgg==", + "_location": "/http-proxy-agent", + "_phantomChildren": {}, + "_requested": { + "type": "range", + "registry": true, + "raw": "http-proxy-agent@^2.1.0", + "name": "http-proxy-agent", + "escapedName": "http-proxy-agent", + "rawSpec": "^2.1.0", + "saveSpec": null, + "fetchSpec": "^2.1.0" + }, + "_requiredBy": [ + "/make-fetch-happen", + "/npm-profile/make-fetch-happen", + "/npm-registry-fetch/make-fetch-happen" + ], + "_resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-2.1.0.tgz", + "_shasum": "e4821beef5b2142a2026bd73926fe537631c5405", + "_spec": "http-proxy-agent@^2.1.0", + "_where": "/Users/rebecca/code/npm/node_modules/make-fetch-happen", + "author": { + "name": "Nathan Rajlich", + "email": "nathan@tootallnate.net", + "url": "http://n8.io/" + }, + "bugs": { + "url": "https://github.com/TooTallNate/node-http-proxy-agent/issues" + }, + "bundleDependencies": false, + "dependencies": { + "agent-base": "4", + "debug": "3.1.0" + }, + "deprecated": false, + "description": "An HTTP(s) proxy `http.Agent` implementation for HTTP", + "devDependencies": { + "mocha": "3", + "proxy": "~0.2.3" + }, + "engines": { + "node": ">= 4.5.0" + }, + "homepage": "https://github.com/TooTallNate/node-http-proxy-agent#readme", + "keywords": [ + "http", + "proxy", + "endpoint", + "agent" + ], + "license": "MIT", + "main": "./index.js", + "name": "http-proxy-agent", + "repository": { + "type": "git", + "url": "git://github.com/TooTallNate/node-http-proxy-agent.git" + }, + "scripts": { + "test": "mocha --reporter spec" + }, + "version": "2.1.0" +} diff --git a/node_modules/http-proxy-agent/test/ssl-cert-snakeoil.key b/node_modules/http-proxy-agent/test/ssl-cert-snakeoil.key new file mode 100644 index 000000000..fd1250122 --- /dev/null +++ b/node_modules/http-proxy-agent/test/ssl-cert-snakeoil.key @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICWwIBAAKBgQCzURxIqzer0ACAbX/lHdsn4Gd9PLKrf7EeDYfIdV0HZKPD8WDr +bBx2/fBu0OW2sjnzv/SVZbJ0DAuPE/p0+eT0qb2qC10iz9iTD7ribd7gxhirVb8y +b3fBjXsxc8V8p4Ny1LcvNSqCjwUbJqdRogfoJeTiqPM58z5sNzuv5iq7iwIDAQAB +AoGAPMQy4olrP0UotlzlJ36bowLP70ffgHCwU+/f4NWs5fF78c3du0oSx1w820Dd +Z7E0JF8bgnlJJTxjumPZz0RUCugrEHBKJmzEz3cxF5E3+7NvteZcjKn9D67RrM5x +1/uSZ9cqKE9cYvY4fSuHx18diyZ4axR/wB1Pea2utjjDM+ECQQDb9ZbmmaWMiRpQ +5Up+loxP7BZNPsEVsm+DVJmEFbaFgGfncWBqSIqnPNjMwTwj0OigTwCAEGPkfRVW +T0pbYWCxAkEA0LK7SCTwzyDmhASUalk0x+3uCAA6ryFdwJf/wd8TRAvVOmkTEldX +uJ7ldLvfrONYO3v56uKTU/SoNdZYzKtO+wJAX2KM4ctXYy5BXztPpr2acz4qHa1N +Bh+vBAC34fOYhyQ76r3b1btHhWZ5jbFuZwm9F2erC94Ps5IaoqcX07DSwQJAPKGw +h2U0EPkd/3zVIZCJJQya+vgWFIs9EZcXVtvYXQyTBkVApTN66MhBIYjzkub5205J +bVQmOV37AKklY1DhwQJAA1wos0cYxro02edzatxd0DIR2r4qqOqLkw6BhYHhq6HJ +ZvIcQkHqdSXzdETFc01I1znDGGIrJHcnvKWgBPoEUg== +-----END RSA PRIVATE KEY----- diff --git a/node_modules/http-proxy-agent/test/ssl-cert-snakeoil.pem b/node_modules/http-proxy-agent/test/ssl-cert-snakeoil.pem new file mode 100644 index 000000000..b115a5e91 --- /dev/null +++ b/node_modules/http-proxy-agent/test/ssl-cert-snakeoil.pem @@ -0,0 +1,12 @@ +-----BEGIN CERTIFICATE----- +MIIB1TCCAT4CCQDV5mPlzm9+izANBgkqhkiG9w0BAQUFADAvMS0wKwYDVQQDEyQ3 +NTI3YmQ3Ny1hYjNlLTQ3NGItYWNlNy1lZWQ2MDUzOTMxZTcwHhcNMTUwNzA2MjI0 +NTA3WhcNMjUwNzAzMjI0NTA3WjAvMS0wKwYDVQQDEyQ3NTI3YmQ3Ny1hYjNlLTQ3 +NGItYWNlNy1lZWQ2MDUzOTMxZTcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGB +ALNRHEirN6vQAIBtf+Ud2yfgZ308sqt/sR4Nh8h1XQdko8PxYOtsHHb98G7Q5bay +OfO/9JVlsnQMC48T+nT55PSpvaoLXSLP2JMPuuJt3uDGGKtVvzJvd8GNezFzxXyn +g3LUty81KoKPBRsmp1GiB+gl5OKo8znzPmw3O6/mKruLAgMBAAEwDQYJKoZIhvcN +AQEFBQADgYEACzoHUF8UV2Z6541Q2wKEA0UFUzmUjf/E1XwBO+1P15ZZ64uw34B4 +1RwMPtAo9RY/PmICTWtNxWGxkzwb2JtDWtnxVER/lF8k2XcXPE76fxTHJF/BKk9J +QU8OTD1dd9gHCBviQB9TqntRZ5X7axjtuWjb2umY+owBYzAHZkp1HKI= +-----END CERTIFICATE----- diff --git a/node_modules/http-proxy-agent/test/test.js b/node_modules/http-proxy-agent/test/test.js new file mode 100644 index 000000000..cc320c7fc --- /dev/null +++ b/node_modules/http-proxy-agent/test/test.js @@ -0,0 +1,303 @@ + +/** + * Module dependencies. + */ + +var fs = require('fs'); +var url = require('url'); +var http = require('http'); +var https = require('https'); +var assert = require('assert'); +var Proxy = require('proxy'); +var HttpProxyAgent = require('../'); + +describe('HttpProxyAgent', function () { + + var server; + var serverPort; + + var proxy; + var proxyPort; + + var sslProxy; + var sslProxyPort; + + before(function (done) { + // setup HTTP proxy server + proxy = Proxy(); + proxy.listen(function () { + proxyPort = proxy.address().port; + done(); + }); + }); + + before(function (done) { + // setup target HTTP server + server = http.createServer(); + server.listen(function () { + serverPort = server.address().port; + done(); + }); + }); + + before(function (done) { + // setup SSL HTTP proxy server + var options = { + key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'), + cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem') + }; + sslProxy = Proxy(https.createServer(options)); + sslProxy.listen(function () { + sslProxyPort = sslProxy.address().port; + done(); + }); + }); + + // shut down test HTTP server + after(function (done) { + proxy.once('close', function () { done(); }); + proxy.close(); + }); + + after(function (done) { + server.once('close', function () { done(); }); + server.close(); + }); + + after(function (done) { + sslProxy.once('close', function () { done(); }); + sslProxy.close(); + }); + + describe('constructor', function () { + it('should throw an Error if no "proxy" argument is given', function () { + assert.throws(function () { + new HttpProxyAgent(); + }); + }); + it('should accept a "string" proxy argument', function () { + var agent = new HttpProxyAgent('http://127.0.0.1:' + proxyPort); + assert.equal('127.0.0.1', agent.proxy.host); + assert.equal(proxyPort, agent.proxy.port); + }); + it('should accept a `url.parse()` result object argument', function () { + var opts = url.parse('http://127.0.0.1:' + proxyPort); + var agent = new HttpProxyAgent(opts); + assert.equal('127.0.0.1', agent.proxy.host); + assert.equal(proxyPort, agent.proxy.port); + }); + describe('secureProxy', function () { + it('should default to `false`', function () { + var agent = new HttpProxyAgent({ port: proxyPort }); + assert.equal(false, agent.secureProxy); + }); + it('should be `false` when "http:" protocol is used', function () { + var agent = new HttpProxyAgent({ port: proxyPort, protocol: 'http:' }); + assert.equal(false, agent.secureProxy); + }); + it('should be `true` when "https:" protocol is used', function () { + var agent = new HttpProxyAgent({ port: proxyPort, protocol: 'https:' }); + assert.equal(true, agent.secureProxy); + }); + it('should be `true` when "https" protocol is used', function () { + var agent = new HttpProxyAgent({ port: proxyPort, protocol: 'https' }); + assert.equal(true, agent.secureProxy); + }); + }); + }); + + describe('"http" module', function () { + it('should work over an HTTP proxy', function (done) { + // set HTTP "request" event handler for this test + server.once('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + }); + + var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort; + var agent = new HttpProxyAgent(proxy); + + var opts = url.parse('http://127.0.0.1:' + serverPort); + opts.agent = agent; + + http.get(opts, function (res) { + var data = ''; + res.setEncoding('utf8'); + res.on('data', function (b) { + data += b; + }); + res.on('end', function () { + data = JSON.parse(data); + assert.equal('127.0.0.1:' + serverPort, data.host); + assert('via' in data); + done(); + }); + }); + }); + it('should work over an HTTPS proxy', function (done) { + // set HTTP "request" event handler for this test + server.once('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + }); + + var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://127.0.0.1:' + sslProxyPort; + proxy = url.parse(proxy); + proxy.rejectUnauthorized = false; + var agent = new HttpProxyAgent(proxy); + assert.equal(true, agent.secureProxy); + + var opts = url.parse('http://127.0.0.1:' + serverPort); + opts.agent = agent; + + http.get(opts, function (res) { + var data = ''; + res.setEncoding('utf8'); + res.on('data', function (b) { + data += b; + }); + res.on('end', function () { + data = JSON.parse(data); + assert.equal('127.0.0.1:' + serverPort, data.host); + assert('via' in data); + done(); + }); + }); + }); + it('should proxy the query string of the request path', function (done) { + // set HTTP "request" event handler for this test + server.once('request', function (req, res) { + res.end(JSON.stringify({ + url: req.url + })); + }); + + var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort; + var agent = new HttpProxyAgent(proxy); + + var opts = url.parse('http://127.0.0.1:' + serverPort + '/test?foo=bar&1=2'); + opts.agent = agent; + + http.get(opts, function (res) { + var data = ''; + res.setEncoding('utf8'); + res.on('data', function (b) { + data += b; + }); + res.on('end', function () { + data = JSON.parse(data); + assert.equal('/test?foo=bar&1=2', data.url); + done(); + }); + }); + }); + it('should receive the 407 authorization code on the `http.ClientResponse`', function (done) { + // set a proxy authentication function for this test + proxy.authenticate = function (req, fn) { + // reject all requests + fn(null, false); + }; + + var proxyUri = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort; + var agent = new HttpProxyAgent(proxyUri); + + var opts = {}; + // `host` and `port` don't really matter since the proxy will reject anyways + opts.host = '127.0.0.1'; + opts.port = 80; + opts.agent = agent; + + http.get(opts, function (res) { + assert.equal(407, res.statusCode); + assert('proxy-authenticate' in res.headers); + delete proxy.authenticate; + done(); + }); + }); + it('should send the "Proxy-Authorization" request header', function (done) { + // set a proxy authentication function for this test + proxy.authenticate = function (req, fn) { + // username:password is "foo:bar" + fn(null, req.headers['proxy-authorization'] == 'Basic Zm9vOmJhcg=='); + }; + + // set HTTP "request" event handler for this test + server.once('request', function (req, res) { + res.end(JSON.stringify(req.headers)); + }); + + var proxyUri = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort; + var proxyOpts = url.parse(proxyUri); + proxyOpts.auth = 'foo:bar'; + var agent = new HttpProxyAgent(proxyOpts); + + var opts = url.parse('http://127.0.0.1:' + serverPort); + opts.agent = agent; + + http.get(opts, function (res) { + var data = ''; + res.setEncoding('utf8'); + res.on('data', function (b) { + data += b; + }); + res.on('end', function () { + data = JSON.parse(data); + assert.equal('127.0.0.1:' + serverPort, data.host); + assert('via' in data); + delete proxy.authenticate; + done(); + }); + }); + }); + it('should emit an "error" event on the `http.ClientRequest` if the proxy does not exist', function (done) { + // port 4 is a reserved, but "unassigned" port + var proxyUri = 'http://127.0.0.1:4'; + var agent = new HttpProxyAgent(proxyUri); + + var opts = url.parse('http://nodejs.org'); + opts.agent = agent; + + var req = http.get(opts); + req.once('error', function (err) { + assert.equal('ECONNREFUSED', err.code); + req.abort(); + done(); + }); + }); + it('should work after the first tick of the `http.ClientRequest` instance', function (done) { + // set HTTP "request" event handler for this test + server.once('request', function (req, res) { + res.end(JSON.stringify(req.url)); + }); + + var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort; + var agent = new HttpProxyAgent(proxy); + + var opts = url.parse('http://127.0.0.1:' + serverPort + '/test'); + opts.agent = agent; + + // defer the "connect()" function logic, since calling .end() before the + // "socket" event can cause weirdness since the HTTP header will have been + // cached and the HttpProxyAgent `req.path` patches won't be respected + var callback = agent.callback; + agent.callback = function (req, opts, fn) { + setTimeout(function () { + agent.callback = callback; + agent.callback(req, opts, fn); + }, 10); + }; + + http.get(opts, function (res) { + var data = ''; + res.setEncoding('utf8'); + res.on('data', function (b) { + data += b; + }); + res.on('end', function () { + data = JSON.parse(data); + assert.equal('/test', data); + done(); + }); + }); + }); + }); + +}); |