Skip to content

Commit

Permalink
fix(config): fix grpc cofnig parsing when service name only has one '…
Browse files Browse the repository at this point in the history
…/' char
  • Loading branch information
cty123 authored and yuhan6665 committed Aug 21, 2023
1 parent 599cfd0 commit efe8f3f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion transport/internet/grpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
8 changes: 7 additions & 1 deletion transport/internet/grpc/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package grpc

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfig_GetServiceName(t *testing.T) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit efe8f3f

Please sign in to comment.