diff --git a/client.go b/client.go index ef327d5..fcd0e1d 100644 --- a/client.go +++ b/client.go @@ -41,6 +41,12 @@ const ( // MethodOptions HTTP method MethodOptions = "OPTIONS" + + // MethodTrace HTTP method + MethodTrace = "TRACE" + + // MethodConnect HTTP method + MethodConnect = "CONNECT" ) var ( diff --git a/client_test.go b/client_test.go index 46ec163..90aa876 100644 --- a/client_test.go +++ b/client_test.go @@ -159,7 +159,7 @@ func TestClientDigestErrors(t *testing.T) { } func TestOnAfterMiddleware(t *testing.T) { - ts := createGenServer(t) + ts := createGenericServer(t) defer ts.Close() c := dc() @@ -638,7 +638,7 @@ func (rt *CustomRoundTripper) RoundTrip(_ *http.Request) (*http.Response, error) } func TestAutoGzip(t *testing.T) { - ts := createGenServer(t) + ts := createGenericServer(t) defer ts.Close() c := New() diff --git a/request.go b/request.go index 427fb1f..491dc97 100644 --- a/request.go +++ b/request.go @@ -1022,19 +1022,29 @@ func (r *Request) Put(url string) (*Response, error) { return r.Execute(MethodPut, url) } +// Patch method does PATCH HTTP request. It's defined in section 2 of RFC5789. +func (r *Request) Patch(url string) (*Response, error) { + return r.Execute(MethodPatch, url) +} + // Delete method does DELETE HTTP request. It's defined in section 4.3.5 of RFC7231. func (r *Request) Delete(url string) (*Response, error) { return r.Execute(MethodDelete, url) } +// Connect method does CONNECT HTTP request. It's defined in section 4.3.6 of RFC7231. +func (r *Request) Connect(url string) (*Response, error) { + return r.Execute(MethodConnect, url) +} + // Options method does OPTIONS HTTP request. It's defined in section 4.3.7 of RFC7231. func (r *Request) Options(url string) (*Response, error) { return r.Execute(MethodOptions, url) } -// Patch method does PATCH HTTP request. It's defined in section 2 of RFC5789. -func (r *Request) Patch(url string) (*Response, error) { - return r.Execute(MethodPatch, url) +// Trace method does TRACE HTTP request. It's defined in section 4.3.8 of RFC7231. +func (r *Request) Trace(url string) (*Response, error) { + return r.Execute(MethodTrace, url) } // Send method performs the HTTP request using the method and URL already defined diff --git a/request_test.go b/request_test.go index 2987de1..435943d 100644 --- a/request_test.go +++ b/request_test.go @@ -1164,7 +1164,7 @@ func TestGetWithCookies(t *testing.T) { } func TestPutPlainString(t *testing.T) { - ts := createGenServer(t) + ts := createGenericServer(t) defer ts.Close() resp, err := dc().R(). @@ -1177,7 +1177,7 @@ func TestPutPlainString(t *testing.T) { } func TestPutJSONString(t *testing.T) { - ts := createGenServer(t) + ts := createGenericServer(t) defer ts.Close() client := dc() @@ -1206,7 +1206,7 @@ func TestPutJSONString(t *testing.T) { } func TestPutXMLString(t *testing.T) { - ts := createGenServer(t) + ts := createGenericServer(t) defer ts.Close() resp, err := dc().R(). @@ -1220,7 +1220,7 @@ func TestPutXMLString(t *testing.T) { } func TestOnBeforeMiddleware(t *testing.T) { - ts := createGenServer(t) + ts := createGenericServer(t) defer ts.Close() c := dc() @@ -1266,41 +1266,55 @@ func TestHostCheckRedirectPolicy(t *testing.T) { assertEqual(t, true, strings.Contains(err.Error(), "redirect is not allowed as per DomainCheckRedirectPolicy")) } -func TestHeadMethod(t *testing.T) { - ts := createGetServer(t) +func TestHttpMethods(t *testing.T) { + ts := createGenericServer(t) defer ts.Close() - resp, err := dclr().Head(ts.URL + "/") + t.Run("head method", func(t *testing.T) { + resp, err := dclr().Head(ts.URL + "/") - assertError(t, err) - assertEqual(t, http.StatusOK, resp.StatusCode()) -} + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + }) -func TestOptionsMethod(t *testing.T) { - ts := createGenServer(t) - defer ts.Close() + t.Run("options method", func(t *testing.T) { + resp, err := dclr().Options(ts.URL + "/options") - resp, err := dclr().Options(ts.URL + "/options") + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + assertEqual(t, resp.Header().Get("Access-Control-Expose-Headers"), "x-go-resty-id") + }) - assertError(t, err) - assertEqual(t, http.StatusOK, resp.StatusCode()) - assertEqual(t, resp.Header().Get("Access-Control-Expose-Headers"), "x-go-resty-id") -} + t.Run("patch method", func(t *testing.T) { + resp, err := dclr().Patch(ts.URL + "/patch") -func TestPatchMethod(t *testing.T) { - ts := createGenServer(t) - defer ts.Close() + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) - resp, err := dclr().Patch(ts.URL + "/patch") + assertEqual(t, "", resp.String()) + }) - assertError(t, err) - assertEqual(t, http.StatusOK, resp.StatusCode()) + t.Run("trace method", func(t *testing.T) { + resp, err := dclr().Trace(ts.URL + "/trace") - assertEqual(t, "", resp.String()) + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + + assertEqual(t, "", resp.String()) + }) + + t.Run("connect method", func(t *testing.T) { + resp, err := dclr().Connect(ts.URL + "/connect") + + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + + assertEqual(t, "", resp.String()) + }) } func TestSendMethod(t *testing.T) { - ts := createGenServer(t) + ts := createGenericServer(t) defer ts.Close() t.Run("send-get", func(t *testing.T) { @@ -1784,7 +1798,7 @@ type noCtTest struct { } func TestRequestExpectContentTypeTest(t *testing.T) { - ts := createGenServer(t) + ts := createGenericServer(t) defer ts.Close() c := dc() @@ -1823,7 +1837,7 @@ func TestGetPathParamAndPathParams(t *testing.T) { } func TestReportMethodSupportsPayload(t *testing.T) { - ts := createGenServer(t) + ts := createGenericServer(t) defer ts.Close() c := dc() diff --git a/resty_test.go b/resty_test.go index 7b41499..3d8f62d 100644 --- a/resty_test.go +++ b/resty_test.go @@ -554,7 +554,7 @@ func createAuthServerTLSOptional(t *testing.T, useTLS bool) *httptest.Server { return httptest.NewServer(handler) } -func createGenServer(t *testing.T) *httptest.Server { +func createGenericServer(t *testing.T) *httptest.Server { ts := createTestServer(func(w http.ResponseWriter, r *http.Request) { t.Logf("Method: %v", r.Method) t.Logf("Path: %v", r.URL.Path) @@ -620,6 +620,16 @@ func createGenServer(t *testing.T) *httptest.Server { } return } + + if r.Method == MethodTrace && r.URL.Path == "/trace" { + w.WriteHeader(http.StatusOK) + return + } + + if r.Method == MethodConnect && r.URL.Path == "/connect" { + w.WriteHeader(http.StatusOK) + return + } }) return ts