From efe8f3f4d6cce5ed529c2b1aa0a39b7a08e031a3 Mon Sep 17 00:00:00 2001 From: cty123 Date: Sun, 20 Aug 2023 13:18:39 +0200 Subject: [PATCH] fix(config): fix grpc cofnig parsing when service name only has one '/' char --- transport/internet/grpc/config.go | 7 ++++++- transport/internet/grpc/config_test.go | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/transport/internet/grpc/config.go b/transport/internet/grpc/config.go index 39eadf31..aab1178e 100644 --- a/transport/internet/grpc/config.go +++ b/transport/internet/grpc/config.go @@ -21,8 +21,13 @@ func (c *Config) getServiceName() string { if !strings.HasPrefix(c.ServiceName, "/") { return url.PathEscape(c.ServiceName) } + // Otherwise new custom paths - rawServiceName := c.ServiceName[1:strings.LastIndex(c.ServiceName, "/")] // trim from first to last '/' + lastIndex := strings.LastIndex(c.ServiceName, "/") + if lastIndex < 1 { + lastIndex = 1 + } + rawServiceName := c.ServiceName[1:lastIndex] // trim from first to last '/' serviceNameParts := strings.Split(rawServiceName, "/") for i := range serviceNameParts { serviceNameParts[i] = url.PathEscape(serviceNameParts[i]) diff --git a/transport/internet/grpc/config_test.go b/transport/internet/grpc/config_test.go index fbc549b4..b159ffdf 100644 --- a/transport/internet/grpc/config_test.go +++ b/transport/internet/grpc/config_test.go @@ -1,8 +1,9 @@ package grpc import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestConfig_GetServiceName(t *testing.T) { @@ -31,6 +32,11 @@ func TestConfig_GetServiceName(t *testing.T) { ServiceName: "/hello /world!/a|b", Expected: "hello%20/world%21", }, + { + TestName: "path with only one '/'", + ServiceName: "/foo", + Expected: "", + }, } for _, test := range tests { t.Run(test.TestName, func(t *testing.T) {