11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This test checks that calling `net.connect` internally calls
51cb0ef41Sopenharmony_ci// `Socket.prototype.connect`.
61cb0ef41Sopenharmony_ci//
71cb0ef41Sopenharmony_ci// This is important for people who monkey-patch `Socket.prototype.connect`
81cb0ef41Sopenharmony_ci// since it's not possible to monkey-patch `net.connect` directly (as the core
91cb0ef41Sopenharmony_ci// `connect` function is called internally in Node instead of calling the
101cb0ef41Sopenharmony_ci// `exports.connect` function).
111cb0ef41Sopenharmony_ci//
121cb0ef41Sopenharmony_ci// Monkey-patching of `Socket.prototype.connect` is done by - among others -
131cb0ef41Sopenharmony_ci// most APM vendors, the async-listener module and the
141cb0ef41Sopenharmony_ci// continuation-local-storage module.
151cb0ef41Sopenharmony_ci//
161cb0ef41Sopenharmony_ci// Related:
171cb0ef41Sopenharmony_ci// - https://github.com/nodejs/node/pull/12342
181cb0ef41Sopenharmony_ci// - https://github.com/nodejs/node/pull/12852
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst net = require('net');
211cb0ef41Sopenharmony_ciconst Socket = net.Socket;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// Monkey patch Socket.prototype.connect to check that it's called.
241cb0ef41Sopenharmony_ciconst orig = Socket.prototype.connect;
251cb0ef41Sopenharmony_ciSocket.prototype.connect = common.mustCall(function() {
261cb0ef41Sopenharmony_ci  return orig.apply(this, arguments);
271cb0ef41Sopenharmony_ci});
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst server = net.createServer();
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciserver.listen(common.mustCall(function() {
321cb0ef41Sopenharmony_ci  const port = server.address().port;
331cb0ef41Sopenharmony_ci  const client = net.connect({ port }, common.mustCall(function() {
341cb0ef41Sopenharmony_ci    client.end();
351cb0ef41Sopenharmony_ci  }));
361cb0ef41Sopenharmony_ci  client.on('end', common.mustCall(function() {
371cb0ef41Sopenharmony_ci    server.close();
381cb0ef41Sopenharmony_ci  }));
391cb0ef41Sopenharmony_ci}));
40