Skip to content

Commit

Permalink
Setup http body close linter
Browse files Browse the repository at this point in the history
Add linter to check that code closes the http body.
  • Loading branch information
miry committed Sep 16, 2021
1 parent 8ae7b96 commit f6fbc5f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
linters:
disable-all: true
enable:
- bodyclose
- lll
- misspell

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Add /v2 suffix to module import path (#311, @dnwe)
* Setup code linter (#314, @miry)
* Max line length is 100 characters (#316, @miry)
* Linter to check whether HTTP response body is closed successfully (#317, @miry)

# [2.1.5]

Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ fmt:
lint:
golangci-lint run

.PHONY: fmt
fmt:
goimports -w **/*.go
golangci-lint run --fix

.PHONY: e2e
e2e: build
bin/e2e
Expand Down
14 changes: 12 additions & 2 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func TestBrowserGets403(t *testing.T) {
"(KHTML, like Gecko) Chrome/33.0.1750.117 Mobile Safari/537.36 OPR/20.0.1396.72047",
)

resp, _ := client.Do(req)
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Does not expect errors from client: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != 403 {
t.Fatal("Browser-like UserAgent was not denied access to Toxiproxy")
Expand All @@ -62,7 +66,11 @@ func TestNonBrowserGets200(t *testing.T) {
req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil)
req.Header.Add("User-Agent", "Wget/2.1")

resp, _ := client.Do(req)
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Does not expect errors from client: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode == 403 {
t.Fatal("Non-Browser-like UserAgent was denied access to Toxiproxy")
Expand Down Expand Up @@ -147,6 +155,7 @@ func TestPopulateDefaultEnabled(t *testing.T) {
if err != nil {
t.Fatal("Failed to send POST to /populate:", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
message, _ := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -992,6 +1001,7 @@ func TestVersionEndpointReturnsVersion(t *testing.T) {
if err != nil {
t.Fatal("Failed to get index", err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down
19 changes: 7 additions & 12 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,18 @@ func (proxy *Proxy) Save() error {
return err
}

var resp *http.Response
path := proxy.client.endpoint + "/proxies"
contenttype := "application/json"
if proxy.created {
resp, err = http.Post(
proxy.client.endpoint+"/proxies/"+proxy.Name,
"text/plain",
bytes.NewReader(request),
)
} else {
resp, err = http.Post(
proxy.client.endpoint+"/proxies",
"application/json",
bytes.NewReader(request),
)
path += "/" + proxy.Name
contenttype = "text/plain"
}

resp, err := http.Post(path, contenttype, bytes.NewReader(request))
if err != nil {
return err
}
defer resp.Body.Close()

if proxy.created {
err = checkError(resp, http.StatusOK, "Save")
Expand Down

0 comments on commit f6fbc5f

Please sign in to comment.