summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/test/test.js
blob: f87d308f8e65a2843f7c6746e3fc252291ec4fcf (plain)
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

/**
 * Module dependencies.
 */

var fs = require('fs');
var url = require('url');
var net = require('net');
var tls = require('tls');
var http = require('http');
var https = require('https');
var assert = require('assert');
var events = require('events');
var Agent = require('../');

describe('Agent', function () {
  describe('"error" event', function () {
    it('should be invoked on `http.ClientRequest` instance if passed to callback function on the first tick', function (done) {
      var agent = new Agent(function (req, opts, fn) {
        fn(new Error('is this caught?'));
      });
      var info = url.parse('http://127.0.0.1/foo');
      info.agent = agent;
      var req = http.get(info);
      req.on('error', function (err) {
        assert.equal('is this caught?', err.message);
        done();
      });
    });
    it('should be invoked on `http.ClientRequest` instance if passed to callback function after the first tick', function (done) {
      var agent = new Agent(function (req, opts, fn) {
        setTimeout(function () {
          fn(new Error('is this caught?'));
        }, 10);
      });
      var info = url.parse('http://127.0.0.1/foo');
      info.agent = agent;
      var req = http.get(info);
      req.on('error', function (err) {
        assert.equal('is this caught?', err.message);
        done();
      });
    });
  });
  describe('artificial "streams"', function () {
    it('should send a GET request', function (done) {
      var stream = new events.EventEmitter();

      // needed for the `http` module to call .write() on the stream
      stream.writable = true;

      stream.write = function (str) {
        assert(0 == str.indexOf('GET / HTTP/1.1'));
        done();
      };

      var opts = {
        method: 'GET',
        host: '127.0.0.1',
        path: '/',
        port: 80,
        agent: new Agent(function (req, opts, fn) {
          fn(null, stream);
        })
      };
      var req = http.request(opts);
      req.end();
    });
    it('should receive a GET response', function (done) {
      var stream = new events.EventEmitter();
      var opts = {
        method: 'GET',
        host: '127.0.0.1',
        path: '/',
        port: 80,
        agent: new Agent(function (req, opts, fn) {
          fn(null, stream);
        })
      };
      var req = http.request(opts, function (res) {
        assert.equal('0.9', res.httpVersion);
        assert.equal(111, res.statusCode);
        assert.equal('bar', res.headers.foo);
        done();
      });
      req.end();

      // have to nextTick() since `http.ClientRequest` doesn't *actually*
      // attach the listeners to the "stream" until the next tick :\
      process.nextTick(function () {
        var buf = new Buffer('HTTP/0.9 111\r\n' +
                             'Foo: bar\r\n' +
                             'Set-Cookie: 1\r\n' +
                             'Set-Cookie: 2\r\n\r\n');
        if ('function' == typeof stream.ondata) {
          // node <= v0.11.3
          stream.ondata(buf, 0, buf.length);
        } else {
          // node > v0.11.3
          stream.emit('data', buf);
        }
      });
    });
  });
});

describe('"http" module', function () {
  var server;
  var port;

  // setup test HTTP server
  before(function (done) {
    server = http.createServer();
    server.listen(0, function () {
      port = server.address().port;
      done();
    });
  });

  // shut down test HTTP server
  after(function (done) {
    server.once('close', function () {
      done();
    });
    server.close();
  });

  it('should work for basic HTTP requests', function (done) {
    var called = false;
    var agent = new Agent(function (req, opts, fn) {
      called = true;
      var socket = net.connect(opts);
      fn(null, socket);
    });

    // add HTTP server "request" listener
    var gotReq = false;
    server.once('request', function (req, res) {
      gotReq = true;
      res.setHeader('X-Foo', 'bar');
      res.setHeader('X-Url', req.url);
      res.end();
    });

    var info = url.parse('http://127.0.0.1:' + port + '/foo');
    info.agent = agent;
    http.get(info, function (res) {
      assert.equal('bar', res.headers['x-foo']);
      assert.equal('/foo', res.headers['x-url']);
      assert(gotReq);
      assert(called);
      done();
    });
  });

  it('should set the `Connection: close` response header', function (done) {
    var called = false;
    var agent = new Agent(function (req, opts, fn) {
      called = true;
      var socket = net.connect(opts);
      fn(null, socket);
    });

    // add HTTP server "request" listener
    var gotReq = false;
    server.once('request', function (req, res) {
      gotReq = true;
      res.setHeader('X-Url', req.url);
      assert.equal('close', req.headers.connection);
      res.end();
    });

    var info = url.parse('http://127.0.0.1:' + port + '/bar');
    info.agent = agent;
    http.get(info, function (res) {
      assert.equal('/bar', res.headers['x-url']);
      assert.equal('close', res.headers.connection);
      assert(gotReq);
      assert(called);
      done();
    });
  });

  it('should pass through options from `http.request()`', function (done) {
    var agent = new Agent(function (req, opts, fn) {
      assert.equal('google.com', opts.host);
      assert.equal('bar', opts.foo);
      done();
    });

    http.get({
      host: 'google.com',
      foo: 'bar',
      agent: agent
    });
  });

  it('should default to port 80', function (done) {
    var agent = new Agent(function (req, opts, fn) {
      assert.equal(80, opts.port);
      done();
    });

    // (probably) not hitting a real HTTP server here,
    // so no need to add a httpServer request listener
    http.get({
      host: '127.0.0.1',
      path: '/foo',
      agent: agent
    });
  });
});

describe('"https" module', function () {
  var server;
  var port;

  // setup test HTTPS server
  before(function (done) {
    var options = {
      key: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.key'),
      cert: fs.readFileSync(__dirname + '/ssl-cert-snakeoil.pem')
    };
    server = https.createServer(options);
    server.listen(0, function () {
      port = server.address().port;
      done();
    });
  });

  // shut down test HTTP server
  after(function (done) {
    server.once('close', function () {
      done();
    });
    server.close();
  });

  it('should work for basic HTTPS requests', function (done) {
    var called = false;
    var agent = new Agent(function (req, opts, fn) {
      called = true;
      assert(opts.secureEndpoint);
      var socket = tls.connect(opts);
      fn(null, socket);
    });

    // add HTTPS server "request" listener
    var gotReq = false;
    server.once('request', function (req, res) {
      gotReq = true;
      res.setHeader('X-Foo', 'bar');
      res.setHeader('X-Url', req.url);
      res.end();
    });

    var info = url.parse('https://127.0.0.1:' + port + '/foo');
    info.agent = agent;
    info.rejectUnauthorized = false;
    https.get(info, function (res) {
      assert.equal('bar', res.headers['x-foo']);
      assert.equal('/foo', res.headers['x-url']);
      assert(gotReq);
      assert(called);
      done();
    });
  });

  it('should pass through options from `https.request()`', function (done) {
    var agent = new Agent(function (req, opts, fn) {
      assert.equal('google.com', opts.host);
      assert.equal('bar', opts.foo);
      done();
    });

    https.get({
      host: 'google.com',
      foo: 'bar',
      agent: agent
    });
  });

  it('should default to port 443', function (done) {
    var agent = new Agent(function (req, opts, fn) {
      assert.equal(true, opts.secureEndpoint);
      assert.equal(false, opts.rejectUnauthorized);
      assert.equal(443, opts.port);
      done();
    });

    // (probably) not hitting a real HTTPS server here,
    // so no need to add a httpsServer request listener
    https.get({
      host: '127.0.0.1',
      path: '/foo',
      agent: agent,
      rejectUnauthorized: false
    });
  });
});