diff --git a/dispatcher.go b/dispatcher.go index 69a99a7..b377146 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -349,7 +349,7 @@ func parseConnectUrl(connectUrl string) (*connectArgs, error) { return nil, err } - parts := strings.Split(u.Host, ".") + parts := strings.Split(u.Hostname(), ".") serviceName := parts[0] diff --git a/dispatcher_test.go b/dispatcher_test.go index 149bb3e..b8fba8b 100644 --- a/dispatcher_test.go +++ b/dispatcher_test.go @@ -29,7 +29,8 @@ import ( const testCtxKey key = "testkey" func newTestDispatcher() *Dispatcher { - d, _ := NewDispatcher("kubernetes://my-service.my-namespace", + d, _ := NewDispatcher( + "kubernetes://my-service.my-namespace", WithKubernetesClientset(fake.NewSimpleClientset()), WithDialOptions( grpc.WithTransportCredentials(insecure.NewCredentials()), @@ -38,6 +39,41 @@ func newTestDispatcher() *Dispatcher { return d } +func TestParseConnectUrl(t *testing.T) { + tests := []struct { + name string + setUrl string + wantNamespace string + wantServiceName string + wantPort string + }{ + { + "default port", + "kubernetes://my-service.my-namespace", + "my-namespace", + "my-service", + "50051", + }, + { + "custom port number", + "kubernetes://my-service.my-namespace:1234", + "my-namespace", + "my-service", + "1234", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + args, err := parseConnectUrl(tt.setUrl) + require.Nil(t, err) + require.Equal(t, tt.wantNamespace, args.Namespace) + require.Equal(t, tt.wantServiceName, args.ServiceName) + require.Equal(t, tt.wantPort, args.Port) + }) + } +} + func TestDispatcherUpdateState(t *testing.T) { initialIps := []string{"ip1", "ip2"}