-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_links_test.go
More file actions
88 lines (67 loc) · 1.83 KB
/
extract_links_test.go
File metadata and controls
88 lines (67 loc) · 1.83 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestExtractLinks_SimpleAnchors(t *testing.T) {
html := `<html><body><a href="https://example.com">Example</a></body></html>`
links, err := ExtractLinks(html)
assert.NoError(t, err)
assert.Equal(t, []string{"https://example.com"}, links)
}
func TestExtractLinks_MultipleAnchors(t *testing.T) {
html := `
<html>
<body>
<a href="https://foo.com">Foo</a>
<a href="https://bar.com">Bar</a>
</body>
</html>
`
links, err := ExtractLinks(html)
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"https://foo.com", "https://bar.com"}, links)
}
func TestExtractLinks_NoAnchors(t *testing.T) {
html := `<html><body><p>No links here!</p></body></html>`
links, err := ExtractLinks(html)
assert.NoError(t, err)
assert.Empty(t, links)
}
func TestExtractLinks_InvalidHTML(t *testing.T) {
html := `<html><body><a href="incomplete`
links, err := ExtractLinks(html)
assert.NoError(t, err)
assert.Empty(t, links)
}
func TestExtractLinks_AnchorWithoutHref(t *testing.T) {
html := `<html><body><a>No href here</a></body></html>`
links, err := ExtractLinks(html)
assert.NoError(t, err)
assert.Empty(t, links)
}
func TestExtractLinks_ComplexHTML(t *testing.T) {
html := `
<html>
<head>
<title>Test Page</title>
<link rel="stylesheet" href="https://example.com/style.css">
</head>
<body>
<a href="https://example.com/page1">Page 1</a>
<a href="/page2">Page 2</a>
<a href="https://example.com/page3#section">Page 3 Section</a>
<a href="javascript:void(0)">No link</a>
</body>
</html>
`
expectedLinks := []string{
"https://example.com/page1",
"/page2",
"https://example.com/page3#section",
"javascript:void(0)",
}
links, err := ExtractLinks(html)
assert.NoError(t, err)
assert.ElementsMatch(t, expectedLinks, links)
}