Skip to content

Commit

Permalink
[patch] fixed an edge case of incorrect route match for root route ('/')
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkamalesh committed Jun 19, 2022
1 parent 00fc655 commit 8a92a16
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![](https://godoc.org/github.com/nathany/looper?status.svg)](http://godoc.org/github.com/bnkamalesh/webgo)
[![](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#web-frameworks)

# WebGo v6.6.5
# WebGo v6.6.6

WebGo is a minimalistic framework for [Go](https://golang.org) to build web applications (server side) with no 3rd party dependencies. WebGo will always be Go standard library compliant; with the HTTP handlers having the same signature as [http.HandlerFunc](https://golang.org/pkg/net/http/#HandlerFunc).

Expand Down
7 changes: 7 additions & 0 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,18 @@ func (r *Route) matchPath(requestURI string) (bool, map[string]string) {
}

func (r *Route) matchWithWildcard(requestURI string) (bool, map[string]string) {
// if r.fragments is empty, it means there are no variables in the URI pattern
// hence no point checking
if len(r.fragments) == 0 {
return false, nil
}

params := make(map[string]string, r.paramsCount)
uriFragments := strings.Split(requestURI, "/")[1:]
fragmentsLastIdx := len(r.fragments) - 1
fragmentIdx := 0
uriParameter := make([]string, 0, len(uriFragments))

for idx, fragment := range uriFragments {
// if part is empty, it means it's end of URI with trailing slash
if fragment == "" {
Expand Down
41 changes: 41 additions & 0 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,45 @@ func TestMatchWithWildcard(t *testing.T) {
return
}
})

t.Run("root URI, no match", func(t *testing.T) {
route := Route{
Name: "",
Method: http.MethodGet,
TrailingSlash: true,
FallThroughPostResponse: true,
Pattern: "/-/health",
Handlers: []http.HandlerFunc{dummyHandler},
}
err := route.init()
if err != nil {
t.Error(err)
return
}
matched, _ := route.matchPath("/")
if matched {
t.Errorf("Expected no match, got match")
return
}
})
t.Run("root URI, should match", func(t *testing.T) {
route := Route{
Name: "",
Method: http.MethodGet,
TrailingSlash: true,
FallThroughPostResponse: true,
Pattern: "/",
Handlers: []http.HandlerFunc{dummyHandler},
}
err := route.init()
if err != nil {
t.Error(err)
return
}
matched, _ := route.matchPath("/")
if !matched {
t.Errorf("Expected match, got no match")
return
}
})
}

0 comments on commit 8a92a16

Please sign in to comment.