Repository navigation
Expand file tree
/
Copy pathurl_test.go
More file actions
94 lines (88 loc) · 2.37 KB
/
Copy pathurl_test.go
File metadata and controls
94 lines (88 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package dbimp_test
import (
"errors"
"strings"
"testing"
"time"
"github.com/xo/dbimp"
)
func TestParseURL(t *testing.T) {
t.Parallel()
for _, tt := range []struct {
dsn string
want error
}{
{"fake://user:pass@localhost:8093/db?tls=true", nil},
{"fake://[::1]:8093", nil},
{"other://localhost", dbimp.ErrScheme},
{"n1ql://localhost", dbimp.ErrScheme},
{"fake:opaque", dbimp.ErrInvalidValue},
// The fuzz test of the SurrealDB DSN found this host, which net/url
// reads as ":".
{"fake://::/", dbimp.ErrInvalidValue},
{"fake://[::]/", nil},
} {
_, err := dbimp.ParseURL("fake", tt.dsn)
if !errors.Is(err, tt.want) {
t.Errorf("ParseURL(%q) = %v, want %v", tt.dsn, err, tt.want)
}
}
}
func TestParseURLHidesThePassword(t *testing.T) {
t.Parallel()
_, err := dbimp.ParseURL("fake", "fake://user:secret@local host:1")
if err == nil {
t.Fatal("ParseURL took a host with a space")
}
if strings.Contains(err.Error(), "secret") {
t.Errorf("the error %q holds the password", err)
}
}
func TestNewQuery(t *testing.T) {
t.Parallel()
for _, tt := range []struct {
dsn string
want error
}{
{"fake://h?tls=true&timeout=5s", nil},
{"fake://h?other=1", dbimp.ErrUnknownKey},
{"fake://h?tls=true&tls=false", dbimp.ErrRepeatedKey},
} {
u, err := dbimp.ParseURL("fake", tt.dsn)
if err != nil {
t.Fatal(err)
}
if _, err := dbimp.NewQuery(u, "tls", "timeout", "name"); !errors.Is(err, tt.want) {
t.Errorf("NewQuery(%q) = %v, want %v", tt.dsn, err, tt.want)
}
}
}
func TestQueryValues(t *testing.T) {
t.Parallel()
u, err := dbimp.ParseURL("fake", "fake://h?tls=true&timeout=5s&n=3&name=x")
if err != nil {
t.Fatal(err)
}
q, err := dbimp.NewQuery(u, "tls", "timeout", "n", "name", "absent")
if err != nil {
t.Fatal(err)
}
if v, err := q.Bool("tls", false); err != nil || !v {
t.Errorf("Bool = %v, %v", v, err)
}
if v, err := q.Duration("timeout", 0); err != nil || v != 5*time.Second {
t.Errorf("Duration = %v, %v", v, err)
}
if v, err := q.Int("n", 0); err != nil || v != 3 {
t.Errorf("Int = %v, %v", v, err)
}
if v := q.String("name", ""); v != "x" {
t.Errorf("String = %q", v)
}
if v := q.String("absent", "def"); v != "def" || q.Has("absent") {
t.Errorf("String of a missing key = %q", v)
}
if _, err := q.Int("name", 0); !errors.Is(err, dbimp.ErrInvalidValue) {
t.Errorf("Int of %q = %v, want ErrInvalidValue", "x", err)
}
}