-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetURLs_test.go
More file actions
74 lines (71 loc) · 1.61 KB
/
Copy pathgetURLs_test.go
File metadata and controls
74 lines (71 loc) · 1.61 KB
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
package main
import (
"errors"
"reflect"
"testing"
)
func TestGetURLsFromHTML(t *testing.T) {
cases := []struct {
name string
inputURL string
inputBody string
expected []string
expectedErr error
}{
{
name: "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"},
expectedErr: nil,
},
{
name: "invalid html body",
inputURL: "https://blog.boot.dev",
inputBody: `
<html></>
`,
expected: []string{},
expectedErr: errors.New("error"),
},
{
name: "absolute URLs",
inputURL: "https://blog.boot.dev",
inputBody: `
<html>
<body>
<a href="https://one.com/path/one">
<span>Boot.dev</span>
</a>
<a href="https://two.com/path/two">
<span>Boot.dev</span>
</a>
</body>
</html>
`,
expected: []string{"https://one.com/path/one", "https://two.com/path/two"},
expectedErr: nil,
},
}
for i, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
actual, err := GetURLsFromHTML(tc.inputBody, tc.inputURL)
if err != nil && tc.expectedErr == nil {
t.Fatalf("Test %v - %s FAIL: expected error: %v, actual error: %v", i, tc.name, tc.expectedErr, err)
} else if !reflect.DeepEqual(actual, tc.expected) {
t.Fatalf("Test %v - %s FAIL: expected: %v, actual: %v", i, tc.name, tc.expected, actual)
}
})
}
}