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

Always add Content-Type header for non GET APIs #148

Merged
merged 5 commits into from
Nov 14, 2023
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
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
}))
Comment on lines +55 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a comment that this test server's behavior is similar to Mackerel's public API?

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
Loading