Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion routergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS
// Check if file exists and/or if we have permission to access it
if _, err := fs.Open(file); err != nil {
c.Writer.WriteHeader(http.StatusNotFound)
c.handlers = group.engine.allNoRoute
c.handlers = group.engine.noRoute
// Reset index
c.index = -1
return
Expand Down
22 changes: 21 additions & 1 deletion routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ func TestRouterNotFound(t *testing.T) {

func TestRouterStaticFSNotFound(t *testing.T) {
router := New()

router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
router.NoRoute(func(c *Context) {
c.String(404, "non existent")
Expand All @@ -452,6 +451,27 @@ func TestRouterStaticFSFileNotFound(t *testing.T) {
})
}

// Reproduction test for the bug of issue #1805
func TestMiddlewareCalledOnceByRouterStaticFSNotFound(t *testing.T) {
router := New()

// Middleware must be called just only once by per request.
middlewareCalledNum := 0
router.Use(func(c *Context) {
middlewareCalledNum += 1
})

router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))

// First access
performRequest(router, "GET", "/nonexistent")
assert.Equal(t, 1, middlewareCalledNum)

// Second access
performRequest(router, "HEAD", "/nonexistent")
assert.Equal(t, 2, middlewareCalledNum)
}

func TestRouteRawPath(t *testing.T) {
route := New()
route.UseRawPath = true
Expand Down