forked from projectdiscovery/rawhttp
/
client_test.go
50 lines (49 loc) · 1.6 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
package pkg
//import (
// "net/http"
// "net/http/httptest"
// "testing"
// "time"
//
// "github.com/julienschmidt/httprouter"
// "github.com/projectdiscovery/stringsutil"
//)
//
//func getTestHttpServer(timeout time.Duration) *httptest.Server {
// var ts *httptest.Server
// router := httprouter.New()
// router.GET("/rawhttp", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// time.Sleep(timeout)
// }))
// ts = httptest.NewServer(router)
// return ts
//}
//
//// run with go test -timeout 45s -run ^TestDialDefaultTimeout$ github.com/secoba/rawhttp
//func TestDialDefaultTimeout(t *testing.T) {
// timeout := 30 * time.Second
// ts := getTestHttpServer(45 * time.Second)
// defer ts.Close()
// startTime := time.Now()
// client := NewClient(DefaultOptions)
// c, _ := client.CreateConnection("", "", nil, nil)
// _, _, err := client.DoRaw(c, "GET", ts.URL, "/rawhttp", nil, nil)
// if !stringsutil.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
// c, _ := client.CreateConnection("", "", nil, nil)
// _, _, err := client.DoRawWithOptions(c, "GET", ts.URL, "/rawhttp", nil, nil, options)
// if !stringsutil.ContainsAny(err.Error(), "i/o timeout") || time.Now().Before(startTime.Add(timeout)) {
// t.Error("custom timeout error")
// }
//}