Skip to content

Commit

Permalink
Fixing URL issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Shaw committed Mar 23, 2017
1 parent aa426d5 commit eebe57e
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 210 deletions.
4 changes: 2 additions & 2 deletions dburl.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ var (
// ErrUnknownDatabaseScheme is the unknown database type error.
ErrUnknownDatabaseScheme = errors.New("unknown database scheme")

// ErrInvalidPort is the invalid port error.
ErrInvalidPort = errors.New("invalid port")
// ErrInvalidHost is the invalid host error.
ErrInvalidHost = errors.New("invalid host")
)

// Open takes a urlstr like "protocol+transport://user:pass@host/dbname?option1=a&option2=b"
Expand Down
63 changes: 60 additions & 3 deletions dburl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,72 @@ package dburl

import "testing"

func TestBadParse(t *testing.T) {
tests := []struct {
s string
}{
{``},
{`pgsqlx://`},
{`m`},
{`pg+udp://`},
{`sqlite+unix://`},
{`sqlite+tcp://`},
{`file+tcp://`},
{`mssql+tcp://user:pass@host/dbname`},
{`mssql+aoeu://`},
{`mssql+unix:/var/run/mssql.sock`},
{`mssql+udp:localhost:155`},
}

for i, test := range tests {
_, err := Parse(test.s)
if err == nil {
t.Errorf("test %d expected error parsing `%s`, got: nil", i, test.s)
}
}
}

func TestParse(t *testing.T) {
tests := []struct {
s string
d string
exp string
}{
{`pg:booktest:booktest@localhost/booktest`, `postgres`, `postgres://booktest:booktest@localhost/booktest`},
{`mssql://user:!234%23$@localhost:1580/dbname`, `mssql`, `server=localhost;port=1580;database=dbname;user id=user;password=!234#$`},
{`adodb://Microsoft.ACE.OLEDB.12.0?Extended+Properties="Text;HDR=NO;FMT=Delimited"`, `adodb`, `Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.;Extended Properties="Text;HDR=NO;FMT=Delimited"`},
{`pg:`, `postgres`, ``},
{`pg://`, `postgres`, ``},
{`pg:user:pass@localhost/booktest`, `postgres`, `dbname=booktest host=localhost password=pass user=user`},
{`pg:/var/run/postgresql`, `postgres`, `host=/var/run/postgresql`},
{`pg:/var/run/postgresql:6666/mydb`, `postgres`, `dbname=mydb host=/var/run/postgresql port=6666`},
{`pg:/var/run/postgresql/mydb`, `postgres`, `dbname=mydb host=/var/run/postgresql`},
{`pg:/var/run/postgresql:7777`, `postgres`, `host=/var/run/postgresql port=7777`},
{`pg+unix:/var/run/postgresql:4444/booktest`, `postgres`, `dbname=booktest host=/var/run/postgresql port=4444`},
{`pg:user:pass@/var/run/postgresql/mydb`, `postgres`, `dbname=mydb host=/var/run/postgresql password=pass user=user`},
{`pg:user:pass@/really/bad/path`, `postgres`, `host=/really/bad/path password=pass user=user`},

{`my:`, `mysql`, `/`}, // 10
{`my://`, `mysql`, `/`},
{`my:booktest:booktest@localhost/booktest`, `mysql`, `booktest:booktest@tcp(localhost)/booktest`},
{`my:/var/run/mysqld/mysqld.sock/mydb?timeout=90`, `mysql`, `unix(/var/run/mysqld/mysqld.sock)/mydb?timeout=90`},
{`my:///var/run/mysqld/mysqld.sock/mydb?timeout=90`, `mysql`, `unix(/var/run/mysqld/mysqld.sock)/mydb?timeout=90`},
{`my+unix:user:pass@mysqld.sock?timeout=90`, `mysql`, `user:pass@unix(mysqld.sock)/?timeout=90`},

{`mymy:`, `mymysql`, ``}, // 16
{`mymy://`, `mymysql`, ``},
{`mymy:user:pass@localhost/booktest`, `mymysql`, `tcp:localhost*booktest/user/pass`},
{`mymy:/var/run/mysqld/mysqld.sock/mydb?timeout=90&test=true`, `mymysql`, `unix:/var/run/mysqld/mysqld.sock,test,timeout=90*mydb`},
{`mymy:///var/run/mysqld/mysqld.sock/mydb?timeout=90`, `mymysql`, `unix:/var/run/mysqld/mysqld.sock,timeout=90*mydb`},
{`mymy+unix:user:pass@mysqld.sock?timeout=90`, `mymysql`, `unix:mysqld.sock,timeout=90*/user/pass`},

{`mssql://`, `mssql`, ``}, // 22
{`mssql://user:pass@localhost/dbname`, `mssql`, `Database=dbname;Password=pass;Server=localhost;User ID=user`},
{`mssql://user@localhost/service/dbname`, `mssql`, `Database=dbname;Server=localhost\service;User ID=user`},
{`mssql://user:!234%23$@localhost:1580/dbname`, `mssql`, `Database=dbname;Password=!234#$;Port=1580;Server=localhost;User ID=user`},

{`adodb://Microsoft.ACE.OLEDB.12.0?Extended+Properties=%22Text%3BHDR%3DNO%3BFMT%3DDelimited%22`, `adodb`,
`Data Source=.;Extended Properties="Text;HDR=NO;FMT=Delimited";Provider=Microsoft.ACE.OLEDB.12.0`}, // 26

{`oo+Postgres+Unicode://user:pass@host:5432/dbname`, `adodb`,
`Provider=MSDASQL.1;Extended Properties="Database=dbname;Driver={Postgres Unicode};PWD=pass;Port=5432;Server=host;UID=user"`}, // 27
}

for i, test := range tests {
Expand Down
Loading

0 comments on commit eebe57e

Please sign in to comment.