Skip to content

Commit 33918ed

Browse files
watsoncjihrig
authored andcommitted
test: test net.connect monkey patching
This is important for people who monkey-patches `Socket.prototype.connect` since it's not possible to monkey-patch `net.connect` directly (as the core `connect` function is called internally in Node instead of calling the `exports.connect` function). Monkey-patching of `Socket.prototype.connect` is done by - among others - most APM vendors, the async-listener module and the continuation-local-storage module.
1 parent 44de1fc commit 33918ed

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
// This test checks that calling `net.connect` internally calls
4+
// `Socket.prototype.connect`.
5+
//
6+
// This is important for people who monkey-patch `Socket.prototype.connect`
7+
// since it's not possible to monkey-patch `net.connect` directly (as the core
8+
// `connect` function is called internally in Node instead of calling the
9+
// `exports.connect` function).
10+
//
11+
// Monkey-patching of `Socket.prototype.connect` is done by - among others -
12+
// most APM vendors, the async-listener module and the
13+
// continuation-local-storage module.
14+
//
15+
// See https://github.com/nodejs/node/pull/12852 for details.
16+
17+
const common = require('../common');
18+
const net = require('net');
19+
const Socket = net.Socket;
20+
21+
// monkey patch Socket.prototype.connect to check that it's called
22+
const orig = Socket.prototype.connect;
23+
Socket.prototype.connect = common.mustCall(function() {
24+
return orig.apply(this, arguments);
25+
});
26+
27+
const server = net.createServer();
28+
29+
server.listen(common.mustCall(function() {
30+
const port = server.address().port;
31+
const client = net.connect({port}, common.mustCall(function() {
32+
client.end();
33+
}));
34+
client.on('end', common.mustCall(function() {
35+
server.close();
36+
}));
37+
}));

0 commit comments

Comments
 (0)