Skip to content

Commit

Permalink
all: ensure resp.body closed (ethereum#26969)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan authored and JukLee0ira committed Dec 16, 2024
1 parent 94f7994 commit a978d39
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion metrics/librato/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ func (lc *LibratoClient) PostMetrics(batch Batch) (err error) {
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(lc.Email, lc.Token)

if resp, err = http.DefaultClient.Do(req); err != nil {
resp, err = http.DefaultClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
var body []byte
Expand Down
1 change: 1 addition & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,5 +651,6 @@ func doHTTPRequest(t *testing.T, req *http.Request) *http.Response {
if err != nil {
t.Error("could not issue a GET request to the given endpoint", err)
}
t.Cleanup(func() { resp.Body.Close() })
return resp
}
5 changes: 4 additions & 1 deletion p2p/simulations/mocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func TestMocker(t *testing.T) {
if err != nil {
t.Fatalf("Could not start mocker: %s", err)
}
resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("Invalid Status Code received for starting mocker, expected 200, got %d", resp.StatusCode)
}
Expand All @@ -148,15 +149,17 @@ func TestMocker(t *testing.T) {
if err != nil {
t.Fatalf("Could not stop mocker: %s", err)
}
resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("Invalid Status Code received for stopping mocker, expected 200, got %d", resp.StatusCode)
}

//reset the network
_, err = http.Post(s.URL+"/reset", "", nil)
resp, err = http.Post(s.URL+"/reset", "", nil)
if err != nil {
t.Fatalf("Could not reset network: %s", err)
}
resp.Body.Close()

//now the number of nodes in the network should be zero
nodes_info, err = client.GetNodes()
Expand Down
1 change: 1 addition & 0 deletions rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body
if err != nil {
t.Fatalf("request failed: %v", err)
}
resp.Body.Close()
confirmStatusCode(t, resp.StatusCode, expectedStatusCode)
}

Expand Down

0 comments on commit a978d39

Please sign in to comment.