Skip to content

Commit

Permalink
Trim leading and trailing spaces from service tags
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
logocomune authored and tristanmorgan committed Sep 6, 2024
1 parent 3e6d9bb commit d29ac68
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions registry/consul/routecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
47 changes: 47 additions & 0 deletions registry/consul/routecmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit d29ac68

Please sign in to comment.