blob: c8166c4b2d013c6d4c07e7e45e136f3094e2f18f [file] [log] [blame]
Skip Montanaro89feabc2003-03-30 04:54:24 +00001#!/usr/bin/env python
2
3import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Skip Montanaro89feabc2003-03-30 04:54:24 +00005
6import socket
Jeremy Hylton1afc1692008-06-18 20:49:58 +00007import urllib.request
Skip Montanaro89feabc2003-03-30 04:54:24 +00008import sys
Brett Cannona71319e2003-05-14 02:18:31 +00009import os
Barry Warsaw820c1202008-06-12 04:06:45 +000010import email.message
Skip Montanaro89feabc2003-03-30 04:54:24 +000011
Christian Heimesaf98da12008-01-27 15:18:18 +000012
13def _open_with_retry(func, host, *args, **kwargs):
14 # Connecting to remote hosts is flaky. Make it more robust
15 # by retrying the connection several times.
Christian Heimes061ce7f2008-01-27 15:45:24 +000016 last_exc = None
Christian Heimesaf98da12008-01-27 15:18:18 +000017 for i in range(3):
18 try:
19 return func(host, *args, **kwargs)
Christian Heimes061ce7f2008-01-27 15:45:24 +000020 except IOError as err:
21 last_exc = err
Christian Heimesaf98da12008-01-27 15:18:18 +000022 continue
23 except:
24 raise
25 raise last_exc
26
27
Skip Montanaro89feabc2003-03-30 04:54:24 +000028class URLTimeoutTest(unittest.TestCase):
29
30 TIMEOUT = 10.0
31
32 def setUp(self):
33 socket.setdefaulttimeout(self.TIMEOUT)
34
35 def tearDown(self):
36 socket.setdefaulttimeout(None)
37
38 def testURLread(self):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000039 f = _open_with_retry(urllib.request.urlopen, "http://www.python.org/")
Skip Montanaro89feabc2003-03-30 04:54:24 +000040 x = f.read()
41
Brett Cannona71319e2003-05-14 02:18:31 +000042class urlopenNetworkTests(unittest.TestCase):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000043 """Tests urllib.reqest.urlopen using the network.
Tim Peters813cec92003-05-16 15:35:10 +000044
Brett Cannona71319e2003-05-14 02:18:31 +000045 These tests are not exhaustive. Assuming that testing using files does a
46 good job overall of some of the basic interface features. There are no
47 tests exercising the optional 'data' and 'proxies' arguments. No tests
48 for transparent redirection have been written.
Tim Peters813cec92003-05-16 15:35:10 +000049
Brett Cannona71319e2003-05-14 02:18:31 +000050 setUp is not used for always constructing a connection to
51 http://www.python.org/ since there a few tests that don't use that address
52 and making a connection is expensive enough to warrant minimizing unneeded
53 connections.
Tim Peters813cec92003-05-16 15:35:10 +000054
Brett Cannona71319e2003-05-14 02:18:31 +000055 """
56
Christian Heimesaf98da12008-01-27 15:18:18 +000057 def urlopen(self, *args):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000058 return _open_with_retry(urllib.request.urlopen, *args)
Christian Heimesaf98da12008-01-27 15:18:18 +000059
Brett Cannona71319e2003-05-14 02:18:31 +000060 def test_basic(self):
61 # Simple test expected to pass.
Christian Heimesaf98da12008-01-27 15:18:18 +000062 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000063 for attr in ("read", "readline", "readlines", "fileno", "close",
64 "info", "geturl"):
65 self.assert_(hasattr(open_url, attr), "object returned from "
66 "urlopen lacks the %s attribute" % attr)
67 try:
68 self.assert_(open_url.read(), "calling 'read' failed")
69 finally:
70 open_url.close()
71
72 def test_readlines(self):
73 # Test both readline and readlines.
Christian Heimesaf98da12008-01-27 15:18:18 +000074 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000075 try:
Jeremy Hylton3e186152007-08-04 03:46:11 +000076 self.assert_(isinstance(open_url.readline(), bytes),
77 "readline did not return bytes")
Brett Cannona71319e2003-05-14 02:18:31 +000078 self.assert_(isinstance(open_url.readlines(), list),
79 "readlines did not return a list")
80 finally:
81 open_url.close()
82
83 def test_info(self):
84 # Test 'info'.
Christian Heimesaf98da12008-01-27 15:18:18 +000085 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000086 try:
87 info_obj = open_url.info()
88 finally:
89 open_url.close()
Barry Warsaw820c1202008-06-12 04:06:45 +000090 self.assert_(isinstance(info_obj, email.message.Message),
Brett Cannona71319e2003-05-14 02:18:31 +000091 "object returned by 'info' is not an instance of "
Barry Warsaw820c1202008-06-12 04:06:45 +000092 "email.message.Message")
93 self.assertEqual(info_obj.get_content_subtype(), "html")
Brett Cannona71319e2003-05-14 02:18:31 +000094
95 def test_geturl(self):
96 # Make sure same URL as opened is returned by geturl.
97 URL = "http://www.python.org/"
Christian Heimesaf98da12008-01-27 15:18:18 +000098 open_url = self.urlopen(URL)
Brett Cannona71319e2003-05-14 02:18:31 +000099 try:
100 gotten_url = open_url.geturl()
101 finally:
102 open_url.close()
103 self.assertEqual(gotten_url, URL)
104
Christian Heimes9bd667a2008-01-20 15:14:11 +0000105 def test_getcode(self):
106 # test getcode() with the fancy opener to get 404 error codes
107 URL = "http://www.python.org/XXXinvalidXXX"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000108 open_url = urllib.request.FancyURLopener().open(URL)
Christian Heimes9bd667a2008-01-20 15:14:11 +0000109 try:
110 code = open_url.getcode()
111 finally:
112 open_url.close()
113 self.assertEqual(code, 404)
114
Brett Cannona71319e2003-05-14 02:18:31 +0000115 def test_fileno(self):
Tim Petersf545baa2003-06-15 23:26:30 +0000116 if (sys.platform in ('win32',) or
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000117 not hasattr(os, 'fdopen')):
Tim Peters813cec92003-05-16 15:35:10 +0000118 # On Windows, socket handles are not file descriptors; this
119 # test can't pass on Windows.
120 return
Brett Cannona71319e2003-05-14 02:18:31 +0000121 # Make sure fd returned by fileno is valid.
Christian Heimesaf98da12008-01-27 15:18:18 +0000122 open_url = self.urlopen("http://www.python.org/")
Tim Peters813cec92003-05-16 15:35:10 +0000123 fd = open_url.fileno()
Neal Norwitz6ae6ad82008-03-26 04:23:27 +0000124 # XXX(nnorwitz): There is currently no way to pass errors, encoding,
125 # etc to fdopen. :-(
Tim Peters813cec92003-05-16 15:35:10 +0000126 FILE = os.fdopen(fd)
Neal Norwitz6ae6ad82008-03-26 04:23:27 +0000127 FILE._errors = 'ignore'
Tim Peters813cec92003-05-16 15:35:10 +0000128 try:
129 self.assert_(FILE.read(), "reading from file created using fd "
130 "returned by fileno failed")
131 finally:
132 FILE.close()
Brett Cannona71319e2003-05-14 02:18:31 +0000133
134 def test_bad_address(self):
135 # Make sure proper exception is raised when connecting to a bogus
136 # address.
137 self.assertRaises(IOError,
Tim Peters0aab0022003-09-20 22:16:26 +0000138 # SF patch 809915: In Sep 2003, VeriSign started
139 # highjacking invalid .com and .net addresses to
140 # boost traffic to their own site. This test
141 # started failing then. One hopes the .invalid
142 # domain will be spared to serve its defined
143 # purpose.
144 # urllib.urlopen, "http://www.sadflkjsasadf.com/")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000145 urllib.request.urlopen,
146 "http://www.python.invalid./")
Brett Cannona71319e2003-05-14 02:18:31 +0000147
148class urlretrieveNetworkTests(unittest.TestCase):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000149 """Tests urllib.request.urlretrieve using the network."""
Brett Cannona71319e2003-05-14 02:18:31 +0000150
Christian Heimesaf98da12008-01-27 15:18:18 +0000151 def urlretrieve(self, *args):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000152 return _open_with_retry(urllib.request.urlretrieve, *args)
Christian Heimesaf98da12008-01-27 15:18:18 +0000153
Brett Cannona71319e2003-05-14 02:18:31 +0000154 def test_basic(self):
155 # Test basic functionality.
Christian Heimesaf98da12008-01-27 15:18:18 +0000156 file_location,info = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000157 self.assert_(os.path.exists(file_location), "file location returned by"
158 " urlretrieve is not a valid path")
Neal Norwitz6ae6ad82008-03-26 04:23:27 +0000159 FILE = open(file_location, errors='ignore')
Brett Cannona71319e2003-05-14 02:18:31 +0000160 try:
Jeremy Hyltonbd9f5202003-07-17 16:31:00 +0000161 self.assert_(FILE.read(), "reading from the file location returned"
162 " by urlretrieve failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000163 finally:
164 FILE.close()
165 os.unlink(file_location)
166
167 def test_specified_path(self):
168 # Make sure that specifying the location of the file to write to works.
Christian Heimesaf98da12008-01-27 15:18:18 +0000169 file_location,info = self.urlretrieve("http://www.python.org/",
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000170 support.TESTFN)
171 self.assertEqual(file_location, support.TESTFN)
Brett Cannona71319e2003-05-14 02:18:31 +0000172 self.assert_(os.path.exists(file_location))
Neal Norwitz6ae6ad82008-03-26 04:23:27 +0000173 FILE = open(file_location, errors='ignore')
Brett Cannona71319e2003-05-14 02:18:31 +0000174 try:
175 self.assert_(FILE.read(), "reading from temporary file failed")
176 finally:
177 FILE.close()
178 os.unlink(file_location)
179
180 def test_header(self):
181 # Make sure header returned as 2nd value from urlretrieve is good.
Christian Heimesaf98da12008-01-27 15:18:18 +0000182 file_location, header = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000183 os.unlink(file_location)
Barry Warsaw820c1202008-06-12 04:06:45 +0000184 self.assert_(isinstance(header, email.message.Message),
185 "header is not an instance of email.message.Message")
Tim Peters813cec92003-05-16 15:35:10 +0000186
Brett Cannona71319e2003-05-14 02:18:31 +0000187
188
Skip Montanaro89feabc2003-03-30 04:54:24 +0000189def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000190 support.requires('network')
191 support.run_unittest(URLTimeoutTest,
Brett Cannona71319e2003-05-14 02:18:31 +0000192 urlopenNetworkTests,
193 urlretrieveNetworkTests)
Skip Montanaro89feabc2003-03-30 04:54:24 +0000194
195if __name__ == "__main__":
196 test_main()