Skip to content

Commit 95dee2a

Browse files
committed
Retry policy will always clone the *http.Request (Azure#20843)
* Retry policy will always clone the *http.Request This ensures that the entirety of the request is restored on retries. * simplify test
1 parent 9c9d62a commit 95dee2a

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

sdk/azcore/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking Changes
88

99
### Bugs Fixed
10+
* Retry policy always clones the underlying `*http.Request` before invoking the next policy.
1011

1112
### Other Changes
1213

sdk/azcore/runtime/policy_retry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ func (p *retryPolicy) Do(req *policy.Request) (resp *http.Response, err error) {
125125
}
126126

127127
if options.TryTimeout == 0 {
128-
resp, err = req.Next()
128+
clone := req.Clone(req.Raw().Context())
129+
resp, err = clone.Next()
129130
} else {
130131
// Set the per-try time for this particular retry operation and then Do the operation.
131132
tryCtx, tryCancel := context.WithTimeout(req.Raw().Context(), options.TryTimeout)

sdk/azcore/runtime/policy_retry_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,39 @@ func TestRetryableRequestBodyWithCloser(t *testing.T) {
810810
require.True(t, tr.closeCalled)
811811
}
812812

813+
func TestRetryPolicySuccessWithRetryPreserveHeaders(t *testing.T) {
814+
srv, close := mock.NewServer()
815+
defer close()
816+
srv.AppendResponse(mock.WithStatusCode(http.StatusRequestTimeout))
817+
srv.AppendResponse()
818+
pl := exported.NewPipeline(srv, NewRetryPolicy(testRetryOptions()), exported.PolicyFunc(challengeLikePolicy))
819+
req, err := NewRequest(context.Background(), http.MethodGet, srv.URL())
820+
require.NoError(t, err)
821+
body := newRewindTrackingBody("stuff")
822+
require.NoError(t, req.SetBody(body, "text/plain"))
823+
resp, err := pl.Do(req)
824+
require.NoError(t, err)
825+
require.EqualValues(t, http.StatusOK, resp.StatusCode)
826+
require.EqualValues(t, 2, srv.Requests())
827+
require.EqualValues(t, 1, body.rcount)
828+
require.True(t, body.closed)
829+
}
830+
831+
func challengeLikePolicy(req *policy.Request) (*http.Response, error) {
832+
if req.Body() == nil {
833+
return nil, errors.New("request body wasn't restored")
834+
}
835+
if req.Raw().Header.Get("content-type") != "text/plain" {
836+
return nil, errors.New("content-type header wasn't restored")
837+
}
838+
839+
// remove the body and header. the retry policy should restore them
840+
if err := req.SetBody(nil, ""); err != nil {
841+
return nil, err
842+
}
843+
return req.Next()
844+
}
845+
813846
func newRewindTrackingBody(s string) *rewindTrackingBody {
814847
// there are two rewinds that happen before rewinding for a retry
815848
// 1. to get the body's size in SetBody()

0 commit comments

Comments
 (0)