Skip to content
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

feat(XMLHttpRequest): use proxies to implement request interception #337

Merged
merged 11 commits into from
Feb 17, 2023

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Feb 15, 2023

It should be safe migrating to Proxies due to their wide browser support.

Changes

  • Uses ES6 Proxies to spy on globalThis.XMLHttpRequest.
  • Proxies request.headers changes of the Fetch API representation of an XMLHttpRequest to update the underlying XHR instance correctly (in the case the "request" listener has modified the request headers).
  • Fixes the issue where a readystatechange event has been fired twice for XMLHttpRequest: once from the XHR instance itself, and the second time from the underlying http implementation from JSDOM.
  • Sets credentials to same-origin (previously omit) when the withCredentials property of XMLHttpRequest is set to false or not set.
  • Deleting a request header in the "request" event listener will now raise a warning for XMLHttpRequest. XHR headers cannot be deleted per specification.
  • Spec-compliance: We now throw InvalidStateError when reading the request body improperly (e.g. using responseText when the responseType is not an empty string or "text"). This also helps to improve some of our tests as they were reading the response body of XMLHttpRequest incorrectly.

Roadmap

  • Add internal logger to XMLHttpRequestController
  • Emit the "response" event upon bypassed responses. There's also no test for this, apparently.
  • Remove the previous implementation (XMLHttpRequestOverride).
  • Consider if setting the X-Request-Id on the bypassed requests is still needed:
    * @note Set the intercepted request ID on the original request
    * so that if it triggers any other interceptors, they don't attempt
    * to process it once again. This happens when bypassing XMLHttpRequest
    * because it's polyfilled with "http.ClientRequest" in JSDOM.
    */
    originalRequest.setRequestHeader('X-Request-Id', requestId)

@kettanaito kettanaito marked this pull request as draft February 15, 2023 00:05
@kettanaito kettanaito marked this pull request as ready for review February 16, 2023 23:47
@kettanaito kettanaito merged commit 5540e3a into main Feb 17, 2023
@kettanaito kettanaito deleted the feat/use-proxy branch February 17, 2023 00:01
@kettanaito
Copy link
Member Author

Released: v0.20.0 🎉

This has been released in v0.20.0!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant