Skip to content

Commit

Permalink
sigv4: support nil body (#673)
Browse files Browse the repository at this point in the history
Fixes #562
Fixes #465

Signed-off-by: Julien Pivotto <[email protected]>
  • Loading branch information
roidelapluie authored Aug 11, 2024
1 parent 430dbfe commit 1dade5b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 7 additions & 4 deletions sigv4/sigv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,14 @@ func (rt *sigV4RoundTripper) RoundTrip(req *http.Request) (*http.Response, error
buf.Reset()
rt.pool.Put(buf)
}()
if _, err := io.Copy(buf, req.Body); err != nil {
return nil, err

if req.Body != nil {
if _, err := io.Copy(buf, req.Body); err != nil {
return nil, err
}
// Close the original body since we don't need it anymore.
_ = req.Body.Close()
}
// Close the original body since we don't need it anymore.
_ = req.Body.Close()

// Ensure our seeker is back at the start of the buffer once we return.
var seeker io.ReadSeeker = bytes.NewReader(buf.Bytes())
Expand Down
7 changes: 7 additions & 0 deletions sigv4/sigv4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,11 @@ func TestSigV4RoundTripper(t *testing.T) {

require.Equal(t, "/test/test", gotReq.URL.Path)
})

t.Run("No body", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "https://example.com/test/test", nil)
require.NoError(t, err)
_, err = cli.Do(req)
require.NoError(t, err)
})
}

0 comments on commit 1dade5b

Please sign in to comment.