1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var fs = require('fs');
|
7 | var url = require('url');
|
8 | var http = require('http');
|
9 | var https = require('https');
|
10 | var assert = require('assert');
|
11 | var toBuffer = require('stream-to-buffer');
|
12 | var Proxy = require('proxy');
|
13 | var socks = require('socksv5');
|
14 | var ProxyAgent = require('../');
|
15 |
|
16 | describe('ProxyAgent', function () {
|
17 |
|
18 | var httpServer, httpPort;
|
19 | var httpsServer, httpsPort;
|
20 |
|
21 |
|
22 | var socksServer, socksPort;
|
23 | var proxyServer, proxyPort;
|
24 | var proxyHttpsServer, proxyHttpsPort;
|
25 |
|
26 | before(function (done) {
|
27 |
|
28 | httpServer = http.createServer();
|
29 | httpServer.listen(function () {
|
30 | httpPort = httpServer.address().port;
|
31 | done();
|
32 | });
|
33 | });
|
34 |
|
35 | before(function (done) {
|
36 |
|
37 | var options = {
|
38 | key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
|
39 | cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
|
40 | };
|
41 | httpsServer = https.createServer(options);
|
42 | httpsServer.listen(function () {
|
43 | httpsPort = httpsServer.address().port;
|
44 | done();
|
45 | });
|
46 | });
|
47 |
|
48 | before(function (done) {
|
49 |
|
50 | socksServer = socks.createServer(function(info, accept, deny) {
|
51 | accept();
|
52 | });
|
53 | socksServer.listen(function() {
|
54 | socksPort = socksServer.address().port;
|
55 | done();
|
56 | });
|
57 | socksServer.useAuth(socks.auth.None());
|
58 | });
|
59 |
|
60 | before(function (done) {
|
61 |
|
62 | proxyServer = Proxy();
|
63 | proxyServer.listen(function () {
|
64 | proxyPort = proxyServer.address().port;
|
65 | done();
|
66 | });
|
67 | });
|
68 |
|
69 | before(function (done) {
|
70 |
|
71 | var options = {
|
72 | key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
|
73 | cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
|
74 | };
|
75 | proxyHttpsServer = Proxy(https.createServer(options));
|
76 | proxyHttpsServer.listen(function () {
|
77 | proxyHttpsPort = proxyHttpsServer.address().port;
|
78 | done();
|
79 | });
|
80 | });
|
81 |
|
82 |
|
83 | after(function (done) {
|
84 | socksServer.once('close', function () { done(); });
|
85 | socksServer.close();
|
86 | });
|
87 |
|
88 | after(function (done) {
|
89 | httpServer.once('close', function () { done(); });
|
90 | httpServer.close();
|
91 | });
|
92 |
|
93 | after(function (done) {
|
94 | httpsServer.once('close', function () { done(); });
|
95 | httpsServer.close();
|
96 | });
|
97 |
|
98 | after(function (done) {
|
99 | proxyServer.once('close', function () { done(); });
|
100 | proxyServer.close();
|
101 | });
|
102 |
|
103 | after(function (done) {
|
104 | proxyHttpsServer.once('close', function () { done(); });
|
105 | proxyHttpsServer.close();
|
106 | });
|
107 |
|
108 | it('should export a "function"', function () {
|
109 | assert.equal('function', typeof ProxyAgent);
|
110 | });
|
111 |
|
112 | describe('constructor', function () {
|
113 | it('should throw a TypeError if no "protocol" is given', function () {
|
114 | assert.throws(function () {
|
115 | ProxyAgent({ host: 'foo.com', port: 3128 });
|
116 | }, function (e) {
|
117 | return 'TypeError' === e.name &&
|
118 | /must specify a "protocol"/.test(e.message) &&
|
119 | /\bhttp\b/.test(e.message) &&
|
120 | /\bhttps\b/.test(e.message) &&
|
121 | /\bsocks\b/.test(e.message);
|
122 | });
|
123 | });
|
124 |
|
125 | it('should throw a TypeError for unsupported proxy protocols', function () {
|
126 | assert.throws(function () {
|
127 | ProxyAgent('bad://foo.com:8888');
|
128 | }, function (e) {
|
129 | return 'TypeError' === e.name &&
|
130 | /unsupported proxy protocol/.test(e.message);
|
131 | });
|
132 | });
|
133 | });
|
134 |
|
135 | describe('"http" module', function () {
|
136 | describe('over "http" proxy', function () {
|
137 | it('should work', function (done) {
|
138 | httpServer.once('request', function (req, res) {
|
139 | res.end(JSON.stringify(req.headers));
|
140 | });
|
141 |
|
142 | var uri = 'http://127.0.0.1:' + proxyPort;
|
143 | var agent = new ProxyAgent(uri);
|
144 |
|
145 | var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
|
146 | opts.agent = agent;
|
147 |
|
148 | var req = http.get(opts, function (res) {
|
149 | toBuffer(res, function (err, buf) {
|
150 | if (err) return done(err);
|
151 | var data = JSON.parse(buf.toString('utf8'));
|
152 | assert.equal('127.0.0.1:' + httpPort, data.host);
|
153 | assert('via' in data);
|
154 | done();
|
155 | });
|
156 | });
|
157 | req.once('error', done);
|
158 | });
|
159 | });
|
160 |
|
161 | describe('over "http" proxy from env', function () {
|
162 | it('should work', function (done) {
|
163 | httpServer.once('request', function (req, res) {
|
164 | res.end(JSON.stringify(req.headers));
|
165 | });
|
166 |
|
167 | process.env.HTTP_PROXY = 'http://127.0.0.1:' + proxyPort;
|
168 | var agent = new ProxyAgent();
|
169 |
|
170 | var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
|
171 | opts.agent = agent;
|
172 |
|
173 | var req = http.get(opts, function (res) {
|
174 | toBuffer(res, function (err, buf) {
|
175 | if (err) return done(err);
|
176 | var data = JSON.parse(buf.toString('utf8'));
|
177 | assert.equal('127.0.0.1:' + httpPort, data.host);
|
178 | assert('via' in data);
|
179 | done();
|
180 | });
|
181 | });
|
182 | req.once('error', done);
|
183 | });
|
184 | });
|
185 |
|
186 | describe('with no proxy from env', function () {
|
187 | it('should work', function (done) {
|
188 | httpServer.once('request', function (req, res) {
|
189 | res.end(JSON.stringify(req.headers));
|
190 | });
|
191 |
|
192 | process.env.NO_PROXY = '*';
|
193 | var agent = new ProxyAgent();
|
194 |
|
195 | var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
|
196 | opts.agent = agent;
|
197 |
|
198 | var req = http.get(opts, function (res) {
|
199 | toBuffer(res, function (err, buf) {
|
200 | if (err) return done(err);
|
201 | var data = JSON.parse(buf.toString('utf8'));
|
202 | assert.equal('127.0.0.1:' + httpPort, data.host);
|
203 | assert(!('via' in data));
|
204 | done();
|
205 | });
|
206 | });
|
207 | req.once('error', done);
|
208 | });
|
209 | });
|
210 |
|
211 | describe('over "https" proxy', function () {
|
212 | it('should work', function (done) {
|
213 | httpServer.once('request', function (req, res) {
|
214 | res.end(JSON.stringify(req.headers));
|
215 | });
|
216 |
|
217 | var uri = 'https://127.0.0.1:' + proxyHttpsPort;
|
218 | var proxy = url.parse(uri);
|
219 | proxy.rejectUnauthorized = false;
|
220 | var agent = new ProxyAgent(proxy);
|
221 |
|
222 | var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
|
223 | opts.agent = agent;
|
224 |
|
225 | var req = http.get(opts, function (res) {
|
226 | toBuffer(res, function (err, buf) {
|
227 | if (err) return done(err);
|
228 | var data = JSON.parse(buf.toString('utf8'));
|
229 | assert.equal('127.0.0.1:' + httpPort, data.host);
|
230 | assert('via' in data);
|
231 | done();
|
232 | });
|
233 | });
|
234 | req.once('error', done);
|
235 | });
|
236 | });
|
237 |
|
238 | describe('over "socks" proxy', function () {
|
239 | it('should work', function (done) {
|
240 | httpServer.once('request', function (req, res) {
|
241 | res.end(JSON.stringify(req.headers));
|
242 | });
|
243 |
|
244 | var uri = 'socks://127.0.0.1:' + socksPort;
|
245 | var agent = new ProxyAgent(uri);
|
246 |
|
247 | var opts = url.parse('http://127.0.0.1:' + httpPort + '/test');
|
248 | opts.agent = agent;
|
249 |
|
250 | var req = http.get(opts, function (res) {
|
251 | toBuffer(res, function (err, buf) {
|
252 | if (err) return done(err);
|
253 | var data = JSON.parse(buf.toString('utf8'));
|
254 | assert.equal('127.0.0.1:' + httpPort, data.host);
|
255 | done();
|
256 | });
|
257 | });
|
258 | req.once('error', done);
|
259 | });
|
260 | });
|
261 | });
|
262 |
|
263 | describe('"https" module', function () {
|
264 | describe('over "http" proxy', function () {
|
265 | it('should work', function (done) {
|
266 | httpsServer.once('request', function (req, res) {
|
267 | res.end(JSON.stringify(req.headers));
|
268 | });
|
269 |
|
270 | var uri = 'http://127.0.0.1:' + proxyPort;
|
271 | var agent = new ProxyAgent(uri);
|
272 |
|
273 | var opts = url.parse('https://127.0.0.1:' + httpsPort + '/test');
|
274 | opts.agent = agent;
|
275 | opts.rejectUnauthorized = false;
|
276 |
|
277 | var req = https.get(opts, function (res) {
|
278 | toBuffer(res, function (err, buf) {
|
279 | if (err) return done(err);
|
280 | var data = JSON.parse(buf.toString('utf8'));
|
281 | assert.equal('127.0.0.1:' + httpsPort, data.host);
|
282 | done();
|
283 | });
|
284 | });
|
285 | req.once('error', done);
|
286 | });
|
287 | });
|
288 |
|
289 | describe('over "https" proxy', function () {
|
290 | it('should work', function (done) {
|
291 | var gotReq = false;
|
292 | httpsServer.once('request', function (req, res) {
|
293 | gotReq = true;
|
294 | res.end(JSON.stringify(req.headers));
|
295 | });
|
296 |
|
297 | var agent = new ProxyAgent({
|
298 | protocol: 'https:',
|
299 | host: '127.0.0.1',
|
300 | port: proxyHttpsPort,
|
301 | rejectUnauthorized: false
|
302 | });
|
303 |
|
304 | var opts = url.parse('https://127.0.0.1:' + httpsPort + '/test');
|
305 | opts.agent = agent;
|
306 | opts.rejectUnauthorized = false;
|
307 |
|
308 | var req = https.get(opts, function (res) {
|
309 | toBuffer(res, function (err, buf) {
|
310 | if (err) return done(err);
|
311 | var data = JSON.parse(buf.toString('utf8'));
|
312 | assert.equal('127.0.0.1:' + httpsPort, data.host);
|
313 | assert(gotReq);
|
314 | done();
|
315 | });
|
316 | });
|
317 | req.once('error', done);
|
318 | });
|
319 | });
|
320 |
|
321 | describe('over "socks" proxy', function () {
|
322 | it('should work', function (done) {
|
323 | var gotReq = false;
|
324 | httpsServer.once('request', function (req, res) {
|
325 | gotReq = true;
|
326 | res.end(JSON.stringify(req.headers));
|
327 | });
|
328 |
|
329 | var uri = 'socks://127.0.0.1:' + socksPort;
|
330 | var agent = new ProxyAgent(uri);
|
331 |
|
332 | var opts = url.parse('https://127.0.0.1:' + httpsPort + '/test');
|
333 | opts.agent = agent;
|
334 | opts.rejectUnauthorized = false;
|
335 |
|
336 | var req = https.get(opts, function (res) {
|
337 | toBuffer(res, function (err, buf) {
|
338 | if (err) return done(err);
|
339 | var data = JSON.parse(buf.toString('utf8'));
|
340 | assert.equal('127.0.0.1:' + httpsPort, data.host);
|
341 | assert(gotReq);
|
342 | done();
|
343 | });
|
344 | });
|
345 | req.once('error', done);
|
346 | });
|
347 | });
|
348 | });
|
349 | });
|