From 16835b9a9a9585da115056a9daba542c098595d2 Mon Sep 17 00:00:00 2001 From: Bruno M V Souza Date: Thu, 2 Aug 2018 11:57:24 +0200 Subject: [PATCH] Fix transaction creation --- client.go | 3 +++ client_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/client.go b/client.go index 2202e55..0cbbd21 100644 --- a/client.go +++ b/client.go @@ -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 { diff --git a/client_test.go b/client_test.go index 8b6ae90..35829c1 100644 --- a/client_test.go +++ b/client_test.go @@ -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) { @@ -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) + }) }