-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize_url_test.go
56 lines (52 loc) · 1.29 KB
/
normalize_url_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
package main
import (
"testing"
"strings"
)
func TestNormalizeURL(t *testing.T){
tests := []struct {
name string
inputURL string
expected string
errorContains string
}{
{
name: "remove scheme",
inputURL: "https://blog.boot.dev/path",
expected: "blog.boot.dev/path",
},
{
name: "remove trailing slash",
inputURL: "https://blog.boot.dev/path/",
expected: "blog.boot.dev/path",
},
{
name: "lowercase capital letters",
inputURL: "https://BLOG.boot.dev/PATH",
expected: "blog.boot.dev/path",
},
{
name: "remove scheme and capitals and trailing slash",
inputURL: "http://BLOG.boot.dev/path/",
expected: "blog.boot.dev/path",
},
{
name: "handle invalid URL",
inputURL: `:\\invalidURL`,
expected: "",
errorContains: "couldn't parse url",
},
}
for i, tc := range tests{
t.Run(tc.name, func (t *testing.T){
actual, err := normalizeURL(tc.inputURL)
if err != nil && !strings.Contains(err.Error(), tc.errorContains){
t.Errorf("Test %v - '%s' FAIL: unexpected error: %v", i, tc.name, err)
return
}
if actual != tc.expected {
t.Errorf("Test %v - %s FAIL: expected URL: %v, actual: %v", i, tc.name, tc.expected, actual)
}
})
}
}