From 019f932fef45b8280f1529604d3648b7a9a1d2ad Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Tue, 7 Nov 2023 00:13:50 +0300 Subject: [PATCH 1/2] check if the body in options is nil before reading --- modules/http-helper/http_helper.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/http-helper/http_helper.go b/modules/http-helper/http_helper.go index 06efc2ce1..802fa78a4 100644 --- a/modules/http-helper/http_helper.go +++ b/modules/http-helper/http_helper.go @@ -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 From d0d0869ee0ab573c108c25d6450ea4cdbf06e963 Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Sat, 11 Nov 2023 18:08:37 +0300 Subject: [PATCH 2/2] unit test for http request with empty request body --- modules/http-helper/http_helper_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/http-helper/http_helper_test.go b/modules/http-helper/http_helper_test.go index 8fbc2d2c4..5dfc5af04 100644 --- a/modules/http-helper/http_helper_test.go +++ b/modules/http-helper/http_helper_test.go @@ -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)