blob: 4ce7a9b45ed3c39d6174805c9fdbdfe26b977824 [file] [log] [blame]
Skip Montanaro89feabc2003-03-30 04:54:24 +00001#!/usr/bin/env python
2
3import unittest
4from test import test_support
5
6import socket
Brett Cannona71319e2003-05-14 02:18:31 +00007import urllib
Skip Montanaro89feabc2003-03-30 04:54:24 +00008import sys
Brett Cannona71319e2003-05-14 02:18:31 +00009import os
Senthil Kumaran1b7f9e52010-05-01 08:01:56 +000010import time
11
Ezio Melottia2d46532010-01-30 07:22:54 +000012mimetools = test_support.import_module("mimetools", deprecated=True)
Skip Montanaro89feabc2003-03-30 04:54:24 +000013
Neal Norwitz5be30672008-01-26 05:54:48 +000014
15def _open_with_retry(func, host, *args, **kwargs):
16 # Connecting to remote hosts is flaky. Make it more robust
17 # by retrying the connection several times.
18 for i in range(3):
19 try:
20 return func(host, *args, **kwargs)
21 except IOError, last_exc:
22 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):
Neal Norwitz5be30672008-01-26 05:54:48 +000039 f = _open_with_retry(urllib.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):
43 """Tests urllib.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
Neal Norwitz5be30672008-01-26 05:54:48 +000057 def urlopen(self, *args):
58 return _open_with_retry(urllib.urlopen, *args)
59
Brett Cannona71319e2003-05-14 02:18:31 +000060 def test_basic(self):
61 # Simple test expected to pass.
Neal Norwitz5be30672008-01-26 05:54:48 +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"):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000065 self.assertTrue(hasattr(open_url, attr), "object returned from "
Brett Cannona71319e2003-05-14 02:18:31 +000066 "urlopen lacks the %s attribute" % attr)
67 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +000068 self.assertTrue(open_url.read(), "calling 'read' failed")
Brett Cannona71319e2003-05-14 02:18:31 +000069 finally:
70 open_url.close()
71
72 def test_readlines(self):
73 # Test both readline and readlines.
Neal Norwitz5be30672008-01-26 05:54:48 +000074 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000075 try:
Ezio Melottib0f5adc2010-01-24 16:58:36 +000076 self.assertIsInstance(open_url.readline(), basestring,
77 "readline did not return a string")
78 self.assertIsInstance(open_url.readlines(), list,
79 "readlines did not return a list")
Brett Cannona71319e2003-05-14 02:18:31 +000080 finally:
81 open_url.close()
82
83 def test_info(self):
84 # Test 'info'.
Neal Norwitz5be30672008-01-26 05:54:48 +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()
Ezio Melottib0f5adc2010-01-24 16:58:36 +000090 self.assertIsInstance(info_obj, mimetools.Message,
91 "object returned by 'info' is not an "
92 "instance of mimetools.Message")
Brett Cannona71319e2003-05-14 02:18:31 +000093 self.assertEqual(info_obj.getsubtype(), "html")
94
95 def test_geturl(self):
96 # Make sure same URL as opened is returned by geturl.
97 URL = "http://www.python.org/"
Neal Norwitz5be30672008-01-26 05:54:48 +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
Georg Brandl9b0d46d2008-01-20 11:43:03 +0000105 def test_getcode(self):
106 # test getcode() with the fancy opener to get 404 error codes
107 URL = "http://www.python.org/XXXinvalidXXX"
108 open_url = urllib.FancyURLopener().open(URL)
109 try:
110 code = open_url.getcode()
111 finally:
112 open_url.close()
113 self.assertEqual(code, 404)
114
Zachary Ware1f702212013-12-10 14:09:20 -0600115 @unittest.skipIf(sys.platform in ('win32',), 'not appropriate for Windows')
116 @unittest.skipUnless(hasattr(os, 'fdopen'), 'os.fdopen not available')
Brett Cannona71319e2003-05-14 02:18:31 +0000117 def test_fileno(self):
118 # Make sure fd returned by fileno is valid.
Neal Norwitz5be30672008-01-26 05:54:48 +0000119 open_url = self.urlopen("http://www.python.org/")
Tim Peters813cec92003-05-16 15:35:10 +0000120 fd = open_url.fileno()
121 FILE = os.fdopen(fd)
122 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000123 self.assertTrue(FILE.read(), "reading from file created using fd "
Tim Peters813cec92003-05-16 15:35:10 +0000124 "returned by fileno failed")
125 finally:
126 FILE.close()
Brett Cannona71319e2003-05-14 02:18:31 +0000127
128 def test_bad_address(self):
129 # Make sure proper exception is raised when connecting to a bogus
130 # address.
Antoine Pitroua4d58d22011-07-08 19:14:19 +0200131 bogus_domain = "sadflkjsasf.i.nvali.d"
132 try:
133 socket.gethostbyname(bogus_domain)
134 except socket.gaierror:
135 pass
136 else:
137 # This happens with some overzealous DNS providers such as OpenDNS
138 self.skipTest("%r should not resolve for test to work" % bogus_domain)
Brett Cannona71319e2003-05-14 02:18:31 +0000139 self.assertRaises(IOError,
Tim Peters0aab0022003-09-20 22:16:26 +0000140 # SF patch 809915: In Sep 2003, VeriSign started
141 # highjacking invalid .com and .net addresses to
142 # boost traffic to their own site. This test
143 # started failing then. One hopes the .invalid
144 # domain will be spared to serve its defined
145 # purpose.
146 # urllib.urlopen, "http://www.sadflkjsasadf.com/")
Antoine Pitroucc5b64a2008-12-15 00:39:51 +0000147 urllib.urlopen, "http://sadflkjsasf.i.nvali.d/")
Brett Cannona71319e2003-05-14 02:18:31 +0000148
149class urlretrieveNetworkTests(unittest.TestCase):
150 """Tests urllib.urlretrieve using the network."""
151
Neal Norwitz5be30672008-01-26 05:54:48 +0000152 def urlretrieve(self, *args):
153 return _open_with_retry(urllib.urlretrieve, *args)
154
Brett Cannona71319e2003-05-14 02:18:31 +0000155 def test_basic(self):
156 # Test basic functionality.
Neal Norwitz5be30672008-01-26 05:54:48 +0000157 file_location,info = self.urlretrieve("http://www.python.org/")
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000158 self.assertTrue(os.path.exists(file_location), "file location returned by"
Brett Cannona71319e2003-05-14 02:18:31 +0000159 " urlretrieve is not a valid path")
160 FILE = file(file_location)
161 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000162 self.assertTrue(FILE.read(), "reading from the file location returned"
Jeremy Hyltonbd9f5202003-07-17 16:31:00 +0000163 " by urlretrieve failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000164 finally:
165 FILE.close()
166 os.unlink(file_location)
167
168 def test_specified_path(self):
169 # Make sure that specifying the location of the file to write to works.
Neal Norwitz5be30672008-01-26 05:54:48 +0000170 file_location,info = self.urlretrieve("http://www.python.org/",
171 test_support.TESTFN)
Brett Cannona71319e2003-05-14 02:18:31 +0000172 self.assertEqual(file_location, test_support.TESTFN)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000173 self.assertTrue(os.path.exists(file_location))
Brett Cannona71319e2003-05-14 02:18:31 +0000174 FILE = file(file_location)
175 try:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000176 self.assertTrue(FILE.read(), "reading from temporary file failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000177 finally:
178 FILE.close()
179 os.unlink(file_location)
180
181 def test_header(self):
182 # Make sure header returned as 2nd value from urlretrieve is good.
Neal Norwitz5be30672008-01-26 05:54:48 +0000183 file_location, header = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000184 os.unlink(file_location)
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000185 self.assertIsInstance(header, mimetools.Message,
186 "header is not an instance of mimetools.Message")
Tim Peters813cec92003-05-16 15:35:10 +0000187
Senthil Kumaran1b7f9e52010-05-01 08:01:56 +0000188 def test_data_header(self):
189 logo = "http://www.python.org/community/logos/python-logo-master-v3-TM.png"
190 file_location, fileheaders = self.urlretrieve(logo)
191 os.unlink(file_location)
192 datevalue = fileheaders.getheader('Date')
193 dateformat = '%a, %d %b %Y %H:%M:%S GMT'
194 try:
195 time.strptime(datevalue, dateformat)
196 except ValueError:
197 self.fail('Date value not in %r format', dateformat)
198
Brett Cannona71319e2003-05-14 02:18:31 +0000199
200
Skip Montanaro89feabc2003-03-30 04:54:24 +0000201def test_main():
202 test_support.requires('network')
Florent Xicluna6257a7b2010-03-31 22:01:03 +0000203 with test_support.check_py3k_warnings(
204 ("urllib.urlopen.. has been removed", DeprecationWarning)):
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000205 test_support.run_unittest(URLTimeoutTest,
206 urlopenNetworkTests,
207 urlretrieveNetworkTests)
Skip Montanaro89feabc2003-03-30 04:54:24 +0000208
209if __name__ == "__main__":
210 test_main()