加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/gocolly/colly
克隆/下载
http_trace_test.go 1.74 KB
一键复制 编辑 原始数据 按行查看 历史
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)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化