• 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
28function commonHttpGet(fn) {
29  if (typeof fn === 'function') {
30    fn = common.mustCall(fn);
31  }
32  return new Promise((resolve, reject) => {
33    http.get({ createConnection: fn }, (res) => {
34      resolve(res);
35    }).on('error', (err) => {
36      reject(err);
37    });
38  });
39}
40
41const server = http.createServer(common.mustCall(function(req, res) {
42  res.end();
43}, 4)).listen(0, '127.0.0.1', async () => {
44  await commonHttpGet(createConnection);
45  await commonHttpGet(createConnectionAsync);
46  await commonHttpGet(createConnectionBoth1);
47  await commonHttpGet(createConnectionBoth2);
48
49  // Errors
50  await assert.rejects(() => commonHttpGet(createConnectionError), {
51    message: 'sync'
52  });
53  await assert.rejects(() => commonHttpGet(createConnectionAsyncError), {
54    message: 'async'
55  });
56
57  server.close();
58});
59
60function createConnection() {
61  return net.createConnection(server.address().port, '127.0.0.1');
62}
63
64function createConnectionAsync(options, cb) {
65  setImmediate(function() {
66    cb(null, net.createConnection(server.address().port, '127.0.0.1'));
67  });
68}
69
70function createConnectionBoth1(options, cb) {
71  const socket = net.createConnection(server.address().port, '127.0.0.1');
72  setImmediate(function() {
73    cb(null, socket);
74  });
75  return socket;
76}
77
78function createConnectionBoth2(options, cb) {
79  const socket = net.createConnection(server.address().port, '127.0.0.1');
80  cb(null, socket);
81  return socket;
82}
83
84function createConnectionError(options, cb) {
85  throw new Error('sync');
86}
87
88function createConnectionAsyncError(options, cb) {
89  process.nextTick(cb, new Error('async'));
90}
91