Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
language: go | ||
go: | ||
- 1.7.3 | ||
- tip | ||
|
||
os: | ||
- linux | ||
- osx | ||
|
||
sudo: false | ||
|
||
install: | ||
- echo "skipping travis' default" | ||
|
||
script: | ||
- go test -v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package httpstat | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
const ( | ||
TestDomainHTTP = "http://example.com" | ||
TestDomainHTTPS = "https://example.com" | ||
) | ||
|
||
func DefaultTransport() *http.Transport { | ||
// It comes from std transport.go | ||
return &http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: (&net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
KeepAlive: 30 * time.Second, | ||
}).DialContext, | ||
MaxIdleConns: 100, | ||
IdleConnTimeout: 90 * time.Second, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
ExpectContinueTimeout: 1 * time.Second, | ||
} | ||
} | ||
|
||
// To avoid shared transport | ||
func DefaultClient() *http.Client { | ||
return &http.Client{ | ||
Transport: DefaultTransport(), | ||
} | ||
} | ||
|
||
func NewRequest(t *testing.T, urlStr string, result *Result) *http.Request { | ||
req, err := http.NewRequest("GET", urlStr, nil) | ||
if err != nil { | ||
t.Fatal("NewRequest failed:", err) | ||
} | ||
|
||
ctx := WithHTTPStat(req.Context(), result) | ||
return req.WithContext(ctx) | ||
} | ||
|
||
func TestHTTPStat_HTTPS(t *testing.T) { | ||
var result Result | ||
req := NewRequest(t, TestDomainHTTPS, &result) | ||
|
||
client := DefaultClient() | ||
res, err := client.Do(req) | ||
if err != nil { | ||
t.Fatal("client.Do failed:", err) | ||
} | ||
|
||
if _, err := io.Copy(ioutil.Discard, res.Body); err != nil { | ||
t.Fatal("io.Copy failed:", err) | ||
} | ||
res.Body.Close() | ||
end := time.Now() | ||
|
||
for k, d := range result.durations(end) { | ||
if d <= 0*time.Millisecond { | ||
t.Fatalf("expect %s to be non-zero", k) | ||
} | ||
} | ||
} | ||
|
||
func TestHTTPStat_HTTP(t *testing.T) { | ||
var result Result | ||
req := NewRequest(t, TestDomainHTTP, &result) | ||
|
||
client := DefaultClient() | ||
res, err := client.Do(req) | ||
if err != nil { | ||
t.Fatal("client.Do failed:", err) | ||
} | ||
|
||
if _, err := io.Copy(ioutil.Discard, res.Body); err != nil { | ||
t.Fatal("io.Copy failed:", err) | ||
} | ||
res.Body.Close() | ||
end := time.Now() | ||
|
||
if got, want := result.TLSHandshake, 0*time.Millisecond; got != want { | ||
t.Fatalf("TLSHandshake time of HTTP = %d, want %d", got, want) | ||
} | ||
|
||
// Except TLS should be non zero | ||
durations := result.durations(end) | ||
delete(durations, "TLSHandshake") | ||
|
||
for k, d := range durations { | ||
if d <= 0*time.Millisecond { | ||
t.Fatalf("expect %s to be non-zero", k) | ||
} | ||
} | ||
} | ||
|
||
func TestHTTPStat_beforeGO17(t *testing.T) { | ||
var result Result | ||
req := NewRequest(t, TestDomainHTTPS, &result) | ||
|
||
// Before go1.7, it uses non context based Dial function. | ||
// It doesn't support httptrace. | ||
oldTransport := &http.Transport{ | ||
Dial: (&net.Dialer{ | ||
Timeout: 5 * time.Second, | ||
}).Dial, | ||
TLSHandshakeTimeout: 5 * time.Second, | ||
} | ||
|
||
client := &http.Client{ | ||
Timeout: time.Second * 10, | ||
Transport: oldTransport, | ||
} | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
t.Fatal("client.Do failed:", err) | ||
} | ||
|
||
if _, err := io.Copy(ioutil.Discard, res.Body); err != nil { | ||
t.Fatal("io.Copy failed:", err) | ||
} | ||
res.Body.Close() | ||
|
||
// The following values are not mesured. | ||
durations := []time.Duration{ | ||
result.DNSLookup, | ||
result.TCPConnection, | ||
result.TLSHandshake, | ||
} | ||
|
||
for i, d := range durations { | ||
if got, want := d, 0*time.Millisecond; got != want { | ||
t.Fatalf("#%d expect %d to be eq %d", i, got, want) | ||
} | ||
} | ||
} |