Skip to content

Commit

Permalink
Merge 16835b9 into 38c802d
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomvsouza committed Aug 2, 2018
2 parents 38c802d + 16835b9 commit 65440ac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client.go
Expand Up @@ -135,6 +135,9 @@ func (c *client) do(method, url string, responseModel interface{}, requestBody [

req.Header.Set("Accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
if method == http.MethodPost || method == http.MethodPut {
req.Header.Set("Content-Type", "application/json")
}

res, err := c.client.Do(req)
if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions client_test.go
Expand Up @@ -209,6 +209,31 @@ func TestClient_POST(t *testing.T) {
Foo string `json:"foo"`
}{}, response)
})

t.Run("regression test existence of request header content-type = application/json", func(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPost, fmt.Sprintf("%s%s", apiEndpoint, "/foo"),
func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Header.Get("Content-Type"), "application/json")
res := httpmock.NewStringResponse(http.StatusOK, `{"bar":"foo"}`)
res.Header.Add("X-Rate-Limit", "36/200")
return res, nil
},
)

response := struct {
Foo string `json:"foo"`
}{}

c := NewClient("")
err := c.(*client).POST("/foo", &response, []byte(`{"bar":"foo"}`))
assert.NoError(t, err)
assert.Equal(t, struct {
Foo string `json:"foo"`
}{}, response)
})
}

func TestClient_PUT(t *testing.T) {
Expand Down Expand Up @@ -311,4 +336,29 @@ func TestClient_PUT(t *testing.T) {
Foo string `json:"foo"`
}{}, response)
})

t.Run("regression test existence of request header content-type = application/json", func(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodPut, fmt.Sprintf("%s%s", apiEndpoint, "/foo"),
func(req *http.Request) (*http.Response, error) {
assert.Equal(t, req.Header.Get("Content-Type"), "application/json")
res := httpmock.NewStringResponse(http.StatusOK, `{"bar":"foo"}`)
res.Header.Add("X-Rate-Limit", "36/200")
return res, nil
},
)

response := struct {
Foo string `json:"foo"`
}{}

c := NewClient("")
err := c.(*client).PUT("/foo", &response, []byte(`{"bar":"foo"}`))
assert.NoError(t, err)
assert.Equal(t, struct {
Foo string `json:"foo"`
}{}, response)
})
}

0 comments on commit 65440ac

Please sign in to comment.