From d704aaf7098151c4196567f1b9d99e0c13ec3d42 Mon Sep 17 00:00:00 2001 From: sknr Date: Mon, 15 Jan 2024 20:53:58 +0100 Subject: [PATCH 1/2] fix: notfound handler was called although route exists --- node.go | 9 +++++---- router_test.go | 22 ++++++++++++++++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/node.go b/node.go index d1713dd..fd99ebc 100644 --- a/node.go +++ b/node.go @@ -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 } } } @@ -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 diff --git a/router_test.go b/router_test.go index 362bb9e..dc1f8d6 100644 --- a/router_test.go +++ b/router_test.go @@ -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) } @@ -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) @@ -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) } From 5cebd18204eda76b7f11d79daeb38d07ca7ea873 Mon Sep 17 00:00:00 2001 From: sknr Date: Mon, 15 Jan 2024 21:55:59 +0100 Subject: [PATCH 2/2] docs: update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd505d8..37e4aa2 100644 --- a/CHANGELOG.md +++ b/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)