Skip to content

Commit

Permalink
feat: add redirect history feature #295
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Dec 31, 2024
1 parent fdf601a commit ffb4383
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
11 changes: 9 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,23 @@ func TestClientRedirectPolicy(t *testing.T) {
defer ts.Close()

c := dcnl().SetRedirectPolicy(FlexibleRedirectPolicy(20), DomainCheckRedirectPolicy("127.0.0.1"))
_, err := c.R().
res, err := c.R().
SetHeader("Name1", "Value1").
SetHeader("Name2", "Value2").
SetHeader("Name3", "Value3").
Get(ts.URL + "/redirect-1")

assertEqual(t, true, err.Error() == "Get \"/redirect-21\": resty: stopped after 20 redirects")

redirects := res.RedirectHistory()
assertEqual(t, 20, len(redirects))

finalReq := redirects[0]
assertEqual(t, 307, finalReq.StatusCode)
assertEqual(t, ts.URL+"/redirect-20", finalReq.URL)

c.SetRedirectPolicy(NoRedirectPolicy())
res, err := c.R().Get(ts.URL + "/redirect-1")
res, err = c.R().Get(ts.URL + "/redirect-1")
assertNil(t, err)
assertEqual(t, http.StatusTemporaryRedirect, res.StatusCode())
assertEqual(t, `<a href="/redirect-2">Temporary Redirect</a>.`, res.String())
Expand Down
6 changes: 6 additions & 0 deletions redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ type (
// functions as [RedirectPolicy]. If `f` is a function with the appropriate
// signature, RedirectPolicyFunc(f) is a RedirectPolicy object that calls `f`.
RedirectPolicyFunc func(*http.Request, []*http.Request) error

// RedirectInfo struct is used to capture the URL and status code for the redirect history
RedirectInfo struct {
URL string
StatusCode int
}
)

// Apply calls f(req, via).
Expand Down
11 changes: 10 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,13 @@ func TestHTTPAutoRedirectUpTo10(t *testing.T) {
ts := createRedirectServer(t)
defer ts.Close()

_, err := dcnl().R().Get(ts.URL + "/redirect-1")
res, err := dcnl().R().Get(ts.URL + "/redirect-1")
redirects := res.RedirectHistory()
assertEqual(t, 10, len(redirects))

finalReq := redirects[0]
assertEqual(t, 307, finalReq.StatusCode)
assertEqual(t, ts.URL+"/redirect-10", finalReq.URL)

assertEqual(t, true, (err.Error() == "Get /redirect-11: stopped after 10 redirects" ||
err.Error() == "Get \"/redirect-11\": stopped after 10 redirects"))
Expand Down Expand Up @@ -2338,6 +2344,9 @@ func TestRequestSettingsCoverage(t *testing.T) {
result := jsonIndent(invalidJsonBytes)
assertEqual(t, string(invalidJsonBytes), string(result))

res := &Response{}
assertNil(t, res.RedirectHistory())

defer func() {
if rec := recover(); rec != nil {
if err, ok := rec.(error); ok {
Expand Down
20 changes: 20 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ func (r *Response) IsError() bool {
return r.StatusCode() > 399
}

// RedirectHistory method returns a redirect history slice with the URL and status code
func (r *Response) RedirectHistory() []*RedirectInfo {
if r.RawResponse == nil {
return nil
}

redirects := make([]*RedirectInfo, 0)
res := r.RawResponse
for res != nil {
req := res.Request
redirects = append(redirects, &RedirectInfo{
StatusCode: res.StatusCode,
URL: req.URL.String(),
})
res = req.Response
}

return redirects
}

func (r *Response) setReceivedAt() {
r.receivedAt = time.Now()
if r.Request.trace != nil {
Expand Down

0 comments on commit ffb4383

Please sign in to comment.