-
-
Notifications
You must be signed in to change notification settings - Fork 131
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
[XHR] Response interceptors executes after application receives the response #400
Comments
@kettanaito Any idea why I'm getting this behaviour? |
Hey, @JAspeling. Thanks for a great issue report. Let me provide some additional context and let's see whether we're on the same page. IntentionsI can confirm that the intention of this library is to emit the Regarding the interceptors/src/interceptors/fetch/index.ts Lines 99 to 111 in 36400cc
But that's not what's happening. These two callbacks will execute one after another, but both happen once the response has been received (the request promise has been resolved). It is the Promise-like nature of handling fetch responses that may be confusing here. It's important to note that response received isn't equivalent to response read. This stands true for fetch and for interceptors/src/interceptors/ClientRequest/NodeClientRequest.ts Lines 249 to 254 in 36400cc
Regarding
|
If we can find a way to represent the incoming XHR response body as |
Thank you for the detailed explanation! I will take the discussion up with my team and perhaps try to get them involved as well - maybe we can find a solution together. I'll do some research on the subject 👍 |
@kettanaito could you pls help with a fix? I am encountering the same issue. The response is sent before the interceptor request is completed. |
Released: v0.32.0 🎉This has been released in v0.32.0! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Issue Title
XHR Response interceptors get executed after the application receives the response.
Description
I expect to see the response interceptor functions execute before the application receives the response. This is not the case with
XMLHttpRequestInterceptor
. It is, however, the case withFetchInterceptor
Context
I am working on a solution where we need to intercept all HTTP requests fired by any client (fetch, Angular, Axios, etc). There's no telling which underlying HTTP method is used to make the request, so it should cater to all.
We came across this
@mswjs/interceptors
package that looked promising as it allows us to intercept all requests, but in the POC, we encountered some strange behaviour.The expected flow would be:
This should be the same for all HTTP requests.
Current Behavior
I've noticed that XHR requests it does not follow the above pattern. Instead, they do the following:
Expected Behavior
I expect the interceptor functions to be executed in the same order, whichever HTTP method is used.
Request interceptor functions should be executed before the request reaches the API, and Response interceptor functions should be executed before the response reaches the application.
Screenshots and Additional Information (if applicable)
Steps to Reproduce (if applicable)
Please see the reproduction repo where the below behaviour can be seen:
Github Repo: https://github.com/JAspeling/mswjs-interceptors-angular/blob/main/src/main.ts
Stackblitz: https://stackblitz-starters-dgrmdd.stackblitz.io
Stackblitz editor: https://stackblitz.com/edit/stackblitz-starters-dgrmdd?file=src%2Fmain.ts
XMLHttpRequestInterceptor
as a BatchInterceptor.Environment
Please feel free to reach out if anything is unclear or if there is a misunderstanding around using the
@mswjs/interceptors
package.The text was updated successfully, but these errors were encountered: