Skip to content

Commit

Permalink
fix time out
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuzhenfeng-finogeeks committed Oct 30, 2018
1 parent 87ba938 commit c140109
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 49 deletions.
16 changes: 0 additions & 16 deletions example/main.go

This file was deleted.

18 changes: 9 additions & 9 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fetch

import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
Expand All @@ -26,18 +27,17 @@ func Fetch(url string, op Options) (Resp, error) {
defaultOp.Timeout = op.Timeout
}

fmt.Println(defaultOp)

// create a new http client
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: defaultOp.Timeout,
KeepAlive: defaultOp.Timeout,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: defaultOp.Timeout,
TLSHandshakeTimeout: defaultOp.Timeout,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
Timeout: defaultOp.Timeout,
Expand Down
35 changes: 12 additions & 23 deletions fetch_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
package fetch

import (
"reflect"
"fmt"
"testing"
)

func TestFetch(t *testing.T) {
type args struct {
url string
op Options
}
tests := []struct {
name string
args args
want Resp
wantErr bool
}{
// TODO: Add test cases.
url := "https://www.baidu.com"

options := Options{
Method: "GET",
// Header: headers,
// Timeout: 1 * time.Second,
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Fetch(tt.args.url, tt.args.op)
if (err != nil) != tt.wantErr {
t.Errorf("Fetch() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Fetch() = %v, want %v", got, tt.want)
}
})
resp, err := Fetch(url, options)
if err != nil {
fmt.Println("error")
fmt.Println(err)
}
fmt.Println(resp.ToString())
}
2 changes: 1 addition & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func NewDefaultOptions() Options {
"Accept": "*/*",
},
Body: nil,
Timeout: 20 * time.Millisecond,
Timeout: 20 * time.Second,
}
}

0 comments on commit c140109

Please sign in to comment.