2016-11-14 20:40:12 +03:00
|
|
|
// +build go1.7
|
|
|
|
|
2016-11-12 02:47:49 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseURL(t *testing.T) {
|
|
|
|
cases := []struct {
|
2016-11-15 21:27:20 +03:00
|
|
|
u string
|
|
|
|
addr string
|
|
|
|
db int
|
|
|
|
tls bool
|
|
|
|
err error
|
2016-11-12 02:47:49 +03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"redis://localhost:123/1",
|
|
|
|
"localhost:123",
|
|
|
|
1, false, nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"redis://localhost:123",
|
|
|
|
"localhost:123",
|
|
|
|
0, false, nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"redis://localhost/1",
|
|
|
|
"localhost:6379",
|
|
|
|
1, false, nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"redis://12345",
|
|
|
|
"12345:6379",
|
|
|
|
0, false, nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"rediss://localhost:123",
|
|
|
|
"localhost:123",
|
|
|
|
0, true, nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"redis://localhost/?abc=123",
|
|
|
|
"",
|
|
|
|
0, false, errors.New("no options supported"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"http://google.com",
|
|
|
|
"",
|
|
|
|
0, false, errors.New("invalid redis URL scheme: http"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"redis://localhost/1/2/3/4",
|
|
|
|
"",
|
|
|
|
0, false, errors.New("invalid redis URL path: /1/2/3/4"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"12345",
|
|
|
|
"",
|
|
|
|
0, false, errors.New("invalid redis URL scheme: "),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"redis://localhost/iamadatabase",
|
|
|
|
"",
|
2017-01-26 17:34:09 +03:00
|
|
|
0, false, errors.New(`invalid redis database number: "iamadatabase"`),
|
2016-11-12 02:47:49 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.u, func(t *testing.T) {
|
|
|
|
o, err := ParseURL(c.u)
|
|
|
|
if c.err == nil && err != nil {
|
2018-01-04 15:20:49 +03:00
|
|
|
t.Fatalf("unexpected error: %q", err)
|
2016-11-12 02:47:49 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.err != nil && err != nil {
|
|
|
|
if c.err.Error() != err.Error() {
|
2016-11-15 21:27:20 +03:00
|
|
|
t.Fatalf("got %q, expected %q", err, c.err)
|
2016-11-12 02:47:49 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if o.Addr != c.addr {
|
2016-11-15 21:27:20 +03:00
|
|
|
t.Errorf("got %q, want %q", o.Addr, c.addr)
|
2016-11-12 02:47:49 +03:00
|
|
|
}
|
|
|
|
if o.DB != c.db {
|
2016-11-15 21:27:20 +03:00
|
|
|
t.Errorf("got %q, expected %q", o.DB, c.db)
|
2016-11-12 02:47:49 +03:00
|
|
|
}
|
2016-11-15 21:27:20 +03:00
|
|
|
if c.tls && o.TLSConfig == nil {
|
|
|
|
t.Errorf("got nil TLSConfig, expected a TLSConfig")
|
2016-11-12 02:47:49 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|