Skip to content

Commit 117530b

Browse files
authored
metrics/librato: ensure resp.body closed (#26969)
This change ensures that we call Close on a http response body, in various places in the source code (mostly tests)
1 parent 41f89ca commit 117530b

File tree

7 files changed

+16
-3
lines changed

7 files changed

+16
-3
lines changed

graphql/graphql_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func TestGraphQLBlockSerialization(t *testing.T) {
156156
t.Fatalf("could not post: %v", err)
157157
}
158158
bodyBytes, err := io.ReadAll(resp.Body)
159+
resp.Body.Close()
159160
if err != nil {
160161
t.Fatalf("could not read from response body: %v", err)
161162
}
@@ -239,6 +240,7 @@ func TestGraphQLBlockSerializationEIP2718(t *testing.T) {
239240
t.Fatalf("could not post: %v", err)
240241
}
241242
bodyBytes, err := io.ReadAll(resp.Body)
243+
resp.Body.Close()
242244
if err != nil {
243245
t.Fatalf("could not read from response body: %v", err)
244246
}
@@ -263,6 +265,7 @@ func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) {
263265
if err != nil {
264266
t.Fatalf("could not post: %v", err)
265267
}
268+
resp.Body.Close()
266269
// make sure the request is not handled successfully
267270
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
268271
}

metrics/librato/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ func (c *LibratoClient) PostMetrics(batch Batch) (err error) {
8787
req.Header.Set("Content-Type", "application/json")
8888
req.SetBasicAuth(c.Email, c.Token)
8989

90-
if resp, err = http.DefaultClient.Do(req); err != nil {
90+
resp, err = http.DefaultClient.Do(req)
91+
if err != nil {
9192
return
9293
}
94+
defer resp.Body.Close()
9395

9496
if resp.StatusCode != http.StatusOK {
9597
var body []byte

node/node_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ func doHTTPRequest(t *testing.T, req *http.Request) *http.Response {
615615
if err != nil {
616616
t.Fatalf("could not issue a GET request to the given endpoint: %v", err)
617617
}
618+
t.Cleanup(func() { resp.Body.Close() })
618619
return resp
619620
}
620621

node/rpcstack_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func baseRpcRequest(t *testing.T, url, bodyStr string, extraHeaders ...string) *
320320
if err != nil {
321321
t.Fatal(err)
322322
}
323+
t.Cleanup(func() { resp.Body.Close() })
323324
return resp
324325
}
325326

p2p/simulations/adapters/exec.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,11 @@ func execP2PNode() {
428428

429429
// Send status to the host.
430430
statusJSON, _ := json.Marshal(status)
431-
if _, err := http.Post(statusURL, "application/json", bytes.NewReader(statusJSON)); err != nil {
431+
resp, err := http.Post(statusURL, "application/json", bytes.NewReader(statusJSON))
432+
if err != nil {
432433
log.Crit("Can't post startup info", "url", statusURL, "err", err)
433434
}
435+
resp.Body.Close()
434436
if stackErr != nil {
435437
os.Exit(1)
436438
}

p2p/simulations/mocker_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func TestMocker(t *testing.T) {
124124
if err != nil {
125125
t.Fatalf("Could not start mocker: %s", err)
126126
}
127+
resp.Body.Close()
127128
if resp.StatusCode != 200 {
128129
t.Fatalf("Invalid Status Code received for starting mocker, expected 200, got %d", resp.StatusCode)
129130
}
@@ -145,15 +146,17 @@ func TestMocker(t *testing.T) {
145146
if err != nil {
146147
t.Fatalf("Could not stop mocker: %s", err)
147148
}
149+
resp.Body.Close()
148150
if resp.StatusCode != 200 {
149151
t.Fatalf("Invalid Status Code received for stopping mocker, expected 200, got %d", resp.StatusCode)
150152
}
151153

152154
//reset the network
153-
_, err = http.Post(s.URL+"/reset", "", nil)
155+
resp, err = http.Post(s.URL+"/reset", "", nil)
154156
if err != nil {
155157
t.Fatalf("Could not reset network: %s", err)
156158
}
159+
resp.Body.Close()
157160

158161
//now the number of nodes in the network should be zero
159162
nodesInfo, err = client.GetNodes()

rpc/http_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body
9494
if err != nil {
9595
t.Fatalf("request failed: %v", err)
9696
}
97+
resp.Body.Close()
9798
confirmStatusCode(t, resp.StatusCode, expectedStatusCode)
9899
}
99100

0 commit comments

Comments
 (0)