aboutsummaryrefslogtreecommitdiff
path: root/client_test.go
diff options
context:
space:
mode:
authorGravatar Vural Kap <vuralkap@gmail.com> 2020-05-27 20:40:04 +0300
committerGravatar Vural Kap <vuralkap@gmail.com> 2020-05-27 20:40:04 +0300
commit77dcda39ae0e758bacef6f3fb5c7a9cafa239279 (patch)
tree4e8cb244a837ed9ff5fa377771b6223d5e4e4828 /client_test.go
parentAdd timeouts to tls Handshake (diff)
downloadfasthttp-77dcda39ae0e758bacef6f3fb5c7a9cafa239279.tar.gz
fasthttp-77dcda39ae0e758bacef6f3fb5c7a9cafa239279.tar.bz2
fasthttp-77dcda39ae0e758bacef6f3fb5c7a9cafa239279.zip
Add custom retry decider for fail http requests
Diffstat (limited to 'client_test.go')
-rw-r--r--client_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/client_test.go b/client_test.go
index f5c9de9..63c7f7e 100644
--- a/client_test.go
+++ b/client_test.go
@@ -1648,6 +1648,58 @@ func TestClientIdempotentRequest(t *testing.T) {
}
}
+func TestClientRetryRequestWithCustomDecider(t *testing.T) {
+ t.Parallel()
+
+ dialsCount := 0
+ c := &Client{
+ Dial: func(addr string) (net.Conn, error) {
+ dialsCount++
+ switch dialsCount {
+ case 1:
+ return &singleReadConn{
+ s: "invalid response",
+ }, nil
+ case 2:
+ return &writeErrorConn{}, nil
+ case 3:
+ return &readErrorConn{}, nil
+ case 4:
+ return &singleReadConn{
+ s: "HTTP/1.1 345 OK\r\nContent-Type: foobar\r\nContent-Length: 7\r\n\r\n0123456",
+ }, nil
+ default:
+ t.Fatalf("unexpected number of dials: %d", dialsCount)
+ }
+ panic("unreachable")
+ },
+ RetryIf: func(req *Request) bool {
+ return req.URI().String() == "http://foobar/a/b"
+ },
+ }
+
+ var args Args
+
+ // Post must succeed for http://foobar/a/b uri.
+ statusCode, body, err := c.Post(nil, "http://foobar/a/b", &args)
+ if err != nil {
+ t.Fatalf("unexpected error: %s", err)
+ }
+ if statusCode != 345 {
+ t.Fatalf("unexpected status code: %d. Expecting 345", statusCode)
+ }
+ if string(body) != "0123456" {
+ t.Fatalf("unexpected body: %q. Expecting %q", body, "0123456")
+ }
+
+ // POST must fail for http://foobar/a/b/c uri.
+ dialsCount = 0
+ _, _, err = c.Post(nil, "http://foobar/a/b/c", &args)
+ if err == nil {
+ t.Fatalf("expecting error")
+ }
+}
+
type writeErrorConn struct {
net.Conn
}