From 85da9aec30c52fda225af78311419cc705985112 Mon Sep 17 00:00:00 2001 From: Arun Bhalla Date: Sat, 3 Feb 2018 15:53:36 -0800 Subject: [PATCH] Allow non-standard HTTP methods to support payloads --- request_test.go | 14 ++++++++++++++ resty_test.go | 8 ++++++++ util.go | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/request_test.go b/request_test.go index e4caf48f..d4f98e06 100644 --- a/request_test.go +++ b/request_test.go @@ -1246,3 +1246,17 @@ func TestGetPathParams(t *testing.T) { "userId": "sample@sample.com", }) } + +func TestReportMethodSupportsPayload(t *testing.T) { + ts := createGenServer(t) + defer ts.Close() + + c := dc() + resp, err := c.R(). + SetBody("body"). + Execute("REPORT", ts.URL + "/report") + + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + +} diff --git a/resty_test.go b/resty_test.go index 0372681a..9e3e3abf 100644 --- a/resty_test.go +++ b/resty_test.go @@ -421,6 +421,14 @@ func createGenServer(t *testing.T) *httptest.Server { w.WriteHeader(http.StatusOK) return } + + if r.Method == "REPORT" && r.URL.Path == "/report" { + body, _ := ioutil.ReadAll(r.Body) + if len(body) == 0 { + w.WriteHeader(http.StatusOK) + } + return + } }) return ts diff --git a/util.go b/util.go index 1aa3397d..c009f84f 100644 --- a/util.go +++ b/util.go @@ -157,7 +157,7 @@ func getPointer(v interface{}) interface{} { } func isPayloadSupported(m string, allowMethodGet bool) bool { - return (m == MethodPost || m == MethodPut || m == MethodDelete || m == MethodPatch || (allowMethodGet && m == MethodGet)) + return !(m == MethodHead || m == MethodOptions || (m == MethodGet && !allowMethodGet)) } func typeOf(i interface{}) reflect.Type {