Add non regex pathPrefix
This commit is contained in:
parent
877770f7cf
commit
b23b2611b3
2 changed files with 87 additions and 2 deletions
|
@ -192,3 +192,67 @@ type fakeHandler struct {
|
|||
}
|
||||
|
||||
func (h *fakeHandler) ServeHTTP(http.ResponseWriter, *http.Request) {}
|
||||
|
||||
func TestPathPrefix(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
path string
|
||||
urls map[string]bool
|
||||
}{
|
||||
{
|
||||
desc: "leading slash",
|
||||
path: "/bar",
|
||||
urls: map[string]bool{
|
||||
"http://foo.com/bar": true,
|
||||
"http://foo.com/bar/": true,
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "leading trailing slash",
|
||||
path: "/bar/",
|
||||
urls: map[string]bool{
|
||||
"http://foo.com/bar": false,
|
||||
"http://foo.com/bar/": true,
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "no slash",
|
||||
path: "bar",
|
||||
urls: map[string]bool{
|
||||
"http://foo.com/bar": false,
|
||||
"http://foo.com/bar/": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "trailing slash",
|
||||
path: "bar/",
|
||||
urls: map[string]bool{
|
||||
"http://foo.com/bar": false,
|
||||
"http://foo.com/bar/": false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rls := &Rules{
|
||||
route: &serverRoute{
|
||||
route: &mux.Route{},
|
||||
},
|
||||
}
|
||||
|
||||
rt := rls.pathPrefix(test.path)
|
||||
|
||||
for testURL, expectedMatch := range test.urls {
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, testURL, nil)
|
||||
match := rt.Match(req, &mux.RouteMatch{})
|
||||
if match != expectedMatch {
|
||||
t.Errorf("Error matching %s with %s, got %v expected %v", test.path, testURL, match, expectedMatch)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue