Skip to content

Commit

Permalink
Merge pull request #1370 from semihbkgr/fix-request-body-nil-pointer
Browse files Browse the repository at this point in the history
Check for nil body in http_helper.HttpDoOptions before reading
  • Loading branch information
denis256 committed Nov 13, 2023
2 parents bfd6c0a + d0d0869 commit 7d7a7a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 9 additions & 5 deletions modules/http-helper/http_helper.go
Expand Up @@ -346,11 +346,15 @@ func HTTPDoWithRetryWithOptionsE(
t testing.TestingT, options HttpDoOptions, expectedStatus int,
retries int, sleepBetweenRetries time.Duration,
) (string, error) {
// The request body is closed after a request is complete.
// Extract the underlying data and cache it so we can reuse for retried requests
data, err := io.ReadAll(options.Body)
if err != nil {
return "", err
var data []byte
if options.Body != nil {
// The request body is closed after a request is complete.
// Read the underlying data and cache it, so we can reuse for retried requests.
b, err := io.ReadAll(options.Body)
if err != nil {
return "", err
}
data = b
}

options.Body = nil
Expand Down
15 changes: 15 additions & 0 deletions modules/http-helper/http_helper_test.go
Expand Up @@ -139,6 +139,21 @@ func TestErrorWithRetry(t *testing.T) {
}
}

func TestEmptyRequestBodyWithRetryWithOptions(t *testing.T) {
t.Parallel()
ts := getTestServerForFunction(bodyCopyHandler)
defer ts.Close()

options := HttpDoOptions{
Method: "GET",
Url: ts.URL,
Body: nil,
}

response := HTTPDoWithRetryWithOptions(t, options, 200, 0, time.Second)
require.Equal(t, "", response)
}

func bodyCopyHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
body, _ := io.ReadAll(r.Body)
Expand Down

0 comments on commit 7d7a7a0

Please sign in to comment.