1
0
Fork 0

Case insensitive host rule

This commit is contained in:
Benjamin Gandon 2018-10-25 10:18:03 +02:00 committed by Traefiker Bot
parent 22ee8700ca
commit aa26927d61
4 changed files with 45 additions and 5 deletions

View file

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

View file

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