Skip to content

Commit 7d7a7a0

Browse files
authored
Merge pull request #1370 from semihbkgr/fix-request-body-nil-pointer
Check for nil body in http_helper.HttpDoOptions before reading
2 parents bfd6c0a + d0d0869 commit 7d7a7a0

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

modules/http-helper/http_helper.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,15 @@ func HTTPDoWithRetryWithOptionsE(
346346
t testing.TestingT, options HttpDoOptions, expectedStatus int,
347347
retries int, sleepBetweenRetries time.Duration,
348348
) (string, error) {
349-
// The request body is closed after a request is complete.
350-
// Extract the underlying data and cache it so we can reuse for retried requests
351-
data, err := io.ReadAll(options.Body)
352-
if err != nil {
353-
return "", err
349+
var data []byte
350+
if options.Body != nil {
351+
// The request body is closed after a request is complete.
352+
// Read the underlying data and cache it, so we can reuse for retried requests.
353+
b, err := io.ReadAll(options.Body)
354+
if err != nil {
355+
return "", err
356+
}
357+
data = b
354358
}
355359

356360
options.Body = nil

modules/http-helper/http_helper_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ func TestErrorWithRetry(t *testing.T) {
139139
}
140140
}
141141

142+
func TestEmptyRequestBodyWithRetryWithOptions(t *testing.T) {
143+
t.Parallel()
144+
ts := getTestServerForFunction(bodyCopyHandler)
145+
defer ts.Close()
146+
147+
options := HttpDoOptions{
148+
Method: "GET",
149+
Url: ts.URL,
150+
Body: nil,
151+
}
152+
153+
response := HTTPDoWithRetryWithOptions(t, options, 200, 0, time.Second)
154+
require.Equal(t, "", response)
155+
}
156+
142157
func bodyCopyHandler(w http.ResponseWriter, r *http.Request) {
143158
w.WriteHeader(http.StatusOK)
144159
body, _ := io.ReadAll(r.Body)

0 commit comments

Comments
 (0)