Merge pull request #635 from dghubble/better-validation

matchbox/client: Validate client endpoint is a host:port
This commit is contained in:
Dalton Hubble
2017-08-09 14:45:57 -07:00
committed by GitHub
2 changed files with 24 additions and 0 deletions

View File

@@ -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)
}

View File

@@ -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)
}
}