Skip to content
Myr

fix #1804 which is caused by calling middleware twice. (#1805)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix: https://github.com/gin-gonic/gin/issues/1804 `allNoRoute` contains middlewares such as `gin.Logger`, `gin.Recovery`, so on. The correct code is to use `noRoute`.

authored by Sai · committed by Bo-Yi Wu · parent 70a0aba · Browse files · 2 files+22 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • cc: @MetalBreaker

Diff stats

2 files+22 added−2 removed

  • other:+22 added−2 removed

2 files+22 added−2 removed

modified:routergroup.go+1 added-1 removed
193 unmodified lines
194 file := c.Param("filepath")195 // Check if file exists and/or if we have permission to access it196 if _, err := fs.Open(file); err != nil {197 c.Writer.WriteHeader(http.StatusNotFound)198 c.handlers = group.engine.allNoRoute198 c.handlers = group.engine.noRoute199 // Reset index200 c.index = -1201 return202 }
26 unmodified lines
modified:routes_test.go+21 added-1 removed
427 unmodified lines
428}429430func TestRouterStaticFSNotFound(t *testing.T) {431 router := New()432432 router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))433 router.NoRoute(func(c *Context) {434 c.String(404, "non existent")435 })
14 unmodified lines
450 performRequest(router, "GET", "/nonexistent")451 })452}453454// Reproduction test for the bug of issue #1805455func TestMiddlewareCalledOnceByRouterStaticFSNotFound(t *testing.T) {456 router := New()457458 // Middleware must be called just only once by per request.459 middlewareCalledNum := 0460 router.Use(func(c *Context) {461 middlewareCalledNum += 1462 })463464 router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))465466 // First access467 performRequest(router, "GET", "/nonexistent")468 assert.Equal(t, 1, middlewareCalledNum)469470 // Second access471 performRequest(router, "HEAD", "/nonexistent")472 assert.Equal(t, 2, middlewareCalledNum)473}474475func TestRouteRawPath(t *testing.T) {476 route := New()477 route.UseRawPath = true478
46 unmodified lines