Case insensitive host rule
This commit is contained in:
parent
22ee8700ca
commit
aa26927d61
4 changed files with 45 additions and 5 deletions
|
@ -53,10 +53,10 @@ func (r *Rules) host(hosts ...string) *mux.Route {
|
|||
})
|
||||
}
|
||||
|
||||
func (r *Rules) hostRegexp(hosts ...string) *mux.Route {
|
||||
func (r *Rules) hostRegexp(hostPatterns ...string) *mux.Route {
|
||||
router := r.Route.Route.Subrouter()
|
||||
for _, host := range hosts {
|
||||
router.Host(strings.ToLower(host))
|
||||
for _, hostPattern := range hostPatterns {
|
||||
router.Host(hostPattern)
|
||||
}
|
||||
return r.Route.Route
|
||||
}
|
||||
|
|
|
@ -195,6 +195,39 @@ func TestHostRegexp(t *testing.T) {
|
|||
"http://barcom": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "regex insensitive",
|
||||
hostExp: "{dummy:[A-Za-z-]+\\.bar\\.com}",
|
||||
urls: map[string]bool{
|
||||
"http://FOO.bar.com": true,
|
||||
"http://foo.bar.com": true,
|
||||
"http://fooubar.com": false,
|
||||
"http://barucom": false,
|
||||
"http://barcom": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "insensitive host",
|
||||
hostExp: "{dummy:[a-z-]+\\.bar\\.com}",
|
||||
urls: map[string]bool{
|
||||
"http://FOO.bar.com": true,
|
||||
"http://foo.bar.com": true,
|
||||
"http://fooubar.com": false,
|
||||
"http://barucom": false,
|
||||
"http://barcom": false,
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "insensitive host simple",
|
||||
hostExp: "foo.bar.com",
|
||||
urls: map[string]bool{
|
||||
"http://FOO.bar.com": true,
|
||||
"http://foo.bar.com": true,
|
||||
"http://fooubar.com": false,
|
||||
"http://barucom": false,
|
||||
"http://barcom": false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
|
@ -212,7 +245,7 @@ func TestHostRegexp(t *testing.T) {
|
|||
|
||||
for testURL, match := range test.urls {
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, testURL, nil)
|
||||
assert.Equal(t, match, rt.Match(req, &mux.RouteMatch{}))
|
||||
assert.Equal(t, match, rt.Match(req, &mux.RouteMatch{}), testURL)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue