Skip to content

Commit efe8f3f

Browse files
cty123yuhan6665
authored andcommitted
fix(config): fix grpc cofnig parsing when service name only has one '/' char
1 parent 599cfd0 commit efe8f3f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

transport/internet/grpc/config.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ func (c *Config) getServiceName() string {
2121
if !strings.HasPrefix(c.ServiceName, "/") {
2222
return url.PathEscape(c.ServiceName)
2323
}
24+
2425
// Otherwise new custom paths
25-
rawServiceName := c.ServiceName[1:strings.LastIndex(c.ServiceName, "/")] // trim from first to last '/'
26+
lastIndex := strings.LastIndex(c.ServiceName, "/")
27+
if lastIndex < 1 {
28+
lastIndex = 1
29+
}
30+
rawServiceName := c.ServiceName[1:lastIndex] // trim from first to last '/'
2631
serviceNameParts := strings.Split(rawServiceName, "/")
2732
for i := range serviceNameParts {
2833
serviceNameParts[i] = url.PathEscape(serviceNameParts[i])

transport/internet/grpc/config_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package grpc
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"testing"
5+
6+
"github.com/stretchr/testify/assert"
67
)
78

89
func TestConfig_GetServiceName(t *testing.T) {
@@ -31,6 +32,11 @@ func TestConfig_GetServiceName(t *testing.T) {
3132
ServiceName: "/hello /world!/a|b",
3233
Expected: "hello%20/world%21",
3334
},
35+
{
36+
TestName: "path with only one '/'",
37+
ServiceName: "/foo",
38+
Expected: "",
39+
},
3440
}
3541
for _, test := range tests {
3642
t.Run(test.TestName, func(t *testing.T) {

0 commit comments

Comments
 (0)