diff --git a/sdk/internal/CHANGELOG.md b/sdk/internal/CHANGELOG.md index e25553eae311..b14787b52493 100644 --- a/sdk/internal/CHANGELOG.md +++ b/sdk/internal/CHANGELOG.md @@ -8,6 +8,8 @@ ### Bugs Fixed +* Recording will restore the original scheme/host after making a successful HTTP(s) call. + ### Other Changes ## 1.5.0 (2023-11-02) diff --git a/sdk/internal/recording/recording.go b/sdk/internal/recording/recording.go index fd15bfe758f5..df82a14b8cb5 100644 --- a/sdk/internal/recording/recording.go +++ b/sdk/internal/recording/recording.go @@ -537,10 +537,10 @@ func defaultOptions() *RecordingOptions { } } -func (r RecordingOptions) ReplaceAuthority(t *testing.T, rawReq *http.Request) *http.Request { +func (r RecordingOptions) ReplaceAuthority(t *testing.T, rawReq *http.Request) (*http.Request, string, string) { + originalURLScheme := rawReq.URL.Scheme + originalURLHost := rawReq.URL.Host if GetRecordMode() != LiveMode && !IsLiveOnly(t) { - originalURLHost := rawReq.URL.Host - // don't modify the original request cp := *rawReq cpURL := *cp.URL @@ -556,7 +556,7 @@ func (r RecordingOptions) ReplaceAuthority(t *testing.T, rawReq *http.Request) * cp.Header.Set(IDHeader, GetRecordingId(t)) rawReq = &cp } - return rawReq + return rawReq, originalURLScheme, originalURLHost } func (r RecordingOptions) host() string { @@ -941,8 +941,17 @@ type RecordingHTTPClient struct { } func (c RecordingHTTPClient) Do(req *http.Request) (*http.Response, error) { - req = c.options.ReplaceAuthority(c.t, req) - return c.defaultClient.Do(req) + req, origScheme, origHost := c.options.ReplaceAuthority(c.t, req) + resp, err := c.defaultClient.Do(req) + if err != nil { + return nil, err + } + // if the request succeeds, restore the scheme/host with their original values. + // this is imporant for things like LROs that might use the originating URL to + // poll for status and/or fetch the final result. + resp.Request.URL.Scheme = origScheme + resp.Request.URL.Host = origHost + return resp, nil } // NewRecordingHTTPClient returns a type that implements `azcore.Transporter`. This will automatically route tests on the `Do` call. diff --git a/sdk/internal/recording/recording_test.go b/sdk/internal/recording/recording_test.go index 1d93807d1839..bc1d93e76fbb 100644 --- a/sdk/internal/recording/recording_test.go +++ b/sdk/internal/recording/recording_test.go @@ -493,8 +493,8 @@ func (s *recordingTests) TestStartStopRecordingClient() { require.NoError(err) require.Equal("https://azsdkengsys.azurecr.io/acr/v1/some_registry/_tags", data.Entries[0].RequestURI) - require.Equal(resp.Request.URL.String(), - fmt.Sprintf("%s/acr/v1/some_registry/_tags", defaultOptions().baseURL())) + require.Equal("https://azsdkengsys.azurecr.io/acr/v1/some_registry/_tags", + resp.Request.URL.String()) } func (s *recordingTests) TestStopRecordingNoStart() { diff --git a/sdk/internal/recording/testdata/recordings/TestRecording/TestGenerateAlphaNumericID.json b/sdk/internal/recording/testdata/recordings/TestRecording/TestGenerateAlphaNumericID.json index 0253f69888b3..f7f287bc1d2d 100644 --- a/sdk/internal/recording/testdata/recordings/TestRecording/TestGenerateAlphaNumericID.json +++ b/sdk/internal/recording/testdata/recordings/TestRecording/TestGenerateAlphaNumericID.json @@ -1,6 +1,6 @@ { "Entries": [], "Variables": { - "randSeed": "1689722394" + "randSeed": "1701821574" } }