Skip to content

Commit

Permalink
Merge pull request #173 from ukurysheva/host-check
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleDi authored Nov 22, 2023
2 parents 9d8cfa4 + 928b857 commit 8997ea9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
7 changes: 6 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ func ParseDSN(dsn string) (*Config, error) {
if err != nil {
return nil, err
}

if u.Host == "" {
return nil, fmt.Errorf("invalid host")
}

cfg := NewConfig()

cfg.Scheme, cfg.Host = u.Scheme, u.Host
Expand Down Expand Up @@ -184,7 +189,7 @@ func parseDSNParams(cfg *Config, params map[string][]string) (err error) {
func ensureHavePort(addr string) string {
if _, _, err := net.SplitHostPort(addr); err != nil {
// we get the missing port error here
if addr[0] == '[' && addr[len(addr)-1] == ']' {
if len(addr) > 1 && addr[0] == '[' && addr[len(addr)-1] == ']' {
// ipv6 brackets
addr = addr[1 : len(addr)-1]
}
Expand Down
17 changes: 11 additions & 6 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clickhouse

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -53,14 +54,18 @@ func TestDefaultConfig(t *testing.T) {
}

func TestParseWrongDSN(t *testing.T) {
testCases := []string{
"http://localhost:8123/?database=test",
"http://localhost:8123/?default_format=Native",
"http://localhost:8123/?query=SELECT%201",
testCases := []struct {
dsn string
errExpected error
}{
{dsn: "http:bad://localhost:8123/?database=test", errExpected: fmt.Errorf("invalid host")},
{dsn: "http://localhost:8123/?database=test", errExpected: fmt.Errorf("unknown option 'database'")},
{dsn: "http://localhost:8123/?default_format=Native", errExpected: fmt.Errorf("unknown option 'default_format'")},
{dsn: "http://localhost:8123/?query=SELECT%201", errExpected: fmt.Errorf("unknown option 'query'")},
}
for _, tc := range testCases {
_, err := ParseDSN(tc)
assert.Error(t, err)
_, err := ParseDSN(tc.dsn)
assert.Equal(t, tc.errExpected, err)
}
}

Expand Down

0 comments on commit 8997ea9

Please sign in to comment.