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
637 stars 48 forks source link

does this work with node.js 14? #71

Closed jchip closed 3 years ago

jchip commented 3 years ago

I have some old project using this for unit tests but they are now failing with timeout on node.js 14.

jchip commented 3 years ago

the issue is because of Content-Length. I have this:

      mitm.on("request", (req, res) => {
        verifyRequestHeaders(req);
        res.writeHead(401, {
          "Content-Type": "application/json; charset=utf-8",
          "Content-Length": 50
        });
        res.end(JSON.stringify({ error: "Invalid login or password." }));
      });

turns out the data changed but length didn't update and node.js 14 throw ReadError now.

moll commented 3 years ago

Hey! I'm glad you found the problem already. For the record, yep, Mitm.js should be fully supported on all Node versions all the way back to 0.10 and up to 16 and beyond. :)

moll commented 3 years ago

Btw, I don't think you need to set the Content-Length header manually. I believe Node.js does it for you in your example or just defaults to chunked-encoding, which should be supported by whichever library you were using to do the request in the first place.