mirror of
https://github.com/outbackdingo/matchbox.git
synced 2026-01-27 18:19:36 +00:00
Merge pull request #635 from dghubble/better-validation
matchbox/client: Validate client endpoint is a host:port
This commit is contained in:
@@ -3,6 +3,8 @@ package client
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
@@ -40,6 +42,11 @@ func New(config *Config) (*Client, error) {
|
||||
if len(config.Endpoints) == 0 {
|
||||
return nil, errNoEndpoints
|
||||
}
|
||||
for _, endpoint := range config.Endpoints {
|
||||
if _, _, err := net.SplitHostPort(endpoint); err != nil {
|
||||
return nil, fmt.Errorf("client: invalid host:port endpoint: %v", err)
|
||||
}
|
||||
}
|
||||
return newClient(config)
|
||||
}
|
||||
|
||||
|
||||
@@ -14,3 +14,20 @@ func TestNew_MissingEndpoints(t *testing.T) {
|
||||
assert.Nil(t, client)
|
||||
assert.Equal(t, errNoEndpoints, err)
|
||||
}
|
||||
|
||||
// gRPC expects host:port with no scheme (e.g. matchbox.example.com:8081)
|
||||
func TestNew_InvalidEndpoints(t *testing.T) {
|
||||
invalid := []string{
|
||||
"matchbox.example.com",
|
||||
"http://matchbox.example.com:8081",
|
||||
"https://matchbox.example.com:8081",
|
||||
}
|
||||
|
||||
for _, endpoint := range invalid {
|
||||
client, err := New(&Config{
|
||||
Endpoints: []string{endpoint},
|
||||
})
|
||||
assert.Nil(t, client)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user