Avoid redirect loops to "/" fixes #15
diff --git a/router.go b/router.go index f4fff35..87e1010 100644 --- a/router.go +++ b/router.go
@@ -275,7 +275,7 @@ if handle, ps, tsr := root.getValue(path); handle != nil { handle(w, req, ps) return - } else if req.Method != "CONNECT" { + } else if req.Method != "CONNECT" && path != "/" { code := 301 // Permanent redirect, request with GET method if req.Method != "GET" { // Temporary redirect, request with same method @@ -283,7 +283,7 @@ code = 307 } - if tsr && r.RedirectTrailingSlash && path != "/" { + if tsr && r.RedirectTrailingSlash { if path[len(path)-1] == '/' { req.URL.Path = path[:len(path)-1] } else {
diff --git a/router_test.go b/router_test.go index b87ef0d..ca59066 100644 --- a/router_test.go +++ b/router_test.go
@@ -207,6 +207,16 @@ if !(w.Code == 307 && fmt.Sprint(w.Header()) == "map[Location:[/path]]") { t.Errorf("Custom NotFound handler failed: Code=%d, Header=%v", w.Code, w.Header()) } + + // Test special case where no node for the prefix "/" exists + router = New() + router.GET("/a", handlerFunc) + r, _ = http.NewRequest("GET", "/", nil) + w = httptest.NewRecorder() + router.ServeHTTP(w, r) + if !(w.Code == 404) { + t.Errorf("NotFound handling route / failed: Code=%d", w.Code) + } } func TestRouterPanicHandler(t *testing.T) {