-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_urls_from_html_test.go
95 lines (92 loc) · 1.87 KB
/
get_urls_from_html_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
package main
import (
"reflect"
"testing"
)
func TestGetURLsFromHTML(t *testing.T) {
tests := []struct {
name string
inputURL string
inputBody string
expected []string
}{
{
name: "absolute and relative URLs",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<a href="/path/one">
<span>Boot.dev</span>
</a>
<a href="https://other.com/path/one">
<span>Boot.dev</span>
</a>
</body>
</html>
`,
expected: []string{"https://blog.boot.dev/path/one", "https://other.com/path/one"},
},
{
name: "only absolute URLs",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<a href="https://example.com/path/one">
<span>Example</span>
</a>
<a href="https://example.com/path/two">
<span>Example</span>
</a>
</body>
</html>
`,
expected: []string{"https://example.com/path/one", "https://example.com/path/two"},
},
{
name: "only relative URLs",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<a href="/path/one">
<span>Boot.dev</span>
</a>
<a href="/path/two">
<span>Boot.dev</span>
</a>
</body>
</html>
`,
expected: []string{"https://blog.boot.dev/path/one", "https://blog.boot.dev/path/two"},
},
{
name: "no URLs",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<p>No links here!</p>
</body>
</html>
`,
expected: []string{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actual, err := getURLsFromHTML(tc.inputBody, tc.inputURL)
if err != nil {
t.Errorf("Test '%s' FAIL: unexpected error: %v", tc.name, err)
return
}
if len(tc.expected) == 0 && len(actual) == 0 {
return // both are empty slices, consider them equal
}
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("Test '%s' FAIL: expected URLs: %v, actual: %v", tc.name, tc.expected, actual)
}
})
}
}