Skip to content

Commit

Permalink
changed tokenizer to match std route match behavior; do not trimright…
Browse files Browse the repository at this point in the history
… the path (#511)
  • Loading branch information
emicklei committed Oct 19, 2022
1 parent 5d08dc1 commit ec34605
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
13 changes: 6 additions & 7 deletions curly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ func Test_detectWebService(t *testing.T) {
}

var routeMatchers = []struct {
route string
path string
matches bool
paramCount int
staticCount int
route string
path string
matches bool
paramCount int
staticCount int
hasCustomVerb bool
}{
// route, request-path
{"/a", "/a", true, 0, 1, false},
{"/a", "/b", false, 0, 0, false},
{"/a", "/b", false, 0, 0, false},
{"/a/{b}/c/", "/a/2/c", true, 1, 2, false},
{"/a/{b}/c/", "/a/2/c", false, 0, 0, false},
{"/{a}/{b}/{c}/", "/a/b", false, 0, 0, false},
{"/{x:*}", "/", false, 0, 0, false},
{"/{x:*}", "/a", true, 1, 0, false},
Expand All @@ -119,7 +119,6 @@ var routeMatchers = []struct {
{"/resources:run", "/resources", false, 0, 0, true},
{"/users/{userId:^prefix-}:start", "/users/prefix-}:startUserId", false, 0, 0, true},
{"/users/{userId:^prefix-}:start", "/users/prefix-userId:start", true, 1, 2, true},

}

// clear && go test -v -test.run Test_matchesRouteByPathTokens ...restful
Expand Down
7 changes: 5 additions & 2 deletions path_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestMatchesPath_TwoVars(t *testing.T) {
}

func TestMatchesPath_VarOnFront(t *testing.T) {
params := doExtractParams("{what}/from/{source}/", 3, "who/from/SOS/", t)
params := doExtractParams("{what}/from/{source}/", 4, "who/from/SOS/", t) // slash is not removed
if params["source"] != "SOS" {
t.Errorf("parameter mismatch SOS")
}
Expand Down Expand Up @@ -103,7 +103,10 @@ func doExtractParams(routePath string, size int, urlPath string, t *testing.T) m
r := Route{Path: routePath}
r.postBuild()
if len(r.pathParts) != size {
t.Fatalf("len not %v %v, but %v", size, r.pathParts, len(r.pathParts))
for i, each := range r.pathParts {
t.Logf("%d:%q", i, each)
}
t.Fatalf("len not %v, but %v", size, len(r.pathParts))
}
pathProcessor := defaultPathProcessor{}
return pathProcessor.ExtractParameters(&r, nil, urlPath)
Expand Down
4 changes: 3 additions & 1 deletion route.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func tokenizePath(path string) []string {
if "/" == path {
return nil
}
return strings.Split(strings.Trim(path, "/"), "/")
return strings.Split(strings.TrimLeft(path, "/"), "/")
}

// for debugging
Expand All @@ -176,3 +176,5 @@ func (r *Route) String() string {
func (r *Route) EnableContentEncoding(enabled bool) {
r.contentEncodingEnabled = &enabled
}

var TrimRightSlashEnabled = false
3 changes: 2 additions & 1 deletion tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "testing"

// Use like this:
//
// TraceLogger(testLogger{t})
// TraceLogger(testLogger{t})
type testLogger struct {
t *testing.T
}
Expand All @@ -14,5 +14,6 @@ func (l testLogger) Print(v ...interface{}) {
}

func (l testLogger) Printf(format string, v ...interface{}) {
l.t.Helper()
l.t.Logf(format, v...)
}

0 comments on commit ec34605

Please sign in to comment.