Skip to content

Commit

Permalink
Defer close connection on each request (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniFrancisco authored Sep 13, 2019
1 parent 15529e5 commit a9ccedd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion vero/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"io/ioutil"
"net/http"

"github.com/uniplaces/vero-go"
"github.com/pkg/errors"
"github.com/uniplaces/vero-go"
)

const baseUrl = "https://api.getvero.com/api/v2/%v"
Expand Down Expand Up @@ -137,6 +137,7 @@ func (VeroClient) send(url string, data map[string]interface{}, method string) (
return []byte{}, err
}

request.Close = true
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/json")

Expand All @@ -145,6 +146,8 @@ func (VeroClient) send(url string, data map[string]interface{}, method string) (
return []byte{}, err
}

defer response.Body.Close()

body, err := ioutil.ReadAll(response.Body)
if err != nil {
return []byte{}, err
Expand Down
14 changes: 7 additions & 7 deletions vero/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ func TestTrack(t *testing.T) {
}

func assertEqual(t *testing.T, expected string, actual string) {
if expected != actual {
t.Errorf(`Expected: %v - Got: %v`, expected, actual)
}
if expected != actual {
t.Errorf(`Expected: %v - Got: %v`, expected, actual)
}
}

func configureHandler(
Expand All @@ -185,10 +185,10 @@ func configureHandler(
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
body := buf.String()
assertEqual(t, expectedRequestBody, body)
assertEqual(t, endpoint, r.URL.Path)
assertEqual(t, method, r.Method)

assertEqual(t, expectedRequestBody, body)
assertEqual(t, endpoint, r.URL.Path)
assertEqual(t, method, r.Method)

w.WriteHeader(http.StatusOK)
w.Write([]byte{})
Expand Down

0 comments on commit a9ccedd

Please sign in to comment.