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

metrics/librato: ensure resp.body closed #26969

Merged
merged 16 commits into from
Mar 27, 2023
Merged
4 changes: 4 additions & 0 deletions graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func TestGraphQLBlockSerialization(t *testing.T) {
if err != nil {
t.Fatalf("could not post: %v", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("could not read from response body: %v", err)
Expand Down Expand Up @@ -238,6 +239,7 @@ func TestGraphQLBlockSerializationEIP2718(t *testing.T) {
if err != nil {
t.Fatalf("could not post: %v", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("could not read from response body: %v", err)
Expand All @@ -263,6 +265,8 @@ func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) {
if err != nil {
t.Fatalf("could not post: %v", err)
}
defer resp.Body.Close()
io.Copy(io.Discard, resp.Body)
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
// make sure the request is not handled successfully
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}
Expand Down
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 (c *LibratoClient) PostMetrics(batch Batch) (err error) {
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(c.Email, c.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 @@ -615,6 +615,7 @@ func doHTTPRequest(t *testing.T, req *http.Request) *http.Response {
if err != nil {
t.Fatalf("could not issue a GET request to the given endpoint: %v", err)
}
t.Cleanup(func() { resp.Body.Close() })
return resp
}

Expand Down
1 change: 1 addition & 0 deletions node/rpcstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func baseRpcRequest(t *testing.T, url, bodyStr string, extraHeaders ...string) *
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { resp.Body.Close() })
return resp
}

Expand Down
4 changes: 3 additions & 1 deletion p2p/simulations/adapters/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,11 @@ func execP2PNode() {

// Send status to the host.
statusJSON, _ := json.Marshal(status)
if _, err := http.Post(statusURL, "application/json", bytes.NewReader(statusJSON)); err != nil {
resp, err := http.Post(statusURL, "application/json", bytes.NewReader(statusJSON))
if err != nil {
log.Crit("Can't post startup info", "url", statusURL, "err", err)
}
resp.Body.Close()
if stackErr != nil {
os.Exit(1)
}
Expand Down
5 changes: 4 additions & 1 deletion p2p/simulations/mocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func TestMocker(t *testing.T) {
if err != nil {
t.Fatalf("Could not start mocker: %s", err)
}
defer resp.Body.Close()
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
if resp.StatusCode != 200 {
t.Fatalf("Invalid Status Code received for starting mocker, expected 200, got %d", resp.StatusCode)
}
Expand All @@ -145,15 +146,17 @@ func TestMocker(t *testing.T) {
if err != nil {
t.Fatalf("Could not stop mocker: %s", err)
}
defer resp.Body.Close()
jsvisa marked this conversation as resolved.
Show resolved Hide resolved
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)
}
defer resp.Body.Close()
jsvisa marked this conversation as resolved.
Show resolved Hide resolved

//now the number of nodes in the network should be zero
nodesInfo, 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 @@ -94,6 +94,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