|
3 | 3 | * Module dependencies. |
4 | 4 | */ |
5 | 5 |
|
| 6 | +var fs = require('fs'); |
6 | 7 | var url = require('url'); |
| 8 | +var http = require('http'); |
7 | 9 | var https = require('https'); |
8 | 10 | var assert = require('assert'); |
| 11 | +var Proxy = require('proxy'); |
9 | 12 | var HttpsProxyAgent = require('../'); |
10 | 13 |
|
11 | 14 | describe('HttpsProxyAgent', function () { |
12 | 15 |
|
13 | | - this.slow(5000); |
14 | | - this.timeout(10000); |
| 16 | + var server; |
| 17 | + var serverPort; |
15 | 18 |
|
16 | | - var link = process.env.LINK || 'https://graph.facebook.com/tootallnate'; |
| 19 | + var sslServer; |
| 20 | + var sslServerPort; |
17 | 21 |
|
18 | | - it('should throw an Error if no "proxy" is given', function () { |
19 | | - assert.throws(function () { |
20 | | - new HttpsProxyAgent(); |
| 22 | + var proxy; |
| 23 | + var proxyPort; |
| 24 | + |
| 25 | + var sslProxy; |
| 26 | + var sslProxyPort; |
| 27 | + |
| 28 | + before(function (done) { |
| 29 | + // setup target HTTP server |
| 30 | + server = http.createServer(); |
| 31 | + server.listen(function () { |
| 32 | + serverPort = server.address().port; |
| 33 | + done(); |
21 | 34 | }); |
22 | 35 | }); |
23 | 36 |
|
24 | | - it('should work over an HTTP proxy', function (done) { |
25 | | - var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://10.1.10.200:3128'; |
26 | | - var agent = new HttpsProxyAgent(proxy); |
| 37 | + before(function (done) { |
| 38 | + // setup HTTP proxy server |
| 39 | + proxy = Proxy(); |
| 40 | + proxy.listen(function () { |
| 41 | + proxyPort = proxy.address().port; |
| 42 | + done(); |
| 43 | + }); |
| 44 | + }); |
27 | 45 |
|
28 | | - var opts = url.parse(link); |
29 | | - opts.agent = agent; |
| 46 | + before(function (done) { |
| 47 | + // setup target HTTPS server |
| 48 | + var options = { |
| 49 | + key: fs.readFileSync(__dirname + '/server.key'), |
| 50 | + cert: fs.readFileSync(__dirname + '/server.crt') |
| 51 | + }; |
| 52 | + sslServer = https.createServer(options); |
| 53 | + sslServer.listen(function () { |
| 54 | + sslServerPort = sslServer.address().port; |
| 55 | + done(); |
| 56 | + }); |
| 57 | + }); |
30 | 58 |
|
31 | | - https.get(opts, function (res) { |
32 | | - var data = ''; |
33 | | - res.setEncoding('utf8'); |
34 | | - res.on('data', function (b) { |
35 | | - data += b; |
36 | | - }); |
37 | | - res.on('end', function () { |
38 | | - data = JSON.parse(data); |
39 | | - assert.equal('tootallnate', data.username); |
40 | | - done(); |
| 59 | + before(function (done) { |
| 60 | + // setup SSL HTTP proxy server |
| 61 | + var options = { |
| 62 | + key: fs.readFileSync(__dirname + '/server.key'), |
| 63 | + cert: fs.readFileSync(__dirname + '/server.crt') |
| 64 | + }; |
| 65 | + sslProxy = Proxy(https.createServer(options)); |
| 66 | + sslProxy.listen(function () { |
| 67 | + sslProxyPort = sslProxy.address().port; |
| 68 | + done(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + // shut down test HTTP server |
| 73 | + after(function (done) { |
| 74 | + server.once('close', function () { done(); }); |
| 75 | + server.close(); |
| 76 | + }); |
| 77 | + |
| 78 | + after(function (done) { |
| 79 | + proxy.once('close', function () { done(); }); |
| 80 | + proxy.close(); |
| 81 | + }); |
| 82 | + |
| 83 | + after(function (done) { |
| 84 | + sslServer.once('close', function () { done(); }); |
| 85 | + sslServer.close(); |
| 86 | + }); |
| 87 | + |
| 88 | + after(function (done) { |
| 89 | + sslProxy.once('close', function () { done(); }); |
| 90 | + sslProxy.close(); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('constructor', function () { |
| 94 | + it('should throw an Error if no "proxy" argument is given', function () { |
| 95 | + assert.throws(function () { |
| 96 | + new HttpsProxyAgent(); |
41 | 97 | }); |
42 | 98 | }); |
| 99 | + it('should accept a "string" proxy argument', function () { |
| 100 | + var agent = new HttpsProxyAgent('http://127.0.0.1:' + proxyPort); |
| 101 | + assert.equal('127.0.0.1', agent.proxy.host); |
| 102 | + assert.equal(proxyPort, agent.proxy.port); |
| 103 | + }); |
| 104 | + it('should accept a `url.parse()` result object argument', function () { |
| 105 | + var opts = url.parse('http://127.0.0.1:' + proxyPort); |
| 106 | + var agent = new HttpsProxyAgent(opts); |
| 107 | + assert.equal('127.0.0.1', agent.proxy.host); |
| 108 | + assert.equal(proxyPort, agent.proxy.port); |
| 109 | + }); |
43 | 110 | }); |
44 | 111 |
|
45 | | - it('should work over an HTTPS proxy', function (done) { |
46 | | - var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://10.1.10.200:3130'; |
47 | | - proxy = url.parse(proxy); |
48 | | - proxy.rejectUnauthorized = false; |
49 | | - var agent = new HttpsProxyAgent(proxy); |
50 | | - |
51 | | - var opts = url.parse(link); |
52 | | - opts.agent = agent; |
53 | | - opts.rejectUnauthorized = false; |
54 | | - |
55 | | - https.get(opts, function (res) { |
56 | | - var data = ''; |
57 | | - res.setEncoding('utf8'); |
58 | | - res.on('data', function (b) { |
59 | | - data += b; |
| 112 | + describe('"https" module', function () { |
| 113 | + it('should work over an HTTP proxy', function (done) { |
| 114 | + // set HTTP "request" event handler for this test |
| 115 | + sslServer.once('request', function (req, res) { |
| 116 | + res.end(JSON.stringify(req.headers)); |
| 117 | + }); |
| 118 | + |
| 119 | + var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort; |
| 120 | + proxy = url.parse(proxy); |
| 121 | + // `rejectUnauthorized` shoudn't *technically* be necessary here, |
| 122 | + // but up until node v0.11.6, the `http.Agent` class didn't have |
| 123 | + // access to the *entire* request "options" object. Thus, |
| 124 | + // `https-proxy-agent` will *also* merge in options you pass here |
| 125 | + // to the destination endpoints… |
| 126 | + proxy.rejectUnauthorized = false; |
| 127 | + var agent = new HttpsProxyAgent(proxy); |
| 128 | + |
| 129 | + var opts = url.parse('https://127.0.0.1:' + sslServerPort); |
| 130 | + opts.rejectUnauthorized = false; |
| 131 | + opts.agent = agent; |
| 132 | + |
| 133 | + https.get(opts, function (res) { |
| 134 | + var data = ''; |
| 135 | + res.setEncoding('utf8'); |
| 136 | + res.on('data', function (b) { |
| 137 | + data += b; |
| 138 | + }); |
| 139 | + res.on('end', function () { |
| 140 | + data = JSON.parse(data); |
| 141 | + assert.equal('127.0.0.1:' + sslServerPort, data.host); |
| 142 | + done(); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | + it('should work over an HTTPS proxy', function (done) { |
| 147 | + // set HTTP "request" event handler for this test |
| 148 | + sslServer.once('request', function (req, res) { |
| 149 | + res.end(JSON.stringify(req.headers)); |
60 | 150 | }); |
61 | | - res.on('end', function () { |
62 | | - data = JSON.parse(data); |
63 | | - assert.equal('tootallnate', data.username); |
64 | | - done(); |
| 151 | + |
| 152 | + var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://127.0.0.1:' + sslProxyPort; |
| 153 | + proxy = url.parse(proxy); |
| 154 | + // `rejectUnauthorized` is actually necessary this time since the HTTPS |
| 155 | + // proxy server itself is using a self-signed SSL certificate… |
| 156 | + proxy.rejectUnauthorized = false; |
| 157 | + var agent = new HttpsProxyAgent(proxy); |
| 158 | + |
| 159 | + var opts = url.parse('https://127.0.0.1:' + sslServerPort); |
| 160 | + opts.agent = agent; |
| 161 | + opts.rejectUnauthorized = false; |
| 162 | + |
| 163 | + https.get(opts, function (res) { |
| 164 | + var data = ''; |
| 165 | + res.setEncoding('utf8'); |
| 166 | + res.on('data', function (b) { |
| 167 | + data += b; |
| 168 | + }); |
| 169 | + res.on('end', function () { |
| 170 | + data = JSON.parse(data); |
| 171 | + assert.equal('127.0.0.1:' + sslServerPort, data.host); |
| 172 | + done(); |
| 173 | + }); |
65 | 174 | }); |
66 | 175 | }); |
67 | 176 | }); |
|
0 commit comments