fix(ClientRequest): allow GET requests with body#715
Conversation
kettanaito
commented
Apr 4, 2025
- Fixes nock 14 regression: GET with body causes Uncaught Invariant Violation nock/nock#2826
|
Hi, @mikicho 👋 I've tried to create a reproduction scenario for this issue but failed at that. The problem is that the body isn't being handled by the HTTP parser at all in this case. It reaches this: interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts Lines 85 to 87 in 77141e2 But this never fires: The intention behind the current test is to throw with the same error as in nock/nock#2826: Could you please take a look at this and let me know if you spot anything I missed? Once we get the test failing correctly, we can proceed with the fix. |
|
@kettanaito I added Regarding the solution, we need the request body in the interceptor for interception and recording. |
|
@mikicho, thanks for looking into this one. There's a lot of difference between the raw request/response instances and the Fetch API representation that we are constructing. I understand how this can bite us back sometimes. But this alignment is still highly worth it! I am not a big fan of In the very early version of Interceptors, we also exposed the raw client request instance on the interceptor. I don't know if I like it, but we technically can do this: interceptor.on('request', ({ request, rawRequest }) => {
request // Request
rawRequest // ClientRequest OR XMLHttpRequest OR Request
})
This kind of API was removed because it introduces significant overhead at the usage layer. You become in charge to differentiate different request types, which kills the purpose of a single interception library. I also agree that all these use cases so far were edge cases. You normally don't need raw headers, for example, and not all request clients even support thats as a concept. I can also argue that HTTP headers are case-insensitive by design so the concept of recording and exposing raw headers is also flawed by design. I may be wrong on this so feel free to point that out. |
|
@kettanaito We see eye to eye here. |
There's quite an important distinction between these two approaches, though. Exposing Given how we are largely discussing edge cases, I think we should not build more logic around them than necessary (which is exactly the point you bring up and I agree). I wonder if we could remove the raw headers recording entirely and replace it with
import { getRawRequest } from '@mswjs/interceptors'
interceptor.on('request', ({ request }) => {
const rawRequest = getRawRequest(request)
}) |
1282c4e to
25d7998
Compare
Released: v0.38.2 🎉This has been released in v0.38.2! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Yes. Definitely into this kind of thing. |