• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const http = require('http');
25const net = require('net');
26const assert = require('assert');
27
28const server = http.createServer(common.mustCall(function(req, res) {
29  res.end();
30}, 4)).listen(0, '127.0.0.1', function() {
31  let fn = common.mustCall(createConnection);
32  http.get({ createConnection: fn }, function(res) {
33    res.resume();
34    fn = common.mustCall(createConnectionAsync);
35    http.get({ createConnection: fn }, function(res) {
36      res.resume();
37      fn = common.mustCall(createConnectionBoth1);
38      http.get({ createConnection: fn }, function(res) {
39        res.resume();
40        fn = common.mustCall(createConnectionBoth2);
41        http.get({ createConnection: fn }, function(res) {
42          res.resume();
43          fn = common.mustCall(createConnectionError);
44          http.get({ createConnection: fn }, function(res) {
45            assert.fail('Unexpected response callback');
46          }).on('error', common.mustCall(function(err) {
47            assert.strictEqual(err.message, 'Could not create socket');
48            server.close();
49          }));
50        });
51      });
52    });
53  });
54});
55
56function createConnection() {
57  return net.createConnection(server.address().port, '127.0.0.1');
58}
59
60function createConnectionAsync(options, cb) {
61  setImmediate(function() {
62    cb(null, net.createConnection(server.address().port, '127.0.0.1'));
63  });
64}
65
66function createConnectionBoth1(options, cb) {
67  const socket = net.createConnection(server.address().port, '127.0.0.1');
68  setImmediate(function() {
69    cb(null, socket);
70  });
71  return socket;
72}
73
74function createConnectionBoth2(options, cb) {
75  const socket = net.createConnection(server.address().port, '127.0.0.1');
76  cb(null, socket);
77  return socket;
78}
79
80function createConnectionError(options, cb) {
81  process.nextTick(cb, new Error('Could not create socket'));
82}
83