diff --git a/mackerel.go b/mackerel.go index 077ec3d..13b6b30 100644 --- a/mackerel.go +++ b/mackerel.go @@ -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) { @@ -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") } diff --git a/mackerel_test.go b/mackerel_test.go index ddae2d4..c26f43d 100644 --- a/mackerel_test.go +++ b/mackerel_test.go @@ -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"