Debian Codegraph @

options_test.go

raw
1 // +build go1.7
2
3 package redis
4
5 import (
6 "errors"
7 "testing"
8 )
9
10 func TestParseURL(t *testing.T) {
11 cases := []struct {
12 u string
13 addr string
14 db int
15 tls bool
16 err error
17 }{
18 {
19 "redis://localhost:123/1",
20 "localhost:123",
21 1, false, nil,
22 },
23 {
24 "redis://localhost:123",
25 "localhost:123",
26 0, false, nil,
27 },
28 {
29 "redis://localhost/1",
30 "localhost:6379",
31 1, false, nil,
32 },
33 {
34 "redis://12345",
35 "12345:6379",
36 0, false, nil,
37 },
38 {
39 "rediss://localhost:123",
40 "localhost:123",
41 0, true, nil,
42 },
43 {
44 "redis://localhost/?abc=123",
45 "",
46 0, false, errors.New("no options supported"),
47 },
48 {
49 "http://google.com",
50 "",
51 0, false, errors.New("invalid redis URL scheme: http"),
52 },
53 {
54 "redis://localhost/1/2/3/4",
55 "",
56 0, false, errors.New("invalid redis URL path: /1/2/3/4"),
57 },
58 {
59 "12345",
60 "",
61 0, false, errors.New("invalid redis URL scheme: "),
62 },
63 {
64 "redis://localhost/iamadatabase",
65 "",
66 0, false, errors.New(`invalid redis database number: "iamadatabase"`),
67 },
68 }
69
70 for _, c := range cases {
71 t.Run(c.u, func(t *testing.T) {
72 o, err := ParseURL(c.u)
73 if c.err == nil && err != nil {
74 t.Fatalf("unexpected error: '%q'", err)
75 return
76 }
77 if c.err != nil && err != nil {
78 if c.err.Error() != err.Error() {
79 t.Fatalf("got %q, expected %q", err, c.err)
80 }
81 return
82 }
83 if o.Addr != c.addr {
84 t.Errorf("got %q, want %q", o.Addr, c.addr)
85 }
86 if o.DB != c.db {
87 t.Errorf("got %q, expected %q", o.DB, c.db)
88 }
89 if c.tls && o.TLSConfig == nil {
90 t.Errorf("got nil TLSConfig, expected a TLSConfig")
91 }
92 })
93 }
94 }