Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose connect options and the client socket via a _mitm property #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ Mitm.prototype.connect = function connect(orig, Socket, opts, done) {
var server = client.server = new Socket({handle: sockets[1]})
this.emit("connection", server, opts)

// Make the client socket and the connect options available via a "private"
// property of the server socket so they can be paired up with requests in
// a reliable way:
server._mitm = {client: client, opts: opts};

// Ensure connect is emitted in next ticks, otherwise it would be impossible
// to listen to it after calling Net.connect or listening to it after the
// ClientRequest emits "socket".
Expand Down
19 changes: 19 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,25 @@ describe("Mitm", function() {
})
})

it("must provide the client socket and options via _mitm", function(done) {
var clientSocket;
var connectOpts;
this.mitm.on("connect", function (socket, opts) {
clientSocket = socket
connectOpts = opts
clientSocket.must.be.an.instanceof(Object)
connectOpts.host.must.equal("foo")
}).on("request", function(req, res) {
req.connection._mitm.must.be.an.instanceof(Object)
req.connection._mitm.opts.must.be(connectOpts)
req.connection._mitm.client.must.be(clientSocket)
done()
})

var client = request({host: "foo"})
client.end()
})

it("must emit request on Mitm after multiple requests", function(done) {
request({host: "foo"}).end()
request({host: "foo"}).end()
Expand Down