Skip to content

Commit

Permalink
[patch] fixed bug where trailing slash config was being ignored when …
Browse files Browse the repository at this point in the history
…using wildcard

[-] removed some unused constants
  • Loading branch information
bnkamalesh committed Sep 7, 2021
1 parent 7f8a116 commit c2580b8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
5 changes: 4 additions & 1 deletion route.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Route struct {
// Pattern is the URI pattern to match
Pattern string
// TrailingSlash if set to true, the URI will be matched with or without
// a trailing slash. Note: It does not *do* a redirect.
// a trailing slash. IMPORTANT: It does not redirect.
TrailingSlash bool

// FallThroughPostResponse if enabled will execute all the handlers even if a response was already sent to the client
Expand Down Expand Up @@ -44,6 +44,9 @@ func (r *Route) computePatternStr(patternString string, hasWildcard bool, key st
if hasWildcard {
patternKey = fmt.Sprintf(":%s*", key)
regexPattern = urlwildcard
if r.TrailingSlash {
regexPattern = urlwildcardWithTrailslash
}
} else {
patternKey = fmt.Sprintf(":%s", key)
regexPattern = urlchars
Expand Down
11 changes: 5 additions & 6 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import (
// https://tools.ietf.org/html/rfc3986
// Though the current one allows invalid characters in the URI parameter, it has better performance.
const (
urlchars = `([^/]+)`
urlwildcard = `(.*)`
trailingSlash = `[\/]?`
errMultiHeaderWrite = `http: multiple response.WriteHeader calls`
errMultiWrite = `http: multiple response.Write calls`
errDuplicateKey = `Error: Duplicate URI keys found`
urlchars = `([^/]+)`
urlwildcard = `(.*)[^/]`
urlwildcardWithTrailslash = `(.*)[/]?`
trailingSlash = `[/]?`
errDuplicateKey = `Error: Duplicate URI keys found`
)

var (
Expand Down
40 changes: 35 additions & 5 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ func TestRouter_ServeHTTP(t *testing.T) {
url := baseAPI
if l.Path != "" {
switch l.TestType {
case "checkpath", "checkpathnotrailingslash", "chaining", "chaining-nofallthrough":
case "checkpath",
"checkpathnotrailingslash",
"chaining",
"chaining-nofallthrough":
{
url = strings.Join([]string{url, l.Path}, "")
}
case "checkparams":
case "checkparams", "widlcardwithouttrailingslash":
{
for idx, key := range l.ParamKeys {
// in case of wildcard params, they have to replaced first for proper URL construction
// in case of wildcard params, they have to be replaced first for proper URL construction
l.Path = strings.Replace(l.Path, ":"+key+"*", l.Params[idx], 1)
l.Path = strings.Replace(l.Path, ":"+key, l.Params[idx], 1)
}
Expand All @@ -60,7 +63,7 @@ func TestRouter_ServeHTTP(t *testing.T) {
router.ServeHTTP(respRec, req)

switch l.TestType {
case "checkpath", "checkpathnotrailingslash":
case "checkpath", "checkpathnotrailingslash", "widlcardwithouttrailingslash":
{
err = checkPath(req, respRec)
}
Expand Down Expand Up @@ -99,7 +102,15 @@ func TestRouter_ServeHTTP(t *testing.T) {
)
}
}
} else if err == nil && l.WantErr {
t.Errorf(
"'%s' (%s '%s') expected error, but received nil",
l.Name,
l.Method,
url,
)
}

err = checkMiddleware(req, respRec)
if err != nil {
t.Error(err.Error())
Expand Down Expand Up @@ -138,7 +149,7 @@ func getRoutes() []*Route {
},
)
}
case "checkpathnotrailingslash":
case "checkpathnotrailingslash", "widlcardwithouttrailingslash":
{
rr = append(rr,
&Route{
Expand All @@ -152,6 +163,7 @@ func getRoutes() []*Route {
)

}

case "chaining":
{
rr = append(
Expand Down Expand Up @@ -462,6 +474,24 @@ func testTable() []struct {
Params: []string{"hello/world/hi/there/-/~/./again"},
WantErr: false,
},
{
Name: "Check with wildcard - 3",
TestType: "widlcardwithouttrailingslash",
Path: "/wildcard3/:a*",
Method: http.MethodGet,
ParamKeys: []string{"a"},
Params: []string{"hello/world/hi/there/-/~/./again/"},
WantErr: true,
},
{
Name: "Check with wildcard - 4",
TestType: "widlcardwithouttrailingslash",
Path: "/wildcard3/:a*",
Method: http.MethodGet,
ParamKeys: []string{"a"},
Params: []string{"hello/world/hi/there/-/~/./again"},
WantErr: false,
},
{
Name: "Check not implemented",
TestType: "notimplemented",
Expand Down

0 comments on commit c2580b8

Please sign in to comment.