diff --git a/routergroup.go b/routergroup.go index 9cb0b989..480db981 100644 --- a/routergroup.go +++ b/routergroup.go @@ -189,6 +189,10 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS return func(c *Context) { if nolisting { c.Writer.WriteHeader(http.StatusNotFound) + + c.handlers = group.engine.allNoRoute + c.index = -1 + return } fileServer.ServeHTTP(c.Writer, c.Request) } diff --git a/routes_test.go b/routes_test.go index 60f1c81b..9e262a56 100644 --- a/routes_test.go +++ b/routes_test.go @@ -411,6 +411,21 @@ func TestRouterNotFound(t *testing.T) { assert.Equal(t, http.StatusNotFound, w.Code) } +func TestRouterStaticFSNotFound(t *testing.T) { + router := New() + + router.StaticFS("/", http.FileSystem(http.Dir("/thisdoesntexist/"))) + router.NoRoute(func(c *Context) { + c.String(404, "non existent") + }) + + w := performRequest(router, "GET", "nonexistent") + assert.Equal(t, "non existent", w.Body.String()) + + w = performRequest(router, "HEAD", "nonexistent") + assert.Equal(t, "non existent", w.Body.String()) +} + func TestRouteRawPath(t *testing.T) { route := New() route.UseRawPath = true