Skip to content

Commit

Permalink
feat: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Oct 5, 2024
1 parent 20e20bd commit 3aa45ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions fox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 3aa45ef

Please sign in to comment.