Skip to content

Commit

Permalink
Merge pull request #148 from mackerelio/fix_request_header
Browse files Browse the repository at this point in the history
Always add `Content-Type` header for non GET APIs
  • Loading branch information
yohfee authored Nov 14, 2023
2 parents 440c2b4 + fa3e844 commit 2b4e0de
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
14 changes: 7 additions & 7 deletions mackerel.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,27 @@ func (c *Client) Request(req *http.Request) (resp *http.Response, err error) {
}

func requestGet[T any](client *Client, path string) (*T, error) {
return requestNoBody[T](client, "GET", path, nil)
return requestNoBody[T](client, http.MethodGet, path, nil)
}

func requestGetWithParams[T any](client *Client, path string, params url.Values) (*T, error) {
return requestNoBody[T](client, "GET", path, params)
return requestNoBody[T](client, http.MethodGet, path, params)
}

func requestGetAndReturnHeader[T any](client *Client, path string) (*T, http.Header, error) {
return requestInternal[T](client, "GET", path, nil, nil)
return requestInternal[T](client, http.MethodGet, path, nil, nil)
}

func requestPost[T any](client *Client, path string, payload any) (*T, error) {
return requestJSON[T](client, "POST", path, payload)
return requestJSON[T](client, http.MethodPost, path, payload)
}

func requestPut[T any](client *Client, path string, payload any) (*T, error) {
return requestJSON[T](client, "PUT", path, payload)
return requestJSON[T](client, http.MethodPut, path, payload)
}

func requestDelete[T any](client *Client, path string) (*T, error) {
return requestNoBody[T](client, "DELETE", path, nil)
return requestNoBody[T](client, http.MethodDelete, path, nil)
}

func requestJSON[T any](client *Client, method, path string, payload any) (*T, error) {
Expand All @@ -174,7 +174,7 @@ func requestInternal[T any](client *Client, method, path string, params url.Valu
if err != nil {
return nil, nil, err
}
if body != nil {
if body != nil || method != http.MethodGet {
req.Header.Add("Content-Type", "application/json")
}

Expand Down
43 changes: 43 additions & 0 deletions mackerel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,49 @@ func TestRequest(t *testing.T) {
}
}

func Test_requestInternal(t *testing.T) {
tests := []struct {
method string
body io.Reader
hasContentTypeHeader bool
}{
{http.MethodGet, nil, false},
{http.MethodPost, nil, true},
{http.MethodPut, nil, true},
{http.MethodDelete, nil, true},
{http.MethodGet, strings.NewReader("some"), true},
{http.MethodPost, strings.NewReader("some"), true},
{http.MethodPut, strings.NewReader("some"), true},
{http.MethodDelete, strings.NewReader("some"), true},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s with %v body", test.method, test.body), func(tt *testing.T) {
// Test server that make requests consistent with Mackerel behavior
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if !test.hasContentTypeHeader && req.Header.Get("Content-Type") == "application/json" {
t.Error("Content-Type header should not have application/json")
}
if test.hasContentTypeHeader && req.Header.Get("Content-Type") != "application/json" {
t.Error("Content-Type header should have application/json")
}
res.Write([]byte(`{"success": true}`)) // nolint
}))
defer ts.Close()

client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
res, _, err := requestInternal[struct {
Success bool `json:"success"`
}](client, test.method, "/", url.Values{}, test.body)
if err != nil {
t.Errorf("request is error %v", err)
}
if !res.Success {
t.Errorf("response is invalid %v", res)
}
})
}
}

func TestUrlFor(t *testing.T) {
client, _ := NewClientWithOptions("dummy-key", "https://example.com/with/ignored/path", false)
expected := "https://example.com/some/super/endpoint"
Expand Down

0 comments on commit 2b4e0de

Please sign in to comment.