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:
Domen Ipavec 2017-09-07 18:24:55 +02:00 committed by Peter Kieltyka
parent 04ec7fc417
commit 624e6e12e9
2 changed files with 34 additions and 0 deletions

View file

@ -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
}

View file

@ -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) {})