-
Notifications
You must be signed in to change notification settings - Fork 0
/
httptrack_test.go
142 lines (115 loc) · 2.66 KB
/
httptrack_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"io"
"io/ioutil"
"net/http"
"testing"
"time"
)
const (
TestHTTP = "http://example.com"
TestHTTPS = "https://example.com"
)
func TestHTTPTrackHTTPS(t *testing.T) {
var result Track
req, err := http.NewRequest("GET", TestHTTPS, nil)
if err != nil {
t.Fatal(err)
}
ctx := WithHTTPTrack(req.Context(), &result)
req = req.WithContext(ctx)
client := http.DefaultClient
res, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
t.Fatal(err)
}
res.Body.Close()
if !result.isTLS {
t.Fatal("isTLS should be true")
}
for k, d := range result.durations() {
if d <= 0*time.Millisecond {
t.Fatalf("%s should be > 0", k)
}
}
}
func TestHTTPTrackHTTP(t *testing.T) {
var result Track
req, err := http.NewRequest("GET", TestHTTP, nil)
if err != nil {
t.Fatal(err)
}
ctx := WithHTTPTrack(req.Context(), &result)
req = req.WithContext(ctx)
client := http.DefaultClient
res, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
if _, err = io.Copy(ioutil.Discard, res.Body); err != nil {
t.Fatal(err)
}
res.Body.Close()
if result.isTLS {
t.Fatal("isTLS should be false")
}
if got, want := result.TLSHandshake, 0*time.Millisecond; got != want {
t.Fatalf("TLSHandshake time = %d, want %d", got, want)
}
// We are expecting 0 for TLSHandshake
// remove it from the durations for the next check
durations := result.durations()
delete(durations, "TLSHandshake")
for k, d := range durations {
if d <= 0*time.Millisecond {
t.Fatalf("%s should be > 0", k)
}
}
}
func TestHTTPTrackKeepAlive(t *testing.T) {
req1, err := http.NewRequest("GET", TestHTTPS, nil)
if err != nil {
t.Fatal(err)
}
client := http.DefaultClient
res1, err := client.Do(req1)
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(ioutil.Discard, res1.Body); err != nil {
t.Fatal(err)
}
res1.Body.Close()
// Make second request
// connection should be re-used
var result Track
req2, err := http.NewRequest("GET", TestHTTPS, nil)
if err != nil {
t.Fatal(err)
}
ctx := WithHTTPTrack(req2.Context(), &result)
req2 = req2.WithContext(ctx)
client = http.DefaultClient
res2, err := client.Do(req2)
if err != nil {
t.Fatal("FAIL: request - ", err)
}
if _, err := io.Copy(ioutil.Discard, res2.Body); err != nil {
t.Fatal("FAIL: copy body - ")
}
res2.Body.Close()
// DNSLookup, Connect, TLSHandshake should be 0
durations := []time.Duration{
result.DNSLookout,
result.Connect,
result.TLSHandshake,
}
for k, d := range durations {
if got, want := d, 0*time.Millisecond; got != want {
t.Fatalf("#%d=%d expected to be eq %d", k, got, want)
}
}
}