-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoada_test.go
89 lines (78 loc) · 2.18 KB
/
goada_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
package goada
import (
"fmt"
"net/url"
"testing"
)
func compareString(t *testing.T, expected, actual, message string) {
if expected != actual {
t.Errorf("Expected %s, but got %s. %s", expected, actual, message)
}
}
func TestBadUrl(t *testing.T) {
url, err := New("some bad url")
if err == nil {
t.Error("Expected error")
}
if url != nil {
t.Error("Expected no URL")
}
}
func TestGoodUrl(t *testing.T) {
url, err := New("https://www.GOogle.com")
if err != nil {
t.Error("Expected no error")
}
fmt.Println(url.Href())
compareString(t, "https://www.google.com/", url.Href(), "Expected normalized url")
compareString(t, "https:", url.Protocol(), "Expected https protocol")
}
func TestGoodUrlSet(t *testing.T) {
url, err := New("https://www.GOogle.com")
if err != nil {
t.Error("Expected no error")
}
fmt.Println(url.Href())
compareString(t, "https://www.google.com/", url.Href(), "Expected normalized url")
compareString(t, "https:", url.Protocol(), "Expected https protocol")
url.SetProtocol("http:")
compareString(t, "http:", url.Protocol(), "Expected http protocol")
url.SetHash("goada")
fmt.Println(url.Hash())
compareString(t, "#goada", url.Hash(), "Expected goada hash")
fmt.Println(url.Href())
compareString(t, "http://www.google.com/#goada", url.Href(), "Expected normalized url")
}
// go test -bench Benchmark -run -
func BenchmarkSillyAda(b *testing.B) {
for j := 0; j < b.N; j++ {
_, err := New("https://www.Googlé.com")
if err != nil {
break
}
}
}
func TestStandard(t *testing.T) {
s1 := "https:// www.GOoglé.com/./path/../path2/"
url, err := New(s1)
if err != nil {
t.Error("Expected no error")
}
fmt.Println(url.Href())
compareString(t, "https://www.xn--googl-fsa.com/path2/", url.Href(), "Expected normalized url")
url.Free()
}
func TestStandardGP(t *testing.T) {
s1 := "https:// www.GOoglé.com"
_, err := url.Parse(s1)
if err == nil {
t.Error("Go url should fail")
}
s2 := "https://www.GOoglé.com/./path/../path2/"
url, err2 := url.Parse(s2)
if err2 != nil {
t.Error("Go url should hot fail")
}
fmt.Println(url.String())
compareString(t, "https://www.GOogl%C3%A9.com/./path/../path2/", url.String(), "Expected invalid normalized url")
}