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

Setup http body close linter #317

Merged
merged 1 commit into from
Sep 16, 2021
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
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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test:
fmt:
go fmt ./...
goimports -w **/*.go
golangci-lint run --fix

.PHONY: lint
lint:
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