github.com/gospider007/requests@v0.0.0-20240506025355-c73d46169a23/test/middleware/try_test.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/gospider007/requests"
     9  )
    10  
    11  func TestMaxRetries(t *testing.T) {
    12  	n := 0
    13  	resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
    14  		MaxRetries: 3,
    15  		ResultCallBack: func(ctx context.Context, option *requests.RequestOption, response *requests.Response) error {
    16  
    17  			if n == 0 {
    18  				n++
    19  				return errors.New("try")
    20  			}
    21  			return nil
    22  		},
    23  	})
    24  	if err != nil {
    25  		t.Error(err)
    26  	}
    27  	if resp.StatusCode() != 200 {
    28  		t.Error("resp.StatusCode!= 200")
    29  	}
    30  	if n != 1 {
    31  		t.Error("n!=1")
    32  	}
    33  }