fix(mock): native fetch bypasses MockAgent due to allowH2#5448
Open
Megashubham wants to merge 2 commits into
Open
fix(mock): native fetch bypasses MockAgent due to allowH2#5448Megashubham wants to merge 2 commits into
Megashubham wants to merge 2 commits into
Conversation
metcoder95
reviewed
Jun 23, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5448 +/- ##
==========================================
- Coverage 93.46% 93.46% -0.01%
==========================================
Files 110 110
Lines 37106 37110 +4
==========================================
+ Hits 34682 34685 +3
- Misses 2424 2425 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Author
|
Can you check once :P |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5036
Description
This PR fixes an issue where native Node.js
fetchqueries would bypass theMockAgentglobally registered usingsetGlobalDispatcher.Root Cause
In version 8.0.3, a change was introduced to wrap the legacy global dispatcher with a
Dispatcher1Wrapper(fix: mirror the legacy global dispatcher for built-in fetch). To support legacy consumers,Dispatcher1Wrapper.dispatchmodifiesoptsto forcefully addallowH2: false.Because of this,
native node fetchpassesallowH2: falsewhen delegating to theMockAgent. WhenMockAgentproxies the request to its internal encapsulatedAgentinstance:MockAgent.get(origin)pre-registers and creates aMockClientfor theoriginexactly as-is.Agent.dispatchchecksallowH2: falseand modifies the lookup key to be${origin}#http1-only.MockClientregistered is under${origin}, the cache miss causes the underlyingAgentto instantiate a brand new realClientunder${origin}#http1-only.Fix
The mock interceptor system shouldn't be concerned about whether
HTTP/2orHTTP/1.1is being explicitly requested by the wrapper. To solve this,MockAgent.dispatchnow safely deletes theallowH2flag fromdispatchOptsif it is set tofalse. This ensures the underlying encapsulatedAgentretains the standard origin as the lookup key and successfully resolves to the pre-registeredMockClient.A regression test using native
globalThis.fetchhas also been added intest/issue-5036.js.