moll / node-mitm

Intercept and mock outgoing Node.js network TCP connections and HTTP requests for testing. Intercepts and gives you a Net.Socket, Http.IncomingMessage and Http.ServerResponse to test and respond with. Super useful when testing code that hits remote servers.
Other
641 stars 48 forks source link

mitm return 400 on custom methods #63

Closed Oloompa closed 4 years ago

Oloompa commented 4 years ago

RFC 2616 and 7540 allow use of custom methods.

When we use other methods, mitm always return 400 without running request event.

moll commented 4 years ago

Hey,

While I'm not sure what responds with 400 instead of just blowing up, you're right that Mitm.js doesn't seem to support custom HTTP methods. Node.js v6 for example just throws "socket hang up". Turns out Node.js' web server itself doesn't support custom HTTP methods — https://github.com/nodejs/node-v0.x-archive/issues/3192. That came as a surprise to me. Because Mitm.js delegates HTTP request parsing to Node.js itself, Mitm.js can't support custom methods out of the box either.

If you've got a custom HTTP parser, you could, however, hack support for that, I suppose, by overwriting Mitm.prototype.request (https://github.com/moll/node-mitm/blob/master/index.js#L146) and not invoking the default Http._connectionListener. Instead, get the request body from the socket.serverSocket and parse that yourself, finally triggering request on the Mitm instance.

Let me know what you decide to do.

moll commented 4 years ago

I've added a note to the README: https://github.com/moll/node-mitm#custom-http-methods.

Oloompa commented 4 years ago

I didn't notice nodejs was blocking custom methods. The "issue" has been open in 2012 for nodejs without any move. I will probably take a look on deno.

moll commented 4 years ago

I didn't know that either. Seems like a fairly arbitrary limitation on Node.js's part. I'll close the issue as there's not much to do right now, but we can continue chatting.