-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_hyperlinks.py
More file actions
122 lines (102 loc) · 4.96 KB
/
Copy pathtest_hyperlinks.py
File metadata and controls
122 lines (102 loc) · 4.96 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
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
import gumbocy
from test_word_groups import TAGS_SEPARATORS
def _links(html, url=None):
parser = gumbocy.HTMLParser(options={
"tags_separators": TAGS_SEPARATORS
})
parser.parse(html)
ret = parser.analyze(url=url)
return {
"all": ret["internal_hyperlinks"] + ret["external_hyperlinks"],
"internal": ret["internal_hyperlinks"],
"external": ret["external_hyperlinks"]
}
def test_get_hyperlinks():
links = _links("""<html><head><title>Test title</title></head><body>x</body></html>""")
assert len(links["all"]) == 0
links = _links("""<html><head><title>Test title</title></head><body>
<a name="x">Y</a>
</body></html>""")
assert len(links["all"]) == 0
links = _links("""<html><head><title>Test title</title></head><body>
<a href="">Y</a>
</body></html>""")
assert len(links["all"]) == 0
links = _links("""<html><head><title>Test title</title></head><body>
<a href="ftp://test.com">Y</a>
</body></html>""")
assert len(links["all"]) == 0
links = _links("""<html><head><title>Test title</title></head><body>
<a href="javascript:hello()">Y</a>
</body></html>""")
assert len(links["all"]) == 0
links = _links("""<html><head><title>Test title</title></head><body>
<a href="mailto:contact@example.com">Y</a>
</body></html>""")
assert len(links["all"]) == 0
links = _links("""<html><head><title>Test title</title></head><body>
<a href="http://sub.test.com/page1?q=2&a=b#xxx" rel="nofollow">Y</a>
</body></html>""")
assert len(links["all"]) == 1
assert links["external"][0][0] == "http://sub.test.com/page1?q=2&a=b#xxx"
assert links["external"][0][1] == "Y"
assert links["external"][0][2] == "nofollow"
links = _links("""<html><head><title>Test title</title></head><body>
<a href="/page1?q=2&a=b#xxx">Y X</a>
</body></html>""", url="http://sub.test.com/page2")
assert len(links["all"]) == 1
assert links["internal"][0][0] == "/page1?q=2&a=b#xxx"
assert links["internal"][0][1] == "Y X"
assert links["internal"][0][2] is None
links = _links("""<html><head><title>Test title</title></head><body>
<a href="../page1?q=2&a=b#xxx">Y Z</a>
</body></html>""", url="http://sub.test.com/page2/x.html")
assert len(links["all"]) == 1
assert links["internal"][0][0] == "../page1?q=2&a=b#xxx"
assert links["internal"][0][1] == "Y Z"
# Absolute links to the same netloc are still internal
links = _links("""<html><head><title>Test title</title></head><body>
<a href="http://sub.test.com/page1?q=2&a=b#xxx">Y Z</a>
</body></html>""", url="http://sub.test.com/page2/x.html")
assert len(links["all"]) == 1
assert len(links["external"]) == 0
assert links["internal"][0][0] == "/page1?q=2&a=b#xxx"
assert links["internal"][0][1] == "Y Z"
# Cross-scheme links are still considered internal
links = _links("""<html><head><title>Test title</title></head><body>
<a href="https://sub.test.com/page1?q=2&a=b#xxx">Y Z</a>
</body></html>""", url="http://sub.test.com/page2/x.html")
assert len(links["all"]) == 1
assert links["internal"][0][0] == "/page1?q=2&a=b#xxx"
assert links["internal"][0][1] == "Y Z"
links = _links("""<html><head><title>Test title</title></head><body>
<a href="http://sub.test.com/page1?q=2&a=b#xxx">Y Z</a>
</body></html>""", url="https://sub.test.com/page2/x.html")
assert len(links["all"]) == 1
assert links["internal"][0][0] == "/page1?q=2&a=b#xxx"
assert links["internal"][0][1] == "Y Z"
links = _links("""<html><head><title>Test title</title></head><body>
<a href="http://sub.test.com/sub.test.com/page1?q=2&a=b#xxx">Y Z</a>
</body></html>""", url="http://sub.test.com/page2/x.html")
assert len(links["all"]) == 1
assert links["internal"][0][0] == "/sub.test.com/page1?q=2&a=b#xxx"
assert links["internal"][0][1] == "Y Z"
links = _links("""<html><head><title>Test title</title></head><body>
<a href="//sub.test.com/page1?q=2&a=b#xxx">Y Z</a>
</body></html>""", url="http://sub.test.com/page2/x.html")
assert len(links["all"]) == 1
assert links["internal"][0][0] == "/page1?q=2&a=b#xxx"
assert links["internal"][0][1] == "Y Z"
links = _links("""<html><head><title>Test title</title></head><body>
<a href="//sub2.test.com/page1?q=2&a=b#xxx">Y Z</a>
</body></html>""", url="http://sub.test.com/page2/x.html")
assert len(links["all"]) == 1
assert links["external"][0][0] == "http://sub2.test.com/page1?q=2&a=b#xxx"
assert links["external"][0][1] == "Y Z"
links = _links("""<html><head><title>Test title</title></head><body>
<a href="https://sub2.test.com/page1?q=2&a=b#xxx">Y Z</a>
</body></html>""", url="http://sub.test.com/page2/x.html")
assert len(links["all"]) == 1
assert links["external"][0][0] == "https://sub2.test.com/page1?q=2&a=b#xxx"
assert links["external"][0][1] == "Y Z"
# TODO resolution tests