From d29ac6826ae4330d5acc8eecf0f50ff9f7ae1d19 Mon Sep 17 00:00:00 2001 From: Alessandro Lucaferro Date: Thu, 21 Mar 2024 21:31:12 +0100 Subject: [PATCH] Trim leading and trailing spaces from service tags This update modifies the 'routecmd' function to trim leading and trailing spaces from service tags. Corresponding tests have been added to verify correct functionality when space-prefixed or suffixed tags are encountered. These changes ensure consistency in how tags are processed and compared. --- registry/consul/routecmd.go | 3 ++ registry/consul/routecmd_test.go | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/registry/consul/routecmd.go b/registry/consul/routecmd.go index 7a17ef0be..6eae8ad3b 100644 --- a/registry/consul/routecmd.go +++ b/registry/consul/routecmd.go @@ -26,6 +26,9 @@ type routecmd struct { func (r routecmd) build() []string { var svctags, routetags []string for _, t := range r.svc.ServiceTags { + + t = strings.TrimSpace(t) + if strings.HasPrefix(t, r.prefix) { routetags = append(routetags, t) } else { diff --git a/registry/consul/routecmd_test.go b/registry/consul/routecmd_test.go index d6b19b031..46f3e2e9f 100644 --- a/registry/consul/routecmd_test.go +++ b/registry/consul/routecmd_test.go @@ -28,6 +28,53 @@ func TestRouteCmd(t *testing.T) { `route add svc-1 foo/bar http://1.1.1.1:2222/`, }, }, + { + name: "http single tag with a space", + r: routecmd{ + prefix: "p-", + svc: &api.CatalogService{ + ServiceName: "svc-1", + ServiceAddress: "1.1.1.1", + ServicePort: 2222, + ServiceTags: []string{` p-foo/bar`}, + }, + }, + cfg: []string{ + `route add svc-1 foo/bar http://1.1.1.1:2222/`, + }, + }, + { + name: "http multiple tags", + r: routecmd{ + prefix: "p-", + svc: &api.CatalogService{ + ServiceName: "svc-1", + ServiceAddress: "1.1.1.1", + ServicePort: 2222, + ServiceTags: []string{`p-foo/bar`, `p-test/foo`}, + }, + }, + cfg: []string{ + `route add svc-1 foo/bar http://1.1.1.1:2222/`, + `route add svc-1 test/foo http://1.1.1.1:2222/`, + }, + }, + { + name: "http multiple routes with space prefix route", + r: routecmd{ + prefix: "p-", + svc: &api.CatalogService{ + ServiceName: "svc-1", + ServiceAddress: "1.1.1.1", + ServicePort: 2222, + ServiceTags: []string{`p-foo/bar`, ` p-test/foo`}, + }, + }, + cfg: []string{ + `route add svc-1 foo/bar http://1.1.1.1:2222/`, + `route add svc-1 test/foo http://1.1.1.1:2222/`, + }, + }, { name: "tcp", r: routecmd{