Fix: regex matching only part of parameter (#258)
* Add start and end to param regex * Added test for regex match whole param
This commit is contained in:
parent
04ec7fc417
commit
624e6e12e9
2 changed files with 34 additions and 0 deletions
9
tree.go
9
tree.go
|
|
@ -676,6 +676,15 @@ func patNextSegment(pattern string) (nodeTyp, string, string, byte, int, int) {
|
|||
key = key[:idx]
|
||||
}
|
||||
|
||||
if len(rexpat) > 0 {
|
||||
if rexpat[0] != '^' {
|
||||
rexpat = "^" + rexpat
|
||||
}
|
||||
if rexpat[len(rexpat)-1] != '$' {
|
||||
rexpat = rexpat + "$"
|
||||
}
|
||||
}
|
||||
|
||||
return nt, key, rexpat, tail, ps, pe
|
||||
}
|
||||
|
||||
|
|
|
|||
25
tree_test.go
25
tree_test.go
|
|
@ -333,6 +333,31 @@ func TestTreeRegexp(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestTreeRegexMatchWholeParam(t *testing.T) {
|
||||
hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
||||
|
||||
rctx := NewRouteContext()
|
||||
tr := &node{}
|
||||
tr.InsertRoute(mGET, "/{id:[0-9]+}", hStub1)
|
||||
|
||||
tests := []struct {
|
||||
url string
|
||||
expectedHandler http.Handler
|
||||
}{
|
||||
{url: "/13", expectedHandler: hStub1},
|
||||
{url: "/a13", expectedHandler: nil},
|
||||
{url: "/13.jpg", expectedHandler: nil},
|
||||
{url: "/a13.jpg", expectedHandler: nil},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
_, _, handler := tr.FindRoute(rctx, mGET, tc.url)
|
||||
if fmt.Sprintf("%v", tc.expectedHandler) != fmt.Sprintf("%v", handler) {
|
||||
t.Errorf("expecting handler:%v , got:%v", tc.expectedHandler, handler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeFindPattern(t *testing.T) {
|
||||
hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
||||
hStub2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
||||
|
|
|
|||
Loading…
Reference in a new issue