github.com/gospider007/requests@v0.0.0-20240506025355-c73d46169a23/test/middleware/errCallBack_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 TestErrCallBack(t *testing.T) {
    12  	n := 0
    13  	_, 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  			return errors.New("try")
    17  		},
    18  		ErrCallBack: func(ctx context.Context, option *requests.RequestOption, response *requests.Response, err error) error {
    19  			if n == 0 {
    20  				n++
    21  				return nil
    22  			}
    23  			return errors.New("test")
    24  		},
    25  	})
    26  	if err == nil {
    27  		t.Error("callback error")
    28  	}
    29  	if n != 1 {
    30  		t.Error("n!=1")
    31  	}
    32  }