-
Notifications
You must be signed in to change notification settings - Fork 0
/
puri_test.go
196 lines (183 loc) · 6.56 KB
/
puri_test.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package puri
import (
"net/url"
"testing"
)
func p[T any](t T) *T {
return &t
}
func toTestURL(uri string) url.URL {
u, e := url.Parse(uri)
if e != nil {
panic("test setup failure")
}
return *u
}
func TestExtractParam(t *testing.T) {
tests := []struct {
name string
uri url.URL
param string
want *string
}{
{
name: "case 1: valid url and param",
uri: toTestURL("http://example.com/?key=value"),
param: "key",
want: p("value"),
},
{
name: "case 2: valid url, param is missing",
uri: toTestURL("http://example.com/?key=value"),
param: "missing",
},
{
name: "case 3: invalid url, valid param",
uri: toTestURL("http:/example.com?param1=value1"),
param: "param1",
want: p("value1"),
},
{
name: "case 4: empty url and param",
uri: toTestURL(""),
param: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ExtractQuery(tt.uri, tt.param)
if err == nil {
if tt.want != nil && (*got != *tt.want) {
t.Errorf("got param: %v, want param: %v", got, tt.want)
}
} else {
if tt.want != nil {
t.Errorf("wanted param: %s, but got error: %v", *tt.want, err)
}
}
})
}
}
func TestExtractScheme(t *testing.T) {
tests := []struct {
name string
uri url.URL
wantScheme *string
wantError bool
}{
{"ftp", toTestURL("ftp://example.com"), p("ftp"), false},
{"http", toTestURL("http://example.com"), p("http"), false},
{"https", toTestURL("https://example.com"), p("https"), false},
{"none", toTestURL("empty uri"), nil, true},
{"bad", toTestURL("invalid uri"), nil, true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotScheme, err := ExtractScheme(tc.uri)
if (err != nil) != tc.wantError {
t.Errorf("got error: %v, want error: %v", err, tc.wantError)
return
}
if tc.wantScheme != nil && (*gotScheme != *tc.wantScheme) {
t.Errorf("got scheme: %v, want scheme: %v", gotScheme, tc.wantScheme)
}
})
}
}
func TestExtractHost(t *testing.T) {
tests := []struct {
name string
uri url.URL
wantHost *string
wantError bool
}{
{"ftp host", toTestURL("ftp://example.com"), p("example.com"), false},
{"http host with port", toTestURL("http://example.com:8080"), p("example.com"), false},
{"http host with port and path", toTestURL("http://example.com:8080/blah/blah?k=v"), p("example.com"), false},
{"simple", toTestURL("example.com"), p("example.com"), false},
{"simpler", toTestURL("host"), p("host"), false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotHost, err := ExtractHost(tc.uri)
if (err != nil) != tc.wantError {
t.Errorf("got error: %v, want error: %v", err, tc.wantError)
return
}
if *gotHost != *tc.wantHost {
t.Errorf("got host: %v, want host: %v", gotHost, tc.wantHost)
}
})
}
}
func TestExtractPort(t *testing.T) {
tests := []struct {
name string
uri url.URL
wantPort *string
wantError bool
}{
{"ftp host no port", toTestURL("ftp://example.com"), p(""), false},
{"http host with port", toTestURL("http://example.com:8080"), p("8080"), false},
{"http host with port and path", toTestURL("http://example.com:8080/blah/blah?k=v"), p("8080"), false},
{"simple", toTestURL("example.com:80"), p("80"), false},
{"simpler", toTestURL("host"), p(""), false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotPort, err := ExtractPort(tc.uri)
if (err != nil) != tc.wantError {
t.Errorf("got error: %v, want error: %v", err, tc.wantError)
return
}
if tc.wantPort != nil && (*gotPort != *tc.wantPort) {
t.Errorf("got port: %v, want port: %v", gotPort, tc.wantPort)
}
})
}
}
func TestExtractPath(t *testing.T) {
tests := []struct {
name string
uri url.URL
wantPath *string
wantError bool
}{
{"No Path", toTestURL(""), p(""), false},
{"No Path with port and params", toTestURL("ftp://example.com:8080?k=v"), p(""), false},
{"No Path but anchor with port and params", toTestURL("ftp://example.com:8080#?k=v"), p("#"), false},
{"No Path with port", toTestURL("ftp://example.com:8080"), p(""), false},
{"No Path with host", toTestURL("ftp://example.com"), p(""), false},
{"Path with ftp", toTestURL("ftp://example.com/path"), p("/path"), false},
{"Path with http and port", toTestURL("http://example.com:8080/path"), p("/path"), false},
{"Path with http and anchor", toTestURL("http://example.com/path#a"), p("/path#a"), false},
{"Path with https and anchor", toTestURL("https://www.google.com/path/path#a"), p("/path/path#a"), false},
{"Path with http, port and query", toTestURL("http://example.com:8080/path/blah?k=v"), p("/path/blah"), false},
{"Path with http, port and query and anchor", toTestURL("http://example.com:8080/path/blah#a?k=v"), p("/path/blah#a"), false},
{"Weird Path with http, no port and query and anchor", toTestURL("https://www.google.com/www.blah.com/?q=www.google.com2"), p("/www.blah.com/"), false},
{"Another weird Path with http, no port and query and anchor", toTestURL("https://www.google.com/blah.com?q=www.google.com2"), p("/blah.com"), false},
{"Weird Path with http, port and query and anchor", toTestURL("https://www.google.com:5432/www.blah.com/?q=www.google.com2"), p("/www.blah.com/"), false},
{"Path with tld host", toTestURL("example.com/path"), p("/path"), false},
{"Path with host and query", toTestURL("example.com/path?k=v"), p("/path"), false},
{"Path with host and anchor and query", toTestURL("example.com/path#x?k=v"), p("/path#x"), false},
{"Second Path with host and anchor and query", toTestURL("www.google.com/path/path#x?k=v"), p("/path/path#x"), false},
{"Third Path with host and anchor and query", toTestURL("www.google.com/path/path/#x?k=v"), p("/path/path/#x"), false},
{"Path with host and port", toTestURL("example.com:8080/path/a"), p("/path/a"), false},
{"Path with host and port and query", toTestURL("example.com:8080/path/a?k=v"), p("/path/a"), false},
{"Path with host and port and anchor and query", toTestURL("example.com:8080/path/a#x?k=v"), p("/path/a#x"), false},
{"Path only", toTestURL("some/path"), p("some/path"), false},
{"Absolute Path only", toTestURL("/some/path/more"), p("/some/path/more"), false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotPath, err := ExtractPath(tc.uri)
if (err != nil) != tc.wantError {
t.Errorf("got error = %v, want error %v", err, tc.wantError)
return
}
if tc.wantPath != nil && (*gotPath != *tc.wantPath) {
t.Errorf("got path: %v, want path: %v", *gotPath, *tc.wantPath)
}
})
}
}