Debian Codegraph @

test/test_html.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 from unittest import TestCase
4
5 import mechanize
6 import mechanize._form
7 from mechanize._response import test_html_response
8 from mechanize._html import content_parser, get_title, Factory
9
10
11 class RegressionTests(TestCase):
12
13 def test_close_base_tag(self):
14 # any document containing a </base> tag used to cause an exception
15 br = mechanize.Browser()
16 br.set_html("</base>")
17 list(br.links())
18
19 def test_bad_base_tag(self):
20 # a document with a base tag with no href used to cause an exception
21 br = mechanize.Browser()
22 br.set_html(
23 "<BASE TARGET='_main'><a href='http://example.com/'>eg</a>",
24 url="http://example.com/",
25 )
26 list(br.links())
27
28
29 class EncodingFinderTests(TestCase):
30
31 def make_response(self, encodings):
32 return mechanize._response.test_response(
33 headers=[("Content-type", "text/html; charset=\"%s\"" % encoding)
34 for encoding in encodings])
35
36 def test_known_encoding(self):
37 encoding_finder = mechanize._html.EncodingFinder("default")
38 response = self.make_response(["utf-8"])
39 self.assertEqual(encoding_finder.encoding(response), "utf-8")
40
41 def test_unknown_encoding(self):
42 encoding_finder = mechanize._html.EncodingFinder("default")
43 response = self.make_response(["bogus"])
44 self.assertEqual(encoding_finder.encoding(response), "default")
45
46 def test_precedence(self):
47 encoding_finder = mechanize._html.EncodingFinder("default")
48 response = self.make_response(["latin-1", "utf-8"])
49 self.assertEqual(encoding_finder.encoding(response), "latin-1")
50
51 def test_fallback(self):
52 encoding_finder = mechanize._html.EncodingFinder("default")
53 response = self.make_response(["bogus", "utf-8"])
54 self.assertEqual(encoding_finder.encoding(response), "utf-8")
55
56
57 class TitleTests(TestCase):
58
59 def test_title_parsing(self):
60 html = ("""\
61 <html><head>
62 <title> Title\n Test</title>
63 </head><body><p>Blah.<p></body></html>
64 """)
65 self.assertEqual(get_title(content_parser(html)), 'Title Test')
66
67
68 class MiscTests(TestCase):
69
70 def test_util_func(self):
71 headers1 = str(test_html_response('').info())
72 headers2 = str(test_html_response('').info())
73 self.assertEqual(headers1, headers2)
74
75 def test_link_parsing(self):
76
77 def get_first_link_text(html):
78 factory = Factory()
79 response = test_html_response(html, url="http://example.com/")
80 factory.set_response(response)
81 return list(factory.links())[0].text
82
83 html = ("""\
84 <html><head><title>Title</title></head><body>
85 <p><a href="http://example.com/">The quick\tbrown fox jumps
86 over the <i><b>lazy</b></i> dog </a>
87 </body></html>
88 """)
89 self.assertEqual(
90 get_first_link_text(html), u'The quick brown fox jumps over the lazy dog')
91
92 html = ("""\
93 <html><head><title>Title</title></head><body>
94 <p><a href="http://example.com/"></a>
95 </body></html>
96 """)
97 self.assertEqual(get_first_link_text(html), '')
98
99 html = ("""\
100 <html><head><title>Title</title></head><body>
101 <p><iframe src="http://example.com/"></iframe>
102 </body></html>
103 """)
104 self.assertEqual(get_first_link_text(html), '')
105
106 def test_title_parsing(self):
107 def get_title(html):
108 factory = Factory()
109 response = test_html_response(html)
110 factory.set_response(response)
111 return factory.title
112
113 html = (b"""\
114 <html><head>
115 <title>T&gt;itle</title>
116 </head><body><p>Blah.<p></body></html>
117 """)
118 self.assertEqual(get_title(html), u'T>itle')
119
120 html = ("""\
121 <html><head>
122 <title> Ti<script type="text/strange">alert("this is valid HTML -- yuck!")</script>
123 tle &amp;&#38;
124 </title>
125 </head><body><p>Blah.<p></body></html>
126 """)
127 self.assertEqual(
128 str(get_title(html)), 'Ti<script type="text/strange">alert("this is valid HTML -- yuck!")</script> tle &&')
129
130 html = ("""\
131 <html><head>
132 <title>""")
133 self.assertEqual(get_title(html), u'')
134
135
136 if __name__ == "__main__":
137 import unittest
138 unittest.main()