Skip to content

Commit 903e85f

Browse files
authored
Fixes port number parsing bug (#6)
1 parent e93219f commit 903e85f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

dispatcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func parseConnectUrl(connectUrl string) (*connectArgs, error) {
349349
return nil, err
350350
}
351351

352-
parts := strings.Split(u.Host, ".")
352+
parts := strings.Split(u.Hostname(), ".")
353353

354354
serviceName := parts[0]
355355

dispatcher_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import (
2929
const testCtxKey key = "testkey"
3030

3131
func newTestDispatcher() *Dispatcher {
32-
d, _ := NewDispatcher("kubernetes://my-service.my-namespace",
32+
d, _ := NewDispatcher(
33+
"kubernetes://my-service.my-namespace",
3334
WithKubernetesClientset(fake.NewSimpleClientset()),
3435
WithDialOptions(
3536
grpc.WithTransportCredentials(insecure.NewCredentials()),
@@ -38,6 +39,41 @@ func newTestDispatcher() *Dispatcher {
3839
return d
3940
}
4041

42+
func TestParseConnectUrl(t *testing.T) {
43+
tests := []struct {
44+
name string
45+
setUrl string
46+
wantNamespace string
47+
wantServiceName string
48+
wantPort string
49+
}{
50+
{
51+
"default port",
52+
"kubernetes://my-service.my-namespace",
53+
"my-namespace",
54+
"my-service",
55+
"50051",
56+
},
57+
{
58+
"custom port number",
59+
"kubernetes://my-service.my-namespace:1234",
60+
"my-namespace",
61+
"my-service",
62+
"1234",
63+
},
64+
}
65+
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
args, err := parseConnectUrl(tt.setUrl)
69+
require.Nil(t, err)
70+
require.Equal(t, tt.wantNamespace, args.Namespace)
71+
require.Equal(t, tt.wantServiceName, args.ServiceName)
72+
require.Equal(t, tt.wantPort, args.Port)
73+
})
74+
}
75+
}
76+
4177
func TestDispatcherUpdateState(t *testing.T) {
4278
initialIps := []string{"ip1", "ip2"}
4379

0 commit comments

Comments
 (0)