blob: 1e9cf0d7865cb06d41e5642a793f6c280f0896fd [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
Ezio Melotti3897a442010-08-02 01:33:30 +000010mimetools = test_support.import_module("mimetools", deprecated=True)
Senthil Kumaranfcfd25b2010-05-01 08:06:38 +000011import time
Skip Montanaro89feabc2003-03-30 04:54:24 +000012
Neal Norwitz5be30672008-01-26 05:54:48 +000013
14def _open_with_retry(func, host, *args, **kwargs):
15 # Connecting to remote hosts is flaky. Make it more robust
16 # by retrying the connection several times.
17 for i in range(3):
18 try:
19 return func(host, *args, **kwargs)
20 except IOError, last_exc:
21 continue
22 except:
23 raise
24 raise last_exc
25
26
Skip Montanaro89feabc2003-03-30 04:54:24 +000027class URLTimeoutTest(unittest.TestCase):
28
29 TIMEOUT = 10.0
30
31 def setUp(self):
32 socket.setdefaulttimeout(self.TIMEOUT)
33
34 def tearDown(self):
35 socket.setdefaulttimeout(None)
36
37 def testURLread(self):
Neal Norwitz5be30672008-01-26 05:54:48 +000038 f = _open_with_retry(urllib.urlopen, "http://www.python.org/")
Skip Montanaro89feabc2003-03-30 04:54:24 +000039 x = f.read()
40
Brett Cannona71319e2003-05-14 02:18:31 +000041class urlopenNetworkTests(unittest.TestCase):
42 """Tests urllib.urlopen using the network.
Tim Peters813cec92003-05-16 15:35:10 +000043
Brett Cannona71319e2003-05-14 02:18:31 +000044 These tests are not exhaustive. Assuming that testing using files does a
45 good job overall of some of the basic interface features. There are no
46 tests exercising the optional 'data' and 'proxies' arguments. No tests
47 for transparent redirection have been written.
Tim Peters813cec92003-05-16 15:35:10 +000048
Brett Cannona71319e2003-05-14 02:18:31 +000049 setUp is not used for always constructing a connection to
50 http://www.python.org/ since there a few tests that don't use that address
51 and making a connection is expensive enough to warrant minimizing unneeded
52 connections.
Tim Peters813cec92003-05-16 15:35:10 +000053
Brett Cannona71319e2003-05-14 02:18:31 +000054 """
55
Neal Norwitz5be30672008-01-26 05:54:48 +000056 def urlopen(self, *args):
57 return _open_with_retry(urllib.urlopen, *args)
58
Brett Cannona71319e2003-05-14 02:18:31 +000059 def test_basic(self):
60 # Simple test expected to pass.
Neal Norwitz5be30672008-01-26 05:54:48 +000061 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000062 for attr in ("read", "readline", "readlines", "fileno", "close",
63 "info", "geturl"):
64 self.assert_(hasattr(open_url, attr), "object returned from "
65 "urlopen lacks the %s attribute" % attr)
66 try:
67 self.assert_(open_url.read(), "calling 'read' failed")
68 finally:
69 open_url.close()
70
71 def test_readlines(self):
72 # Test both readline and readlines.
Neal Norwitz5be30672008-01-26 05:54:48 +000073 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000074 try:
75 self.assert_(isinstance(open_url.readline(), basestring),
76 "readline did not return a string")
77 self.assert_(isinstance(open_url.readlines(), list),
78 "readlines did not return a list")
79 finally:
80 open_url.close()
81
82 def test_info(self):
83 # Test 'info'.
Neal Norwitz5be30672008-01-26 05:54:48 +000084 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000085 try:
86 info_obj = open_url.info()
87 finally:
88 open_url.close()
89 self.assert_(isinstance(info_obj, mimetools.Message),
90 "object returned by 'info' is not an instance of "
91 "mimetools.Message")
92 self.assertEqual(info_obj.getsubtype(), "html")
93
94 def test_geturl(self):
95 # Make sure same URL as opened is returned by geturl.
96 URL = "http://www.python.org/"
Neal Norwitz5be30672008-01-26 05:54:48 +000097 open_url = self.urlopen(URL)
Brett Cannona71319e2003-05-14 02:18:31 +000098 try:
99 gotten_url = open_url.geturl()
100 finally:
101 open_url.close()
102 self.assertEqual(gotten_url, URL)
103
Georg Brandl9b0d46d2008-01-20 11:43:03 +0000104 def test_getcode(self):
105 # test getcode() with the fancy opener to get 404 error codes
106 URL = "http://www.python.org/XXXinvalidXXX"
107 open_url = urllib.FancyURLopener().open(URL)
108 try:
109 code = open_url.getcode()
110 finally:
111 open_url.close()
112 self.assertEqual(code, 404)
113
Brett Cannona71319e2003-05-14 02:18:31 +0000114 def test_fileno(self):
Tim Petersf545baa2003-06-15 23:26:30 +0000115 if (sys.platform in ('win32',) or
Tim Peters813cec92003-05-16 15:35:10 +0000116 not hasattr(os, 'fdopen')):
117 # On Windows, socket handles are not file descriptors; this
118 # test can't pass on Windows.
119 return
Brett Cannona71319e2003-05-14 02:18:31 +0000120 # Make sure fd returned by fileno is valid.
Neal Norwitz5be30672008-01-26 05:54:48 +0000121 open_url = self.urlopen("http://www.python.org/")
Tim Peters813cec92003-05-16 15:35:10 +0000122 fd = open_url.fileno()
123 FILE = os.fdopen(fd)
124 try:
125 self.assert_(FILE.read(), "reading from file created using fd "
126 "returned by fileno failed")
127 finally:
128 FILE.close()
Brett Cannona71319e2003-05-14 02:18:31 +0000129
130 def test_bad_address(self):
131 # Make sure proper exception is raised when connecting to a bogus
132 # address.
133 self.assertRaises(IOError,
Tim Peters0aab0022003-09-20 22:16:26 +0000134 # SF patch 809915: In Sep 2003, VeriSign started
135 # highjacking invalid .com and .net addresses to
136 # boost traffic to their own site. This test
137 # started failing then. One hopes the .invalid
138 # domain will be spared to serve its defined
139 # purpose.
140 # urllib.urlopen, "http://www.sadflkjsasadf.com/")
Antoine Pitrou3fced652008-12-15 17:48:36 +0000141 urllib.urlopen, "http://sadflkjsasf.i.nvali.d/")
Brett Cannona71319e2003-05-14 02:18:31 +0000142
143class urlretrieveNetworkTests(unittest.TestCase):
144 """Tests urllib.urlretrieve using the network."""
145
Neal Norwitz5be30672008-01-26 05:54:48 +0000146 def urlretrieve(self, *args):
147 return _open_with_retry(urllib.urlretrieve, *args)
148
Brett Cannona71319e2003-05-14 02:18:31 +0000149 def test_basic(self):
150 # Test basic functionality.
Neal Norwitz5be30672008-01-26 05:54:48 +0000151 file_location,info = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000152 self.assert_(os.path.exists(file_location), "file location returned by"
153 " urlretrieve is not a valid path")
154 FILE = file(file_location)
155 try:
Jeremy Hyltonbd9f5202003-07-17 16:31:00 +0000156 self.assert_(FILE.read(), "reading from the file location returned"
157 " by urlretrieve failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000158 finally:
159 FILE.close()
160 os.unlink(file_location)
161
162 def test_specified_path(self):
163 # Make sure that specifying the location of the file to write to works.
Neal Norwitz5be30672008-01-26 05:54:48 +0000164 file_location,info = self.urlretrieve("http://www.python.org/",
165 test_support.TESTFN)
Brett Cannona71319e2003-05-14 02:18:31 +0000166 self.assertEqual(file_location, test_support.TESTFN)
167 self.assert_(os.path.exists(file_location))
168 FILE = file(file_location)
169 try:
170 self.assert_(FILE.read(), "reading from temporary file failed")
171 finally:
172 FILE.close()
173 os.unlink(file_location)
174
175 def test_header(self):
176 # Make sure header returned as 2nd value from urlretrieve is good.
Neal Norwitz5be30672008-01-26 05:54:48 +0000177 file_location, header = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000178 os.unlink(file_location)
179 self.assert_(isinstance(header, mimetools.Message),
180 "header is not an instance of mimetools.Message")
Tim Peters813cec92003-05-16 15:35:10 +0000181
Senthil Kumaranfcfd25b2010-05-01 08:06:38 +0000182 def test_data_header(self):
183 logo = "http://www.python.org/community/logos/python-logo-master-v3-TM.png"
184 file_location, fileheaders = self.urlretrieve(logo)
185 os.unlink(file_location)
186 datevalue = fileheaders.getheader('Date')
187 dateformat = '%a, %d %b %Y %H:%M:%S GMT'
188 try:
189 time.strptime(datevalue, dateformat)
190 except ValueError:
191 self.fail('Date value not in %r format', dateformat)
Brett Cannona71319e2003-05-14 02:18:31 +0000192
193
Skip Montanaro89feabc2003-03-30 04:54:24 +0000194def test_main():
195 test_support.requires('network')
Ezio Melotti7b688252010-08-03 00:18:09 +0000196 with test_support._check_py3k_warnings(
Ezio Melotti1d55ec32010-08-02 23:34:49 +0000197 ("urllib.urlopen.. has been removed", DeprecationWarning)):
Brett Cannon8bb8fa52008-07-02 01:57:08 +0000198 test_support.run_unittest(URLTimeoutTest,
199 urlopenNetworkTests,
200 urlretrieveNetworkTests)
Skip Montanaro89feabc2003-03-30 04:54:24 +0000201
202if __name__ == "__main__":
203 test_main()