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

Bug: NotFound handler was called although route exists #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
## [1.0.22]() (???)

### Bug Fixes

* fix a case that the notfound handler was called although route exists ([d704aaf](https://github.com/uptrace/bunrouter/pull/97/commits/d704aaf7098151c4196567f1b9d99e0c13ec3d42))

## [1.0.21](https://github.com/uptrace/bunrouter/compare/v1.0.20...v1.0.21) (2023-11-20)


Expand Down
9 changes: 5 additions & 4 deletions node.go
Expand Up @@ -143,9 +143,7 @@ func (n *node) _findRoute(meth, path string) (*node, *routeHandler, int) {
if handler != nil {
return node, handler, wildcardLen
}
if node != nil {
found = node
}
found = node
}
}
}
Expand All @@ -154,9 +152,12 @@ func (n *node) _findRoute(meth, path string) (*node, *routeHandler, int) {
if n.colon != nil {
if i := strings.IndexByte(path, '/'); i > 0 {
node, handler, wildcardLen := n.colon._findRoute(meth, path[i:])
if handler != nil {
if node != nil && handler != nil {
return node, handler, wildcardLen
}
if found == nil {
found = node
}
} else if n.colon.handlerMap != nil {
if handler := n.colon.handlerMap.Get(meth); handler != nil {
return n.colon, handler, 0
Expand Down
22 changes: 18 additions & 4 deletions router_test.go
Expand Up @@ -108,12 +108,15 @@ func TestNotFound(t *testing.T) {
require.Equal(t, http.StatusNotFound, w.Code)
require.Equal(t, 0, calledNotFound)

// Now try with a custome handler.
// Now try with a custom handler.
router = New(WithNotFoundHandler(notFoundHandler))
router.GET("/user/abc", simpleHandler)

w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/abc/", nil)

router.ServeHTTP(w, req)
require.Equal(t, http.StatusNotFound, w.Code)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, 1, calledNotFound)
}

Expand All @@ -127,6 +130,7 @@ func TestMethodNotAllowed(t *testing.T) {

router := New()
router.POST("/abc", simpleHandler)
router.GET("/abc/:id/def/:sub_id", simpleHandler)

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/abc", nil)
Expand All @@ -135,12 +139,22 @@ func TestMethodNotAllowed(t *testing.T) {
require.Equal(t, http.StatusMethodNotAllowed, w.Code)
require.Equal(t, 0, calledMethodNotAllowed)

// Now try with a custome handler.
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", "/abc/1/def/2", nil)

router.ServeHTTP(w, req)
require.Equal(t, http.StatusMethodNotAllowed, w.Code)
require.Equal(t, 0, calledMethodNotAllowed)

// Now try with a custom handler.
router = New(WithMethodNotAllowedHandler(methodNotAllowedHandler))
router.POST("/abc", simpleHandler)

w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/abc", nil)

router.ServeHTTP(w, req)
require.Equal(t, http.StatusMethodNotAllowed, w.Code)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, 1, calledMethodNotAllowed)
}

Expand Down