[Recorder] Ensure redirected requests are passed to the recorder in Node #21355
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.
Packages impacted by this PR
@azure-tools/test-recorder
Issues associated with this PR
Describe the problem that is addressed by this PR
When the service returns a redirect response, our test proxy policy would not properly rewrite the second request that is made by the pipeline due to the redirection. This meant that the second request would go to the service directly, instead of via the test proxy, which causes a number of issues.
Until recently, the test proxy has been following redirects before returning responses during record mode. This has been identified as incorrect behavior (see some discussion at Azure/azure-sdk-tools#2982), and a change is in the works to make the test proxy follow redirects by default (although the old behavior will still be configurable -- we'll need this for the browser scenario, since we don't control redirection behavior there). This meant that the issue this PR fixes couldn't be encountered until now, since the test proxy would not return a redirect response under any circumstance.
The fix involves changing the recorder HTTP policy slightly. Previously, we didn't rewrite the request if the
X-Recording-Id
header was present (suggesting we had already redirected the request). This logic falls through if the second time the request passes through the HTTP policy, the request hostname has been changed (e.g. due to a redirect). That case would mean we should rewrite the request to point to the test proxy a second time. I have updated the logic so that we instead check if the request URL is already pointing to the test proxy, and if so, then we don't need to rewrite the request. This check is more robust and works in the case of a cross-domain redirect.Are there test cases added in this PR? (If not, why?)
Yes. Added a few cases: