From dc8cfaa5b14cbbf6be140d6336dfef8cbac6c7c6 Mon Sep 17 00:00:00 2001 From: Dan Rising Date: Mon, 18 Nov 2019 17:26:19 -0800 Subject: [PATCH] Add Request.Send() method to execute Request as-is --- request.go | 10 +++++++++ request_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/request.go b/request.go index c5fb7d35..baf94d99 100644 --- a/request.go +++ b/request.go @@ -581,6 +581,16 @@ func (r *Request) Patch(url string) (*Response, error) { return r.Execute(MethodPatch, url) } +// Send method performs the HTTP request using the method and URL already defined +// for current `Request`. +// req := client.R() +// req.Method = resty.GET +// req.URL = "http://httpbin.org/get" +// resp, err := client.R().Send() +func (r *Request) Send() (*Response, error) { + return r.Execute(r.Method, r.URL) +} + // Execute method performs the HTTP request with given HTTP method and URL // for current `Request`. // resp, err := client.R().Execute(resty.GET, "http://httpbin.org/get") diff --git a/request_test.go b/request_test.go index 4bd8c51f..17a5fe9b 100644 --- a/request_test.go +++ b/request_test.go @@ -943,6 +943,64 @@ func TestPatchMethod(t *testing.T) { assertEqual(t, "", resp.String()) } +func TestSendMethod(t *testing.T) { + ts := createGenServer(t) + defer ts.Close() + + t.Run("send-get", func(t *testing.T) { + req := dclr() + req.Method = http.MethodGet + req.URL = ts.URL + "/gzip-test" + + resp, err := req.Send() + + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + + assertEqual(t, "This is Gzip response testing", resp.String()) + }) + + t.Run("send-options", func(t *testing.T) { + req := dclr() + req.Method = http.MethodOptions + req.URL = ts.URL + "/options" + + resp, err := req.Send() + + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + + assertEqual(t, "", resp.String()) + assertEqual(t, "x-go-resty-id", resp.Header().Get("Access-Control-Expose-Headers")) + }) + + t.Run("send-patch", func(t *testing.T) { + req := dclr() + req.Method = http.MethodPatch + req.URL = ts.URL + "/patch" + + resp, err := req.Send() + + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + + assertEqual(t, "", resp.String()) + }) + + t.Run("send-put", func(t *testing.T) { + req := dclr() + req.Method = http.MethodPut + req.URL = ts.URL + "/plaintext" + + resp, err := req.Send() + + assertError(t, err) + assertEqual(t, http.StatusOK, resp.StatusCode()) + + assertEqual(t, "TestPut: plain text response", resp.String()) + }) +} + func TestRawFileUploadByBody(t *testing.T) { ts := createFormPostServer(t) defer ts.Close()