-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_test.go
73 lines (66 loc) · 1.91 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package rawhttp
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func getTestHttpServer(timeout time.Duration) *httptest.Server {
var ts *httptest.Server
mux := http.NewServeMux()
mux.HandleFunc("/rawhttp", func(writer http.ResponseWriter, request *http.Request) {
time.Sleep(timeout)
})
ts = httptest.NewServer(mux)
return ts
}
// run with go test -timeout 45s -run ^TestDialDefaultTimeout$ github.com/projectdiscovery/rawhttp
func TestDialDefaultTimeout(t *testing.T) {
timeout := 30 * time.Second
ts := getTestHttpServer(45 * time.Second)
defer ts.Close()
startTime := time.Now()
client := NewClient(DefaultOptions)
_, err := client.DoRaw("GET", ts.URL, "/rawhttp", nil, nil)
if !strings.ContainsAny(err.Error(), "i/o timeout") || time.Now().Before(startTime.Add(timeout)) {
t.Error("default timeout error")
}
}
func TestDialWithCustomTimeout(t *testing.T) {
timeout := 5 * time.Second
ts := getTestHttpServer(10 * time.Second)
defer ts.Close()
startTime := time.Now()
client := NewClient(DefaultOptions)
options := DefaultOptions
options.Timeout = timeout
_, err := client.DoRawWithOptions("GET", ts.URL, "/rawhttp", nil, nil, options)
if !strings.ContainsAny(err.Error(), "i/o timeout") || time.Now().Before(startTime.Add(timeout)) {
t.Error("custom timeout error")
}
}
func TestSimpleRequest(t *testing.T) {
options := &Options{
Timeout: 0 * time.Second,
FollowRedirects: false,
MaxRedirects: 0,
AutomaticHostHeader: true,
AutomaticContentLength: true,
ForceReadAllBody: false,
}
client := NewClient(options)
req, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Printf("status: %d, body length: %d\n", resp.StatusCode, len(data))
}