From 3aa45efe8a59601a6a72faec133d11d1041e5fc2 Mon Sep 17 00:00:00 2001 From: tigerwill90 Date: Sat, 5 Oct 2024 23:42:49 +0200 Subject: [PATCH] feat: improve test coverage --- fox_test.go | 12 ++++++++++++ tree.go | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/fox_test.go b/fox_test.go index 779613e..ba84358 100644 --- a/fox_test.go +++ b/fox_test.go @@ -1379,6 +1379,18 @@ func TestUpdateConflict(t *testing.T) { } } +func TestInvalidRoute(t *testing.T) { + f := New() + // Invalid route on insert + assert.ErrorIs(t, f.Handle("get", "/foo", emptyHandler), ErrInvalidRoute) + assert.ErrorIs(t, f.Handle("", "/foo", emptyHandler), ErrInvalidRoute) + assert.ErrorIs(t, f.Handle(http.MethodGet, "/foo", nil), ErrInvalidRoute) + + // Invalid route on update + assert.ErrorIs(t, f.Update("", "/foo", emptyHandler), ErrInvalidRoute) + assert.ErrorIs(t, f.Update(http.MethodGet, "/foo", nil), ErrInvalidRoute) +} + func TestUpdateRoute(t *testing.T) { cases := []struct { name string diff --git a/tree.go b/tree.go index 277ecd5..9f1a1eb 100644 --- a/tree.go +++ b/tree.go @@ -45,6 +45,9 @@ type Tree struct { // for serving requests. However, this function is NOT thread-safe and should be run serially, along with all other // Tree APIs that perform write operations. To override an existing route, use Update. func (t *Tree) Handle(method, path string, handler HandlerFunc, opts ...PathOption) error { + if handler == nil { + return fmt.Errorf("%w: nil handler", ErrInvalidRoute) + } if matched := regEnLetter.MatchString(method); !matched { return fmt.Errorf("%w: missing or invalid http method", ErrInvalidRoute) } @@ -62,6 +65,9 @@ func (t *Tree) Handle(method, path string, handler HandlerFunc, opts ...PathOpti // serving requests. However, this function is NOT thread-safe and should be run serially, along with all other // Tree APIs that perform write operations. To add a new handler, use Handle method. func (t *Tree) Update(method, path string, handler HandlerFunc, opts ...PathOption) error { + if handler == nil { + return fmt.Errorf("%w: nil handler", ErrInvalidRoute) + } if method == "" { return fmt.Errorf("%w: missing http method", ErrInvalidRoute) }