Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add func TimeoutDetail #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions gorequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,22 @@ func (s *SuperAgent) Timeout(timeout time.Duration) *SuperAgent {
return s
}

// TimeoutDetail sets dial and read/write timeout respectively, making the request more detailed.
// request := gorequest.New().TimeoutDetail(2*time.Millisecond,3*time.Millisecond)
// resp, body, errs:= request.Get("http://example.com").End()
func (s *SuperAgent) TimeoutDetail(connTimeout time.Duration, respTimeout time.Duration) *SuperAgent {
s.Transport.Dial = func(network, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(network, addr, connTimeout)
if err != nil {
s.Errors = append(s.Errors, err)
return nil, err
}
conn.SetDeadline(time.Now().Add(respTimeout))
return conn, nil
}
return s
}

// Set TLSClientConfig for underling Transport.
// One example is you can use it to disable security check (https):
//
Expand Down
45 changes: 45 additions & 0 deletions gorequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,51 @@ func TestTimeoutFunc(t *testing.T) {

}

func TestTimeoutDetailFunc(t *testing.T) {
// 1st case, dial timeout
startTime := time.Now()
_, _, errs := New().TimeoutDetail(1000*time.Millisecond, 500*time.Millisecond).Get("http://www.google.com:81").End()
elapsedTime := time.Since(startTime)
if errs == nil {
t.Error("Expected dial timeout error but get nothing")
}
if elapsedTime < 1000*time.Millisecond || elapsedTime > 1500*time.Millisecond {
t.Errorf("Expected timeout in between 1000 -> 1500 ms | but got %d", elapsedTime)
}

// 2st case, read/write timeout (Can dial to url but want to timeout because too long operation on the server)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1100 * time.Millisecond) // slightly longer than expected
w.WriteHeader(200)
}))
request := New().TimeoutDetail(500*time.Millisecond, 1000*time.Millisecond)
startTime = time.Now()
_, _, errs = request.Get(ts.URL).End()
elapsedTime = time.Since(startTime)
if errs == nil {
t.Error("Expected dial+read/write timeout | but get nothing")
}
if elapsedTime < 1000*time.Millisecond || elapsedTime > 1500*time.Millisecond {
t.Errorf("Expected timeout in between 1000 -> 1500 ms | but got %d", elapsedTime)
}

// 3rd case, testing reuse of same request
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1100 * time.Millisecond) // slightly longer than expected
w.WriteHeader(200)
}))
startTime = time.Now()
_, _, errs = request.Get(ts.URL).End()
elapsedTime = time.Since(startTime)
if errs == nil {
t.Error("Expected dial+read/write timeout | but get nothing")
}
if elapsedTime < 1000*time.Millisecond || elapsedTime > 1500*time.Millisecond {
t.Errorf("Expected timeout in between 1000 -> 1500 ms | but got %d", elapsedTime)
}

}

func TestCookies(t *testing.T) {
request := New().Timeout(60 * time.Second)
_, _, errs := request.Get("https://github.com").End()
Expand Down