-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils_test.go
137 lines (118 loc) · 2.88 KB
/
utils_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
package scrape
import (
"bytes"
"net/url"
"reflect"
"testing"
)
func Test_extractLinksFromHTML(t *testing.T) {
cases := []struct {
rawHTML string
sourceURL string
expectedLinks []string
failedURLs []string
}{
{
rawHTML: `<!DOCTYPE html>
<html>
<body>
<a href="http://www.test.com">This is a link</a>
<a href="http://www.test.com/page/1">This is a Page</a>
<a href="http://www.test.com/page/2/edit">This is a Page Edit</a>
<a href="http://www.test.com/?query=test">This is a query</a>
<a href="/page/?query=test">This is a query</a>
<a href="/page/?query1=test1#Hash">This is a query</a>
<a href="#">This is a query</a>
</body>
</html>`,
sourceURL: "http://www.test.com",
expectedLinks: []string{
"http://www.test.com",
"http://www.test.com/page/1",
"http://www.test.com/page/2/edit",
"http://www.test.com/?query=test",
"http://www.test.com/page/?query=test",
"http://www.test.com/page/?query1=test1",
},
failedURLs: []string{
"#",
},
},
}
for _, c := range cases {
sourceURL, err := url.Parse(c.sourceURL)
if err != nil {
t.Fatal(err)
}
urls, failedURLs := extractURLsFromHTML(sourceURL, bytes.NewReader([]byte(c.rawHTML)))
var strURLs []string
for _, u := range urls {
strURLs = append(strURLs, u.String())
}
if !reflect.DeepEqual(c.expectedLinks, strURLs) {
t.Fatalf("expected resolved urls %v but got %v", c.expectedLinks, urls)
}
if !reflect.DeepEqual(c.failedURLs, failedURLs) {
t.Fatalf("expected failed urls %v but got %v", c.failedURLs, failedURLs)
}
}
}
func Test_resolveURL(t *testing.T) {
cases := []struct {
sourceURL string
href string
resolvedURL string
error bool
}{
{
sourceURL: "http://www.test.com",
href: "/page/1",
resolvedURL: "http://www.test.com/page/1",
error: false,
},
{
sourceURL: "https://www.test.com",
href: "/page/2",
resolvedURL: "https://www.test.com/page/2",
error: false,
},
{
sourceURL: "",
href: "page",
error: true,
},
{
sourceURL: "www.test.com",
href: "page",
error: true,
},
{
sourceURL: "https://www.test.com",
href: "https://www.test2.com/page/2",
resolvedURL: "https://www.test2.com/page/2",
error: false,
},
{
sourceURL: "http://www.test.com",
href: "https://www.test2.com/page/2",
resolvedURL: "https://www.test2.com/page/2",
error: false,
},
}
for _, c := range cases {
sourceURL, err := url.Parse(c.sourceURL)
if err != nil {
t.Fatal(err)
}
resolvedURL, err := resolveURL(sourceURL, c.href)
if err != nil {
if c.error {
continue
}
t.Fatalf("failed to resolve URL %v from source url %s\n", c.href, c.sourceURL)
}
if c.resolvedURL != resolvedURL.String() {
t.Fatalf("expected url %s but got %s\n", c.resolvedURL, resolvedURL.String())
}
}
}