diff options
author | Tomás Senart <tsenart@gmail.com> | 2020-03-25 17:55:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-25 17:55:13 +0100 |
commit | 75c6008129d016c9a5787290187b47936f63ac73 (patch) | |
tree | 1a4a6fa07081604920fd4bc6c33def65d5df1804 /client_test.go | |
parent | Make the prefork mode more robust (#755) (diff) | |
download | fasthttp-75c6008129d016c9a5787290187b47936f63ac73.tar.gz fasthttp-75c6008129d016c9a5787290187b47936f63ac73.tar.bz2 fasthttp-75c6008129d016c9a5787290187b47936f63ac73.zip |
client: Implement DoRedirects (#765)
This commit adds a `DoRedirects` method to both `HostClient` and
`Client` as well as top level convenience function of the same name that
is called with the package level `defaultClient`.
Re-implementing this redirect logic in user code is harder than
necessary.
Diffstat (limited to 'client_test.go')
-rw-r--r-- | client_test.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/client_test.go b/client_test.go index 841f1ee..b76bf9c 100644 --- a/client_test.go +++ b/client_test.go @@ -1227,6 +1227,42 @@ func TestClientFollowRedirects(t *testing.T) { t.Fatalf("unexpected response %q. Expecting %q", body, "/aaab/sss") } } + + for i := 0; i < 10; i++ { + req := AcquireRequest() + resp := AcquireResponse() + + req.SetRequestURI("http://xxx/foo") + + err := c.DoRedirects(req, resp, 16) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + if statusCode := resp.StatusCode(); statusCode != StatusOK { + t.Fatalf("unexpected status code: %d", statusCode) + } + + if body := string(resp.Body()); body != "/bar" { + t.Fatalf("unexpected response %q. Expecting %q", body, "/bar") + } + + ReleaseRequest(req) + ReleaseResponse(resp) + } + + req := AcquireRequest() + resp := AcquireResponse() + + req.SetRequestURI("http://xxx/foo") + + err := c.DoRedirects(req, resp, 0) + if have, want := err, ErrTooManyRedirects; have != want { + t.Fatalf("want error: %v, have %v", want, have) + } + + ReleaseRequest(req) + ReleaseResponse(resp) } func TestClientGetTimeoutSuccess(t *testing.T) { |