Skip to content

Commit c1727bd

Browse files
committed
Expose connect options and the client socket via a _mitm property.
Fixes #14.
1 parent 2ee831e commit c1727bd

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ Mitm.prototype.connect = function connect(orig, Socket, opts, done) {
7979
var server = client.server = new Socket({handle: sockets[1]})
8080
this.emit("connection", server, opts)
8181

82+
// Make the client socket and the connect options available via a "private"
83+
// property of the server socket so they can be paired up with requests in
84+
// a reliable way:
85+
server._mitm = {client: client, opts: opts};
86+
8287
// Ensure connect is emitted in next ticks, otherwise it would be impossible
8388
// to listen to it after calling Net.connect or listening to it after the
8489
// ClientRequest emits "socket".

test/index_test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,25 @@ describe("Mitm", function() {
429429
})
430430
})
431431

432+
it("must provide the client socket and options via _mitm", function(done) {
433+
var clientSocket;
434+
var connectOpts;
435+
this.mitm.on("connect", function (socket, opts) {
436+
clientSocket = socket
437+
connectOpts = opts
438+
clientSocket.must.be.an.instanceof(Object)
439+
connectOpts.host.must.equal("foo")
440+
}).on("request", function(req, res) {
441+
req.connection._mitm.must.be.an.instanceof(Object)
442+
req.connection._mitm.opts.must.be(connectOpts)
443+
req.connection._mitm.client.must.be(clientSocket)
444+
done()
445+
})
446+
447+
var client = request({host: "foo"})
448+
client.end()
449+
})
450+
432451
it("must emit request on Mitm after multiple requests", function(done) {
433452
request({host: "foo"}).end()
434453
request({host: "foo"}).end()

0 commit comments

Comments
 (0)