mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
565 stars 127 forks source link

Bug - Unhandled Requests with Custom Agent have wrong `this` context bound #635

Closed tebriel closed 2 months ago

tebriel commented 2 months ago

Thanks for reading my bug report, I'm new to this library and this was a tough one to track down based on the test failures I was seeing in my app.

Description

When an intercepted request is not handled (interceptor.on('request'...) and the request uses a custom HTTP Agent, the custom agent's createConnection method is bound to the MockHttpAgent's this context instead of the custom agent's this context, so methods/local variables are unavailable causing an exception.

Steps to Reproduce

  1. Create a Custom Agent
  2. Implement createConnection and have it reference something else within itself (in my example, this._noop().
  3. Do not register a handler for the interceptor.
  4. Code causes exception

Repro Links

Location of Bug

MockAgent

Specifically, it binds the this context of the MockAgent instead of the this of the customAgent.

Additional question

Only the createConnection are bound from the custom agent, should the other methods from the custom agent be used?

kettanaito commented 2 months ago

Released: v0.35.4 🎉

This has been released in v0.35.4!

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.