fix(config): fix grpc cofnig parsing when service name only has one '/' char

This commit is contained in:
cty123 2023-08-20 13:18:39 +02:00 committed by yuhan6665
parent 599cfd09b0
commit efe8f3f4d6
2 changed files with 13 additions and 2 deletions

View File

@ -21,8 +21,13 @@ func (c *Config) getServiceName() string {
if !strings.HasPrefix(c.ServiceName, "/") { if !strings.HasPrefix(c.ServiceName, "/") {
return url.PathEscape(c.ServiceName) return url.PathEscape(c.ServiceName)
} }
// Otherwise new custom paths // 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, "/") serviceNameParts := strings.Split(rawServiceName, "/")
for i := range serviceNameParts { for i := range serviceNameParts {
serviceNameParts[i] = url.PathEscape(serviceNameParts[i]) serviceNameParts[i] = url.PathEscape(serviceNameParts[i])

View File

@ -1,8 +1,9 @@
package grpc package grpc
import ( import (
"github.com/stretchr/testify/assert"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestConfig_GetServiceName(t *testing.T) { func TestConfig_GetServiceName(t *testing.T) {
@ -31,6 +32,11 @@ func TestConfig_GetServiceName(t *testing.T) {
ServiceName: "/hello /world!/a|b", ServiceName: "/hello /world!/a|b",
Expected: "hello%20/world%21", Expected: "hello%20/world%21",
}, },
{
TestName: "path with only one '/'",
ServiceName: "/foo",
Expected: "",
},
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.TestName, func(t *testing.T) { t.Run(test.TestName, func(t *testing.T) {