mirror of
https://github.com/yusing/godoxy.git
synced 2026-01-11 22:30:47 +01:00
fix(route): enhance host parsing with port suffix support
- Added logic to strip the trailing :port from the host when searching for routes. - Updated findRouteByDomains function to ensure consistent host formatting. - Added related tests
This commit is contained in:
@@ -146,11 +146,18 @@ func findRouteAnyDomain(host string) types.HTTPRoute {
|
||||
if r, ok := routes.HTTP.Get(host); ok {
|
||||
return r
|
||||
}
|
||||
// try striping the trailing :port from the host
|
||||
if before, _, ok := strings.Cut(host, ":"); ok {
|
||||
if r, ok := routes.HTTP.Get(before); ok {
|
||||
return r
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func findRouteByDomains(domains []string) func(host string) types.HTTPRoute {
|
||||
return func(host string) types.HTTPRoute {
|
||||
host, _, _ = strings.Cut(host, ":") // strip the trailing :port
|
||||
for _, domain := range domains {
|
||||
if target, ok := strings.CutSuffix(host, domain); ok {
|
||||
if r, ok := routes.HTTP.Get(target); ok {
|
||||
|
||||
@@ -128,3 +128,47 @@ func TestFindRouteByDomainsExactMatch(t *testing.T) {
|
||||
|
||||
run(t, tests, testsNoMatch)
|
||||
}
|
||||
|
||||
func TestFindRouteWithPort(t *testing.T) {
|
||||
t.Run("AnyDomain", func(t *testing.T) {
|
||||
addRoute("app1")
|
||||
addRoute("app2.com")
|
||||
|
||||
tests := []string{
|
||||
"app1:8080",
|
||||
"app1.domain.com:8080",
|
||||
"app2.com:8080",
|
||||
}
|
||||
testsNoMatch := []string{
|
||||
"app11",
|
||||
"app2.co",
|
||||
"app2.co:8080",
|
||||
}
|
||||
run(t, tests, testsNoMatch)
|
||||
})
|
||||
|
||||
t.Run("ByDomains", func(t *testing.T) {
|
||||
ep.SetFindRouteDomains([]string{
|
||||
".domain.com",
|
||||
})
|
||||
addRoute("app1")
|
||||
addRoute("app2")
|
||||
addRoute("app3.domain.com")
|
||||
|
||||
tests := []string{
|
||||
"app1.domain.com:8080",
|
||||
"app2:8080", // exact match fallback
|
||||
"app3.domain.com:8080",
|
||||
}
|
||||
testsNoMatch := []string{
|
||||
"app11",
|
||||
"app1.domain.co",
|
||||
"app1.domain.co:8080",
|
||||
"app2.co",
|
||||
"app2.co:8080",
|
||||
"app3.domain.co",
|
||||
"app3.domain.co:8080",
|
||||
}
|
||||
run(t, tests, testsNoMatch)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user