5 var fs = require('fs');
6 var url = require('url');
7 var net = require('net');
8 var tls = require('tls');
9 var http = require('http');
10 var https = require('https');
11 var WebSocket = require('ws');
12 var assert = require('assert');
13 var events = require('events');
14 var inherits = require('util').inherits;
15 var Agent = require('../');
17 var PassthroughAgent = Agent(function(req, opts) {
18 return opts.secureEndpoint ? https.globalAgent : http.globalAgent;
21 describe('Agent', function() {
22 describe('subclass', function() {
23 it('should be subclassable', function(done) {
27 inherits(MyAgent, Agent);
29 MyAgent.prototype.callback = function(req, opts, fn) {
30 assert.equal(req.path, '/foo');
31 assert.equal(req.getHeader('host'), '127.0.0.1:1234');
32 assert.equal(opts.secureEndpoint, true);
36 var info = url.parse('https://127.0.0.1:1234/foo');
37 info.agent = new MyAgent();
41 describe('options', function() {
42 it('should support an options Object as first argument', function() {
43 var agent = new Agent({ timeout: 1000 });
44 assert.equal(1000, agent.timeout);
46 it('should support an options Object as second argument', function() {
47 var agent = new Agent(function() {}, { timeout: 1000 });
48 assert.equal(1000, agent.timeout);
50 it('should be mixed in with HTTP request options', function(done) {
51 var agent = new Agent({
56 agent.callback = function(req, opts, fn) {
57 assert.equal('bar', opts.foo);
58 assert.equal('a', opts.b);
60 // `host` and `port` are special-cases, and should always be
61 // overwritten in the request `opts` inside the agent-base callback
62 assert.equal('localhost', opts.host);
63 assert.equal(80, opts.port);
73 describe('`this` context', function() {
74 it('should be the Agent instance', function(done) {
76 var agent = new Agent();
77 agent.callback = function() {
79 assert.equal(this, agent);
81 var info = url.parse('http://127.0.0.1/foo');
83 var req = http.get(info);
84 req.on('error', function(err) {
85 assert(/no Duplex stream was returned/.test(err.message));
89 it('should be the Agent instance with callback signature', function(done) {
91 var agent = new Agent();
92 agent.callback = function(req, opts, fn) {
94 assert.equal(this, agent);
97 var info = url.parse('http://127.0.0.1/foo');
99 var req = http.get(info);
100 req.on('error', function(err) {
101 assert(/no Duplex stream was returned/.test(err.message));
106 describe('"error" event', function() {
107 it('should be invoked on `http.ClientRequest` instance if `callback()` has not been defined', function(
110 var agent = new Agent();
111 var info = url.parse('http://127.0.0.1/foo');
113 var req = http.get(info);
114 req.on('error', function(err) {
116 '"agent-base" has no default implementation, you must subclass and override `callback()`',
122 it('should be invoked on `http.ClientRequest` instance if Error passed to callback function on the first tick', function(
125 var agent = new Agent(function(req, opts, fn) {
126 fn(new Error('is this caught?'));
128 var info = url.parse('http://127.0.0.1/foo');
130 var req = http.get(info);
131 req.on('error', function(err) {
132 assert.equal('is this caught?', err.message);
136 it('should be invoked on `http.ClientRequest` instance if Error passed to callback function after the first tick', function(
139 var agent = new Agent(function(req, opts, fn) {
140 setTimeout(function() {
141 fn(new Error('is this caught?'));
144 var info = url.parse('http://127.0.0.1/foo');
146 var req = http.get(info);
147 req.on('error', function(err) {
148 assert.equal('is this caught?', err.message);
153 describe('artificial "streams"', function() {
154 it('should send a GET request', function(done) {
155 var stream = new events.EventEmitter();
157 // needed for the `http` module to call .write() on the stream
158 stream.writable = true;
160 stream.write = function(str) {
161 assert(0 == str.indexOf('GET / HTTP/1.1'));
165 // needed for `http` module in Node.js 4
166 stream.cork = function() {};
173 agent: new Agent(function(req, opts, fn) {
177 var req = http.request(opts);
180 it('should receive a GET response', function(done) {
181 var stream = new events.EventEmitter();
187 agent: new Agent(function(req, opts, fn) {
191 var req = http.request(opts, function(res) {
192 assert.equal('1.0', res.httpVersion);
193 assert.equal(200, res.statusCode);
194 assert.equal('bar', res.headers.foo);
195 assert.deepEqual(['1', '2'], res.headers['set-cookie']);
199 // have to wait for the "socket" event since `http.ClientRequest`
200 // doesn't *actually* attach the listeners to the "stream" until
202 req.once('socket', function() {
203 var buf = Buffer.from(
206 'Set-Cookie: 1\r\n' +
207 'Set-Cookie: 2\r\n\r\n'
209 stream.emit('data', buf);
217 describe('"http" module', function() {
221 // setup test HTTP server
222 before(function(done) {
223 server = http.createServer();
224 server.listen(0, function() {
225 port = server.address().port;
230 // shut down test HTTP server
231 after(function(done) {
232 server.once('close', function() {
238 it('should work for basic HTTP requests', function(done) {
240 var agent = new Agent(function(req, opts, fn) {
242 var socket = net.connect(opts);
246 // add HTTP server "request" listener
248 server.once('request', function(req, res) {
250 res.setHeader('X-Foo', 'bar');
251 res.setHeader('X-Url', req.url);
255 var info = url.parse('http://127.0.0.1:' + port + '/foo');
257 http.get(info, function(res) {
258 assert.equal('bar', res.headers['x-foo']);
259 assert.equal('/foo', res.headers['x-url']);
266 it('should support direct return in `connect()`', function(done) {
268 var agent = new Agent(function(req, opts) {
270 return net.connect(opts);
273 // add HTTP server "request" listener
275 server.once('request', function(req, res) {
277 res.setHeader('X-Foo', 'bar');
278 res.setHeader('X-Url', req.url);
282 var info = url.parse('http://127.0.0.1:' + port + '/foo');
284 http.get(info, function(res) {
285 assert.equal('bar', res.headers['x-foo']);
286 assert.equal('/foo', res.headers['x-url']);
293 it('should support returning a Promise in `connect()`', function(done) {
295 var agent = new Agent(function(req, opts) {
296 return new Promise(function(resolve, reject) {
298 resolve(net.connect(opts));
302 // add HTTP server "request" listener
304 server.once('request', function(req, res) {
306 res.setHeader('X-Foo', 'bar');
307 res.setHeader('X-Url', req.url);
311 var info = url.parse('http://127.0.0.1:' + port + '/foo');
313 http.get(info, function(res) {
314 assert.equal('bar', res.headers['x-foo']);
315 assert.equal('/foo', res.headers['x-url']);
322 it('should set the `Connection: close` response header', function(done) {
324 var agent = new Agent(function(req, opts, fn) {
326 var socket = net.connect(opts);
330 // add HTTP server "request" listener
332 server.once('request', function(req, res) {
334 res.setHeader('X-Url', req.url);
335 assert.equal('close', req.headers.connection);
339 var info = url.parse('http://127.0.0.1:' + port + '/bar');
341 http.get(info, function(res) {
342 assert.equal('/bar', res.headers['x-url']);
343 assert.equal('close', res.headers.connection);
350 it('should pass through options from `http.request()`', function(done) {
351 var agent = new Agent(function(req, opts, fn) {
352 assert.equal('google.com', opts.host);
353 assert.equal('bar', opts.foo);
364 it('should default to port 80', function(done) {
365 var agent = new Agent(function(req, opts, fn) {
366 assert.equal(80, opts.port);
370 // (probably) not hitting a real HTTP server here,
371 // so no need to add a httpServer request listener
379 it('should support the "timeout" option', function(done) {
380 // ensure we timeout after the "error" event had a chance to trigger
384 var agent = new Agent(
385 function(req, opts, fn) {
386 // this function will time out
391 var opts = url.parse('http://nodejs.org');
394 var req = http.get(opts);
395 req.once('error', function(err) {
396 assert.equal('ETIMEOUT', err.code);
402 it('should free sockets after use', function(done) {
403 var agent = new Agent(function(req, opts, fn) {
404 var socket = net.connect(opts);
408 // add HTTP server "request" listener
410 server.once('request', function(req, res) {
415 var info = url.parse('http://127.0.0.1:' + port + '/foo');
417 http.get(info, function(res) {
418 res.socket.emit('free');
419 assert.equal(true, res.socket.destroyed);
426 describe('PassthroughAgent', function() {
427 it('should pass through to `http.globalAgent`', function(done) {
428 // add HTTP server "request" listener
430 server.once('request', function(req, res) {
432 res.setHeader('X-Foo', 'bar');
433 res.setHeader('X-Url', req.url);
437 var info = url.parse('http://127.0.0.1:' + port + '/foo');
438 info.agent = PassthroughAgent;
439 http.get(info, function(res) {
440 assert.equal('bar', res.headers['x-foo']);
441 assert.equal('/foo', res.headers['x-url']);
449 describe('"https" module', function() {
453 // setup test HTTPS server
454 before(function(done) {
456 key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
457 cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
459 server = https.createServer(options);
460 server.listen(0, function() {
461 port = server.address().port;
466 // shut down test HTTP server
467 after(function(done) {
468 server.once('close', function() {
474 it('should not modify the passed in Options object', function(done) {
476 var agent = new Agent(function(req, opts, fn) {
478 assert.equal(true, opts.secureEndpoint);
479 assert.equal(443, opts.port);
480 assert.equal('localhost', opts.host);
482 var opts = { agent: agent };
483 var req = https.request(opts);
484 assert.equal(true, called);
485 assert.equal(false, 'secureEndpoint' in opts);
486 assert.equal(false, 'port' in opts);
490 it('should work with a String URL', function(done) {
491 var endpoint = 'https://127.0.0.1:' + port;
492 var req = https.get(endpoint);
494 // it's gonna error out since `rejectUnauthorized` is not being passed in
495 req.on('error', function(err) {
496 assert.equal(err.code, 'DEPTH_ZERO_SELF_SIGNED_CERT');
501 it('should work for basic HTTPS requests', function(done) {
503 var agent = new Agent(function(req, opts, fn) {
505 assert(opts.secureEndpoint);
506 var socket = tls.connect(opts);
510 // add HTTPS server "request" listener
512 server.once('request', function(req, res) {
514 res.setHeader('X-Foo', 'bar');
515 res.setHeader('X-Url', req.url);
519 var info = url.parse('https://127.0.0.1:' + port + '/foo');
521 info.rejectUnauthorized = false;
522 https.get(info, function(res) {
523 assert.equal('bar', res.headers['x-foo']);
524 assert.equal('/foo', res.headers['x-url']);
531 it('should pass through options from `https.request()`', function(done) {
532 var agent = new Agent(function(req, opts, fn) {
533 assert.equal('google.com', opts.host);
534 assert.equal('bar', opts.foo);
545 it('should support the 3-argument `https.get()`', function(done) {
546 var agent = new Agent(function(req, opts, fn) {
547 assert.equal('google.com', opts.host);
548 assert.equal('/q', opts.pathname || opts.path);
549 assert.equal('881', opts.port);
550 assert.equal('bar', opts.foo);
555 'https://google.com:881/q',
564 it('should default to port 443', function(done) {
565 var agent = new Agent(function(req, opts, fn) {
566 assert.equal(true, opts.secureEndpoint);
567 assert.equal(false, opts.rejectUnauthorized);
568 assert.equal(443, opts.port);
572 // (probably) not hitting a real HTTPS server here,
573 // so no need to add a httpsServer request listener
578 rejectUnauthorized: false
582 it('should not re-patch https.request', () => {
583 var patchModulePath = "../patch-core";
584 var patchedRequest = https.request;
586 delete require.cache[require.resolve(patchModulePath)];
587 require(patchModulePath);
589 assert.equal(patchedRequest, https.request);
590 assert.equal(true, https.request.__agent_base_https_request_patched__);
593 describe('PassthroughAgent', function() {
594 it('should pass through to `https.globalAgent`', function(done) {
595 // add HTTP server "request" listener
597 server.once('request', function(req, res) {
599 res.setHeader('X-Foo', 'bar');
600 res.setHeader('X-Url', req.url);
604 var info = url.parse('https://127.0.0.1:' + port + '/foo');
605 info.agent = PassthroughAgent;
606 info.rejectUnauthorized = false;
607 https.get(info, function(res) {
608 assert.equal('bar', res.headers['x-foo']);
609 assert.equal('/foo', res.headers['x-url']);
617 describe('"ws" server', function() {
622 // setup test HTTP server
623 before(function(done) {
624 server = http.createServer();
625 wss = new WebSocket.Server({ server: server });
626 server.listen(0, function() {
627 port = server.address().port;
632 // shut down test HTTP server
633 after(function(done) {
634 server.once('close', function() {
640 it('should work for basic WebSocket connections', function(done) {
641 function onconnection(ws) {
642 ws.on('message', function(data) {
643 assert.equal('ping', data);
647 wss.on('connection', onconnection);
649 var agent = new Agent(function(req, opts, fn) {
650 var socket = net.connect(opts);
654 var client = new WebSocket('ws://127.0.0.1:' + port + '/', {
658 client.on('open', function() {
662 client.on('message', function(data) {
663 assert.equal('pong', data);
665 wss.removeListener('connection', onconnection);
671 describe('"wss" server', function() {
676 // setup test HTTP server
677 before(function(done) {
679 key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
680 cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
682 server = https.createServer(options);
683 wss = new WebSocket.Server({ server: server });
684 server.listen(0, function() {
685 port = server.address().port;
690 // shut down test HTTP server
691 after(function(done) {
692 server.once('close', function() {
698 it('should work for secure WebSocket connections', function(done) {
699 function onconnection(ws) {
700 ws.on('message', function(data) {
701 assert.equal('ping', data);
705 wss.on('connection', onconnection);
707 var agent = new Agent(function(req, opts, fn) {
708 var socket = tls.connect(opts);
712 var client = new WebSocket('wss://127.0.0.1:' + port + '/', {
714 rejectUnauthorized: false
717 client.on('open', function() {
721 client.on('message', function(data) {
722 assert.equal('pong', data);
724 wss.removeListener('connection', onconnection);