1 | n/a | import unittest |
---|
2 | n/a | from test import support |
---|
3 | n/a | |
---|
4 | n/a | import contextlib |
---|
5 | n/a | import socket |
---|
6 | n/a | import urllib.request |
---|
7 | n/a | import os |
---|
8 | n/a | import email.message |
---|
9 | n/a | import time |
---|
10 | n/a | |
---|
11 | n/a | |
---|
12 | n/a | support.requires('network') |
---|
13 | n/a | |
---|
14 | n/a | class URLTimeoutTest(unittest.TestCase): |
---|
15 | n/a | # XXX this test doesn't seem to test anything useful. |
---|
16 | n/a | |
---|
17 | n/a | TIMEOUT = 30.0 |
---|
18 | n/a | |
---|
19 | n/a | def setUp(self): |
---|
20 | n/a | socket.setdefaulttimeout(self.TIMEOUT) |
---|
21 | n/a | |
---|
22 | n/a | def tearDown(self): |
---|
23 | n/a | socket.setdefaulttimeout(None) |
---|
24 | n/a | |
---|
25 | n/a | def testURLread(self): |
---|
26 | n/a | with support.transient_internet("www.example.com"): |
---|
27 | n/a | f = urllib.request.urlopen("http://www.example.com/") |
---|
28 | n/a | x = f.read() |
---|
29 | n/a | |
---|
30 | n/a | |
---|
31 | n/a | class urlopenNetworkTests(unittest.TestCase): |
---|
32 | n/a | """Tests urllib.reqest.urlopen using the network. |
---|
33 | n/a | |
---|
34 | n/a | These tests are not exhaustive. Assuming that testing using files does a |
---|
35 | n/a | good job overall of some of the basic interface features. There are no |
---|
36 | n/a | tests exercising the optional 'data' and 'proxies' arguments. No tests |
---|
37 | n/a | for transparent redirection have been written. |
---|
38 | n/a | |
---|
39 | n/a | setUp is not used for always constructing a connection to |
---|
40 | n/a | http://www.pythontest.net/ since there a few tests that don't use that address |
---|
41 | n/a | and making a connection is expensive enough to warrant minimizing unneeded |
---|
42 | n/a | connections. |
---|
43 | n/a | |
---|
44 | n/a | """ |
---|
45 | n/a | |
---|
46 | n/a | url = 'http://www.pythontest.net/' |
---|
47 | n/a | |
---|
48 | n/a | @contextlib.contextmanager |
---|
49 | n/a | def urlopen(self, *args, **kwargs): |
---|
50 | n/a | resource = args[0] |
---|
51 | n/a | with support.transient_internet(resource): |
---|
52 | n/a | r = urllib.request.urlopen(*args, **kwargs) |
---|
53 | n/a | try: |
---|
54 | n/a | yield r |
---|
55 | n/a | finally: |
---|
56 | n/a | r.close() |
---|
57 | n/a | |
---|
58 | n/a | def test_basic(self): |
---|
59 | n/a | # Simple test expected to pass. |
---|
60 | n/a | with self.urlopen(self.url) as open_url: |
---|
61 | n/a | for attr in ("read", "readline", "readlines", "fileno", "close", |
---|
62 | n/a | "info", "geturl"): |
---|
63 | n/a | self.assertTrue(hasattr(open_url, attr), "object returned from " |
---|
64 | n/a | "urlopen lacks the %s attribute" % attr) |
---|
65 | n/a | self.assertTrue(open_url.read(), "calling 'read' failed") |
---|
66 | n/a | |
---|
67 | n/a | def test_readlines(self): |
---|
68 | n/a | # Test both readline and readlines. |
---|
69 | n/a | with self.urlopen(self.url) as open_url: |
---|
70 | n/a | self.assertIsInstance(open_url.readline(), bytes, |
---|
71 | n/a | "readline did not return a string") |
---|
72 | n/a | self.assertIsInstance(open_url.readlines(), list, |
---|
73 | n/a | "readlines did not return a list") |
---|
74 | n/a | |
---|
75 | n/a | def test_info(self): |
---|
76 | n/a | # Test 'info'. |
---|
77 | n/a | with self.urlopen(self.url) as open_url: |
---|
78 | n/a | info_obj = open_url.info() |
---|
79 | n/a | self.assertIsInstance(info_obj, email.message.Message, |
---|
80 | n/a | "object returned by 'info' is not an " |
---|
81 | n/a | "instance of email.message.Message") |
---|
82 | n/a | self.assertEqual(info_obj.get_content_subtype(), "html") |
---|
83 | n/a | |
---|
84 | n/a | def test_geturl(self): |
---|
85 | n/a | # Make sure same URL as opened is returned by geturl. |
---|
86 | n/a | with self.urlopen(self.url) as open_url: |
---|
87 | n/a | gotten_url = open_url.geturl() |
---|
88 | n/a | self.assertEqual(gotten_url, self.url) |
---|
89 | n/a | |
---|
90 | n/a | def test_getcode(self): |
---|
91 | n/a | # test getcode() with the fancy opener to get 404 error codes |
---|
92 | n/a | URL = self.url + "XXXinvalidXXX" |
---|
93 | n/a | with support.transient_internet(URL): |
---|
94 | n/a | with self.assertWarns(DeprecationWarning): |
---|
95 | n/a | open_url = urllib.request.FancyURLopener().open(URL) |
---|
96 | n/a | try: |
---|
97 | n/a | code = open_url.getcode() |
---|
98 | n/a | finally: |
---|
99 | n/a | open_url.close() |
---|
100 | n/a | self.assertEqual(code, 404) |
---|
101 | n/a | |
---|
102 | n/a | def test_bad_address(self): |
---|
103 | n/a | # Make sure proper exception is raised when connecting to a bogus |
---|
104 | n/a | # address. |
---|
105 | n/a | |
---|
106 | n/a | # Given that both VeriSign and various ISPs have in |
---|
107 | n/a | # the past or are presently hijacking various invalid |
---|
108 | n/a | # domain name requests in an attempt to boost traffic |
---|
109 | n/a | # to their own sites, finding a domain name to use |
---|
110 | n/a | # for this test is difficult. RFC2606 leads one to |
---|
111 | n/a | # believe that '.invalid' should work, but experience |
---|
112 | n/a | # seemed to indicate otherwise. Single character |
---|
113 | n/a | # TLDs are likely to remain invalid, so this seems to |
---|
114 | n/a | # be the best choice. The trailing '.' prevents a |
---|
115 | n/a | # related problem: The normal DNS resolver appends |
---|
116 | n/a | # the domain names from the search path if there is |
---|
117 | n/a | # no '.' the end and, and if one of those domains |
---|
118 | n/a | # implements a '*' rule a result is returned. |
---|
119 | n/a | # However, none of this will prevent the test from |
---|
120 | n/a | # failing if the ISP hijacks all invalid domain |
---|
121 | n/a | # requests. The real solution would be to be able to |
---|
122 | n/a | # parameterize the framework with a mock resolver. |
---|
123 | n/a | bogus_domain = "sadflkjsasf.i.nvali.d." |
---|
124 | n/a | try: |
---|
125 | n/a | socket.gethostbyname(bogus_domain) |
---|
126 | n/a | except OSError: |
---|
127 | n/a | # socket.gaierror is too narrow, since getaddrinfo() may also |
---|
128 | n/a | # fail with EAI_SYSTEM and ETIMEDOUT (seen on Ubuntu 13.04), |
---|
129 | n/a | # i.e. Python's TimeoutError. |
---|
130 | n/a | pass |
---|
131 | n/a | else: |
---|
132 | n/a | # This happens with some overzealous DNS providers such as OpenDNS |
---|
133 | n/a | self.skipTest("%r should not resolve for test to work" % bogus_domain) |
---|
134 | n/a | failure_explanation = ('opening an invalid URL did not raise OSError; ' |
---|
135 | n/a | 'can be caused by a broken DNS server ' |
---|
136 | n/a | '(e.g. returns 404 or hijacks page)') |
---|
137 | n/a | with self.assertRaises(OSError, msg=failure_explanation): |
---|
138 | n/a | urllib.request.urlopen("http://{}/".format(bogus_domain)) |
---|
139 | n/a | |
---|
140 | n/a | |
---|
141 | n/a | class urlretrieveNetworkTests(unittest.TestCase): |
---|
142 | n/a | """Tests urllib.request.urlretrieve using the network.""" |
---|
143 | n/a | |
---|
144 | n/a | @contextlib.contextmanager |
---|
145 | n/a | def urlretrieve(self, *args, **kwargs): |
---|
146 | n/a | resource = args[0] |
---|
147 | n/a | with support.transient_internet(resource): |
---|
148 | n/a | file_location, info = urllib.request.urlretrieve(*args, **kwargs) |
---|
149 | n/a | try: |
---|
150 | n/a | yield file_location, info |
---|
151 | n/a | finally: |
---|
152 | n/a | support.unlink(file_location) |
---|
153 | n/a | |
---|
154 | n/a | def test_basic(self): |
---|
155 | n/a | # Test basic functionality. |
---|
156 | n/a | with self.urlretrieve(self.logo) as (file_location, info): |
---|
157 | n/a | self.assertTrue(os.path.exists(file_location), "file location returned by" |
---|
158 | n/a | " urlretrieve is not a valid path") |
---|
159 | n/a | with open(file_location, 'rb') as f: |
---|
160 | n/a | self.assertTrue(f.read(), "reading from the file location returned" |
---|
161 | n/a | " by urlretrieve failed") |
---|
162 | n/a | |
---|
163 | n/a | def test_specified_path(self): |
---|
164 | n/a | # Make sure that specifying the location of the file to write to works. |
---|
165 | n/a | with self.urlretrieve(self.logo, |
---|
166 | n/a | support.TESTFN) as (file_location, info): |
---|
167 | n/a | self.assertEqual(file_location, support.TESTFN) |
---|
168 | n/a | self.assertTrue(os.path.exists(file_location)) |
---|
169 | n/a | with open(file_location, 'rb') as f: |
---|
170 | n/a | self.assertTrue(f.read(), "reading from temporary file failed") |
---|
171 | n/a | |
---|
172 | n/a | def test_header(self): |
---|
173 | n/a | # Make sure header returned as 2nd value from urlretrieve is good. |
---|
174 | n/a | with self.urlretrieve(self.logo) as (file_location, info): |
---|
175 | n/a | self.assertIsInstance(info, email.message.Message, |
---|
176 | n/a | "info is not an instance of email.message.Message") |
---|
177 | n/a | |
---|
178 | n/a | logo = "http://www.pythontest.net/" |
---|
179 | n/a | |
---|
180 | n/a | def test_data_header(self): |
---|
181 | n/a | with self.urlretrieve(self.logo) as (file_location, fileheaders): |
---|
182 | n/a | datevalue = fileheaders.get('Date') |
---|
183 | n/a | dateformat = '%a, %d %b %Y %H:%M:%S GMT' |
---|
184 | n/a | try: |
---|
185 | n/a | time.strptime(datevalue, dateformat) |
---|
186 | n/a | except ValueError: |
---|
187 | n/a | self.fail('Date value not in %r format' % dateformat) |
---|
188 | n/a | |
---|
189 | n/a | def test_reporthook(self): |
---|
190 | n/a | records = [] |
---|
191 | n/a | def recording_reporthook(blocks, block_size, total_size): |
---|
192 | n/a | records.append((blocks, block_size, total_size)) |
---|
193 | n/a | |
---|
194 | n/a | with self.urlretrieve(self.logo, reporthook=recording_reporthook) as ( |
---|
195 | n/a | file_location, fileheaders): |
---|
196 | n/a | expected_size = int(fileheaders['Content-Length']) |
---|
197 | n/a | |
---|
198 | n/a | records_repr = repr(records) # For use in error messages. |
---|
199 | n/a | self.assertGreater(len(records), 1, msg="There should always be two " |
---|
200 | n/a | "calls; the first one before the transfer starts.") |
---|
201 | n/a | self.assertEqual(records[0][0], 0) |
---|
202 | n/a | self.assertGreater(records[0][1], 0, |
---|
203 | n/a | msg="block size can't be 0 in %s" % records_repr) |
---|
204 | n/a | self.assertEqual(records[0][2], expected_size) |
---|
205 | n/a | self.assertEqual(records[-1][2], expected_size) |
---|
206 | n/a | |
---|
207 | n/a | block_sizes = {block_size for _, block_size, _ in records} |
---|
208 | n/a | self.assertEqual({records[0][1]}, block_sizes, |
---|
209 | n/a | msg="block sizes in %s must be equal" % records_repr) |
---|
210 | n/a | self.assertGreaterEqual(records[-1][0]*records[0][1], expected_size, |
---|
211 | n/a | msg="number of blocks * block size must be" |
---|
212 | n/a | " >= total size in %s" % records_repr) |
---|
213 | n/a | |
---|
214 | n/a | |
---|
215 | n/a | if __name__ == "__main__": |
---|
216 | n/a | unittest.main() |
---|