Exemplo n.º 1
0
func BenchmarkStaticRouting(b *testing.B) {
	r := router.NewTreeRouter()
	requestHandler := NewRequestHandler(r)
	preHandler := &MockHandler{output: "A"}
	routerHandler := &MockHandler{output: "B"}
	postHandler := &MockHandler{output: "C"}
	r.Add(router.GET, "/", routerHandler)
	requestHandler.AppendGlobalPreHandler(preHandler)
	requestHandler.AppendGlobalPostHander(postHandler)
	writer := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/", nil)
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		requestHandler.ServeHTTP(writer, req)
	}
}
Exemplo n.º 2
0
func TestActualRouter(t *testing.T) {
	r := router.NewTreeRouter()
	requestHandler := NewRequestHandler(r)
	preHandler := &MockHandler{output: "A"}
	routerHandler := &MockHandler{output: "B"}
	postHandler := &MockHandler{output: "C"}
	r.Add(router.GET, "/", routerHandler)
	requestHandler.AppendGlobalPreHandler(preHandler)
	requestHandler.AppendGlobalPostHander(postHandler)
	writer := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/", nil)
	requestHandler.ServeHTTP(writer, req)
	if writer.Body.String() != "ABC" {
		t.Logf("Output and possibly its order was not written as expected. Go %s instead of ABC.", writer.Body.String())
		t.Fail()
	}
}