func TestRemoveOfMiddleNode(t *testing.T) { handler := interfaces.HandlerFuncNext(func(rw http.ResponseWriter, r *http.Request, ctx context.Context, n interfaces.HandlerFunc) {}) tree := NewRadixTree() tree.Add("/web", handler) tree.Add("/web/news", nil) tree.Add("/web/article", nil) tree.Remove("/web") web, _, _, err := tree.Get("/web") if err != nil || web != nil { t.Fatalf("Deletion of middle node failed: %v, %v", err, web) } }
// Recursively converts a slice of Handlers to a single linked list of // HandlerContainers and returns the first one. At the end of the list, there // is another HandlerContainer whose ServeHTTP method is empty and does not // invoke any further Handlers. func ContainHandler(a []interfaces.Handler) *HandlerContainer { var next *HandlerContainer length := len(a) if length > 1 { next = ContainHandler(a[1:]) } else if length == 1 { next = &HandlerContainer{ interfaces.HandlerFuncNext(func(rw http.ResponseWriter, r *http.Request, ctx context.Context, n interfaces.HandlerFunc) {}), nil, } } else { return &HandlerContainer{ interfaces.HandlerFuncNext(func(rw http.ResponseWriter, r *http.Request, ctx context.Context, n interfaces.HandlerFunc) {}), nil, } } return &HandlerContainer{ a[0], next, } }
func TestWrapper(t *testing.T) { mockRouter := &MockRouter{} requestHandler := NewRequestHandler(mockRouter) requestHandler.SetRouter(mockRouter) testFunc := func(rw http.ResponseWriter, r *http.Request, ctx context.Context, n interfaces.HandlerFunc) { rw.Write([]byte("TestFunc")) } requestHandler.AppendGlobalPostHander(interfaces.HandlerFuncNext(testFunc)) request := &http.Request{} writer := httptest.NewRecorder() requestHandler.ServeHTTP(writer, request) if writer.Body.String() != "OKTestFunc" { t.Fatalf("Handler was not executed. Body: %s", writer.Body.String()) } }