Skip to content

Commit 47b6547

Browse files
tigerwill90tigerwill90
andauthored
Add support for manually invoking the NoRoute handler (#59)
* feat(router): add HandleNoRoute method * feat: simple semantic update for ErrNotSupported. --------- Co-authored-by: tigerwill90 <contact@sylvainmuller.ch>
1 parent a50f853 commit 47b6547

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

fox.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ const (
9999
// Router is a lightweight high performance HTTP request router that support mutation on its routing tree
100100
// while handling request concurrently.
101101
type Router struct {
102+
noRouteBase HandlerFunc
102103
noRoute HandlerFunc
103104
noMethod HandlerFunc
104105
tsrRedirect HandlerFunc
@@ -138,7 +139,7 @@ var _ http.Handler = (*Router)(nil)
138139
func New(opts ...GlobalOption) (*Router, error) {
139140
r := new(Router)
140141

141-
r.noRoute = DefaultNotFoundHandler
142+
r.noRouteBase = DefaultNotFoundHandler
142143
r.noMethod = DefaultMethodNotAllowedHandler
143144
r.autoOptions = DefaultOptionsHandler
144145
r.clientip = noClientIPResolver{}
@@ -151,7 +152,7 @@ func New(opts ...GlobalOption) (*Router, error) {
151152
}
152153
}
153154

154-
r.noRoute = applyMiddleware(NoRouteHandler, r.mws, r.noRoute)
155+
r.noRoute = applyMiddleware(NoRouteHandler, r.mws, r.noRouteBase)
155156
r.noMethod = applyMiddleware(NoMethodHandler, r.mws, r.noMethod)
156157
r.tsrRedirect = applyMiddleware(RedirectHandler, r.mws, defaultRedirectTrailingSlashHandler)
157158
r.autoOptions = applyMiddleware(OptionsHandler, r.mws, r.autoOptions)
@@ -361,6 +362,11 @@ func (fox *Router) NewRoute(pattern string, handler HandlerFunc, opts ...RouteOp
361362
return rte, nil
362363
}
363364

365+
// HandleNoRoute calls the no route handler with the provided [Context].
366+
func (fox *Router) HandleNoRoute(c Context) {
367+
fox.noRouteBase(c)
368+
}
369+
364370
// Len returns the number of registered route.
365371
func (fox *Router) Len() int {
366372
tree := fox.getRoot()

fox_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4806,6 +4806,29 @@ func TestRouterWithAllowedMethod(t *testing.T) {
48064806
}
48074807
}
48084808

4809+
func TestRouterHandleNoRoute(t *testing.T) {
4810+
called := 0
4811+
m := MiddlewareFunc(func(next HandlerFunc) HandlerFunc {
4812+
return func(c Context) {
4813+
called++
4814+
next(c)
4815+
}
4816+
})
4817+
4818+
f, err := New(WithMiddleware(m))
4819+
require.NoError(t, err)
4820+
require.NoError(t, onlyError(f.Handle(http.MethodGet, "/foo", func(c Context) {
4821+
c.Fox().HandleNoRoute(c)
4822+
})))
4823+
4824+
w := httptest.NewRecorder()
4825+
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
4826+
f.ServeHTTP(w, req)
4827+
assert.Equal(t, http.StatusNotFound, w.Code)
4828+
assert.Equal(t, 1, called)
4829+
4830+
}
4831+
48094832
func TestRouterWithAllowedMethodAndIgnoreTsEnable(t *testing.T) {
48104833
f, _ := New(WithNoMethod(true), WithIgnoreTrailingSlash(true))
48114834

options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func WithNoRouteHandler(handler HandlerFunc) GlobalOption {
5757
if handler == nil {
5858
return fmt.Errorf("%w: no route handler cannot be nil", ErrInvalidConfig)
5959
}
60-
s.router.noRoute = handler
60+
s.router.noRouteBase = handler
6161
return nil
6262
})
6363
}

response_writer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,7 @@ func relevantCaller() runtime.Frame {
328328
return frame
329329
}
330330

331-
var errHttpNotSupported = fmt.Errorf("%w", http.ErrNotSupported)
332-
333331
// ErrNotSupported returns an error that Is http.ErrNotSupported, but is not == to it.
334332
func ErrNotSupported() error {
335-
return errHttpNotSupported
333+
return fmt.Errorf("%w", http.ErrNotSupported)
336334
}

0 commit comments

Comments
 (0)