Skip to content

Commit

Permalink
feat: add trace and connect direct methods; even though it is possibl…
Browse files Browse the repository at this point in the history
…e to do with Execute method directly
  • Loading branch information
jeevatkm committed Sep 23, 2024
1 parent 73328dd commit bc3024b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 34 deletions.
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const (

// MethodOptions HTTP method
MethodOptions = "OPTIONS"

// MethodTrace HTTP method
MethodTrace = "TRACE"

// MethodConnect HTTP method
MethodConnect = "CONNECT"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 13 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 42 additions & 28 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand All @@ -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()
Expand Down Expand Up @@ -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().
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1784,7 +1798,7 @@ type noCtTest struct {
}

func TestRequestExpectContentTypeTest(t *testing.T) {
ts := createGenServer(t)
ts := createGenericServer(t)
defer ts.Close()

c := dc()
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 11 additions & 1 deletion resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bc3024b

Please sign in to comment.