-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
http_trace_test.go
73 lines (60 loc) · 1.74 KB
/
http_trace_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 colly
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
const testDelay = 200 * time.Millisecond
func newTraceTestServer(delay time.Duration) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(delay)
w.WriteHeader(200)
})
mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(delay)
w.WriteHeader(500)
})
return httptest.NewServer(mux)
}
func TestTraceWithNoDelay(t *testing.T) {
ts := newTraceTestServer(0)
defer ts.Close()
client := ts.Client()
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Errorf("Failed to construct request %v", err)
}
trace := &HTTPTrace{}
req = trace.WithTrace(req)
if _, err = client.Do(req); err != nil {
t.Errorf("Failed to make request %v", err)
}
if trace.ConnectDuration > testDelay {
t.Errorf("trace ConnectDuration should be (almost) 0, got %v", trace.ConnectDuration)
}
if trace.FirstByteDuration > testDelay {
t.Errorf("trace FirstByteDuration should be (almost) 0, got %v", trace.FirstByteDuration)
}
}
func TestTraceWithDelay(t *testing.T) {
ts := newTraceTestServer(testDelay)
defer ts.Close()
client := ts.Client()
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Errorf("Failed to construct request %v", err)
}
trace := &HTTPTrace{}
req = trace.WithTrace(req)
if _, err = client.Do(req); err != nil {
t.Errorf("Failed to make request %v", err)
}
if trace.ConnectDuration > testDelay {
t.Errorf("trace ConnectDuration should be (almost) 0, got %v", trace.ConnectDuration)
}
if trace.FirstByteDuration < testDelay {
t.Errorf("trace FirstByteDuration should be at least 200ms, got %v", trace.FirstByteDuration)
}
}