Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for nil body in http_helper.HttpDoOptions before reading #1370

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions modules/http-helper/http_helper.go
Original file line number Diff line number Diff line change
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be helpful to add a test to monitor null values in options.Body to avoid future regressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will be on it.

// 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
Original file line number Diff line number Diff line change
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