本文整理汇总了Golang中github.com/taironas/route.Router.ServeHTTP方法的典型用法代码示例。如果您正苦于以下问题:Golang Router.ServeHTTP方法的具体用法?Golang Router.ServeHTTP怎么用?Golang Router.ServeHTTP使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/taironas/route.Router
的用法示例。
在下文中一共展示了Router.ServeHTTP方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestSpaceInvadersCache
func TestSpaceInvadersCache(t *testing.T) {
t.Parallel()
r := new(route.Router)
r.HandleFunc("/spaceinvaders/:key", SpaceInvaders)
var etag string
test := tgTesting.GenerateHandlerFunc(t, SpaceInvaders)
if recorder := test("/spaceinvaders/somekey", "GET", nil, r); recorder != nil {
if recorder.Code != http.StatusOK {
t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
}
etag = recorder.Header().Get("Etag")
}
// test caching
if req, err := http.NewRequest("GET", "/spaceinvaders/somekey", nil); err != nil {
t.Errorf("%v", err)
} else {
req.Header.Set("If-None-Match", etag)
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
if recorder.Code != http.StatusNotModified {
t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusNotModified)
}
}
}
示例2: TestSquares
func TestSquares(t *testing.T) {
t.Parallel()
r := new(route.Router)
r.HandleFunc("/squares/:key", Square)
test := tgTesting.GenerateHandlerFunc(t, Square)
for _, p := range tgTesting.GoodParams {
recorder := test("/squares/test", "GET", p, r)
if recorder.Code != http.StatusOK {
t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
}
}
for _, p := range tgTesting.BadParams {
recorder := test("/squares/test", "GET", p, r)
if recorder.Code != http.StatusOK {
t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
}
}
// test caching:
recorder := test("/squares/cache", "GET", nil, r)
if recorder.Code != http.StatusOK {
t.Errorf("returned %v. Expected %v.", recorder.Code, http.StatusOK)
}
if req, err := http.NewRequest("GET", "/squares/cache", nil); err != nil {
t.Errorf("%v", err)
} else {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("If-None-Match", recorder.Header().Get("Etag"))
recorder2 := httptest.NewRecorder()
r.ServeHTTP(recorder2, req)
if recorder2.Code != http.StatusNotModified {
t.Errorf("returned %v. Expected %v.", recorder2.Code, http.StatusNotModified)
}
}
}