-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextractlinks_test.go
63 lines (54 loc) · 1.33 KB
/
extractlinks_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
package extractlinks
import (
"strings"
"testing"
)
func TestAll(t *testing.T) {
htmlBody := strings.NewReader(`
<html>
<body>
<h1>Hello!</h1>
<a href="/another-page">
A link to
<span>internal page</span>
</a>
<a href="https://ajinkya.com/about/">Link with slash</a>
<a href="https://ajinkya.com/about">Same link without slash</a>
<a href="https://youtube.com/jsfunc/">A link to external page</a>
</body>
</html>
`)
links, err := All(htmlBody)
if err != nil {
t.Error("Err should be nil")
}
if len(links) != 3 {
t.Error("Links count should be 3, we are removing duplicates")
}
for i, link := range links {
if i == 0 {
if link.Href != "/another-page" {
t.Error("Anchor link href is invalid")
}
if link.Text != "A link to internal page" {
t.Error("Anchor link text is invalid")
}
}
if i == 1 {
//since we are removing trailing slash
if link.Href != "https://ajinkya.com/about" {
t.Error("Anchor link href is invalid")
}
if link.Text != "Link with slash" {
t.Error("Anchor link text is invalid")
}
}
}
}
func TestRemoveTrailingSlash(t *testing.T) {
href := "https://ajinkya.js.org/about/"
cleanHref := removeTrailingSlash(href)
if strings.Compare(cleanHref, "https://ajinkya.js.org/about") != 0 {
t.Error("Trailing slash should be removed")
}
}