Skip to content

Commit

Permalink
Expose connect options and the client socket via a _mitm property.
Browse files Browse the repository at this point in the history
Fixes moll#14.
  • Loading branch information
papandreou committed Aug 25, 2016
1 parent 22cbd00 commit eaf0459
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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

0 comments on commit eaf0459

Please sign in to comment.