blob: 32efb2b06e617492e8bd02463e48babf14a70b5c [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Skip Montanaro89feabc2003-03-30 04:54:24 +00002
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
Senthil Kumaranf6c456d2010-05-01 08:29:18 +000011import time
Skip Montanaro89feabc2003-03-30 04:54:24 +000012
Christian Heimesaf98da12008-01-27 15:18:18 +000013
Skip Montanaro89feabc2003-03-30 04:54:24 +000014class URLTimeoutTest(unittest.TestCase):
Antoine Pitroud9faa202011-03-26 18:38:06 +010015 # XXX this test doesn't seem to test anything useful.
Skip Montanaro89feabc2003-03-30 04:54:24 +000016
Senthil Kumaranbd8f1452010-12-15 04:02:45 +000017 TIMEOUT = 30.0
Skip Montanaro89feabc2003-03-30 04:54:24 +000018
19 def setUp(self):
20 socket.setdefaulttimeout(self.TIMEOUT)
21
22 def tearDown(self):
23 socket.setdefaulttimeout(None)
24
25 def testURLread(self):
Senthil Kumaranee2538b2010-10-17 10:52:12 +000026 with support.transient_internet("www.python.org"):
27 f = urllib.request.urlopen("http://www.python.org/")
Antoine Pitroud9faa202011-03-26 18:38:06 +010028 x = f.read()
Skip Montanaro89feabc2003-03-30 04:54:24 +000029
Brett Cannona71319e2003-05-14 02:18:31 +000030class urlopenNetworkTests(unittest.TestCase):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000031 """Tests urllib.reqest.urlopen using the network.
Tim Peters813cec92003-05-16 15:35:10 +000032
Brett Cannona71319e2003-05-14 02:18:31 +000033 These tests are not exhaustive. Assuming that testing using files does a
34 good job overall of some of the basic interface features. There are no
35 tests exercising the optional 'data' and 'proxies' arguments. No tests
36 for transparent redirection have been written.
Tim Peters813cec92003-05-16 15:35:10 +000037
Brett Cannona71319e2003-05-14 02:18:31 +000038 setUp is not used for always constructing a connection to
39 http://www.python.org/ since there a few tests that don't use that address
40 and making a connection is expensive enough to warrant minimizing unneeded
41 connections.
Tim Peters813cec92003-05-16 15:35:10 +000042
Brett Cannona71319e2003-05-14 02:18:31 +000043 """
44
Senthil Kumaranee2538b2010-10-17 10:52:12 +000045 def urlopen(self, *args, **kwargs):
46 resource = args[0]
Antoine Pitroub6519492011-03-26 18:36:42 +010047 cm = support.transient_internet(resource)
48 cm.__enter__()
49 self.addCleanup(cm.__exit__, None, None, None)
50 return urllib.request.urlopen(*args, **kwargs)
Christian Heimesaf98da12008-01-27 15:18:18 +000051
Brett Cannona71319e2003-05-14 02:18:31 +000052 def test_basic(self):
53 # Simple test expected to pass.
Christian Heimesaf98da12008-01-27 15:18:18 +000054 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000055 for attr in ("read", "readline", "readlines", "fileno", "close",
56 "info", "geturl"):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000057 self.assertTrue(hasattr(open_url, attr), "object returned from "
Brett Cannona71319e2003-05-14 02:18:31 +000058 "urlopen lacks the %s attribute" % attr)
59 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000060 self.assertTrue(open_url.read(), "calling 'read' failed")
Brett Cannona71319e2003-05-14 02:18:31 +000061 finally:
62 open_url.close()
63
64 def test_readlines(self):
65 # Test both readline and readlines.
Christian Heimesaf98da12008-01-27 15:18:18 +000066 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000067 try:
Ezio Melottie9615932010-01-24 19:26:24 +000068 self.assertIsInstance(open_url.readline(), bytes,
69 "readline did not return a string")
70 self.assertIsInstance(open_url.readlines(), list,
71 "readlines did not return a list")
Brett Cannona71319e2003-05-14 02:18:31 +000072 finally:
73 open_url.close()
74
75 def test_info(self):
76 # Test 'info'.
Christian Heimesaf98da12008-01-27 15:18:18 +000077 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000078 try:
79 info_obj = open_url.info()
80 finally:
81 open_url.close()
Ezio Melottie9615932010-01-24 19:26:24 +000082 self.assertIsInstance(info_obj, email.message.Message,
83 "object returned by 'info' is not an "
84 "instance of email.message.Message")
Barry Warsaw820c1202008-06-12 04:06:45 +000085 self.assertEqual(info_obj.get_content_subtype(), "html")
Brett Cannona71319e2003-05-14 02:18:31 +000086
87 def test_geturl(self):
88 # Make sure same URL as opened is returned by geturl.
89 URL = "http://www.python.org/"
Christian Heimesaf98da12008-01-27 15:18:18 +000090 open_url = self.urlopen(URL)
Brett Cannona71319e2003-05-14 02:18:31 +000091 try:
92 gotten_url = open_url.geturl()
93 finally:
94 open_url.close()
95 self.assertEqual(gotten_url, URL)
96
Christian Heimes9bd667a2008-01-20 15:14:11 +000097 def test_getcode(self):
98 # test getcode() with the fancy opener to get 404 error codes
99 URL = "http://www.python.org/XXXinvalidXXX"
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000100 open_url = urllib.request.FancyURLopener().open(URL)
Christian Heimes9bd667a2008-01-20 15:14:11 +0000101 try:
102 code = open_url.getcode()
103 finally:
104 open_url.close()
105 self.assertEqual(code, 404)
106
Brett Cannona71319e2003-05-14 02:18:31 +0000107 def test_fileno(self):
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000108 if sys.platform in ('win32',):
Tim Peters813cec92003-05-16 15:35:10 +0000109 # On Windows, socket handles are not file descriptors; this
110 # test can't pass on Windows.
111 return
Brett Cannona71319e2003-05-14 02:18:31 +0000112 # Make sure fd returned by fileno is valid.
Senthil Kumaranee2538b2010-10-17 10:52:12 +0000113 open_url = self.urlopen("http://www.python.org/", timeout=None)
Tim Peters813cec92003-05-16 15:35:10 +0000114 fd = open_url.fileno()
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000115 FILE = os.fdopen(fd, encoding='utf-8')
Tim Peters813cec92003-05-16 15:35:10 +0000116 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000117 self.assertTrue(FILE.read(), "reading from file created using fd "
Tim Peters813cec92003-05-16 15:35:10 +0000118 "returned by fileno failed")
119 finally:
120 FILE.close()
Brett Cannona71319e2003-05-14 02:18:31 +0000121
122 def test_bad_address(self):
123 # Make sure proper exception is raised when connecting to a bogus
124 # address.
125 self.assertRaises(IOError,
Tim Peters0aab0022003-09-20 22:16:26 +0000126 # SF patch 809915: In Sep 2003, VeriSign started
127 # highjacking invalid .com and .net addresses to
128 # boost traffic to their own site. This test
129 # started failing then. One hopes the .invalid
130 # domain will be spared to serve its defined
131 # purpose.
132 # urllib.urlopen, "http://www.sadflkjsasadf.com/")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000133 urllib.request.urlopen,
Antoine Pitrou8fd33d32008-12-15 13:08:55 +0000134 "http://sadflkjsasf.i.nvali.d/")
Brett Cannona71319e2003-05-14 02:18:31 +0000135
136class urlretrieveNetworkTests(unittest.TestCase):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000137 """Tests urllib.request.urlretrieve using the network."""
Brett Cannona71319e2003-05-14 02:18:31 +0000138
Christian Heimesaf98da12008-01-27 15:18:18 +0000139 def urlretrieve(self, *args):
Senthil Kumaranee2538b2010-10-17 10:52:12 +0000140 resource = args[0]
Antoine Pitroub6519492011-03-26 18:36:42 +0100141 cm = support.transient_internet(resource)
142 cm.__enter__()
143 self.addCleanup(cm.__exit__, None, None, None)
144 return urllib.request.urlretrieve(*args)
Christian Heimesaf98da12008-01-27 15:18:18 +0000145
Brett Cannona71319e2003-05-14 02:18:31 +0000146 def test_basic(self):
147 # Test basic functionality.
Christian Heimesaf98da12008-01-27 15:18:18 +0000148 file_location,info = self.urlretrieve("http://www.python.org/")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000149 self.assertTrue(os.path.exists(file_location), "file location returned by"
Brett Cannona71319e2003-05-14 02:18:31 +0000150 " urlretrieve is not a valid path")
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000151 FILE = open(file_location, encoding='utf-8')
Brett Cannona71319e2003-05-14 02:18:31 +0000152 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000153 self.assertTrue(FILE.read(), "reading from the file location returned"
Jeremy Hyltonbd9f5202003-07-17 16:31:00 +0000154 " by urlretrieve failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000155 finally:
156 FILE.close()
157 os.unlink(file_location)
158
159 def test_specified_path(self):
160 # Make sure that specifying the location of the file to write to works.
Christian Heimesaf98da12008-01-27 15:18:18 +0000161 file_location,info = self.urlretrieve("http://www.python.org/",
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000162 support.TESTFN)
163 self.assertEqual(file_location, support.TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000164 self.assertTrue(os.path.exists(file_location))
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000165 FILE = open(file_location, encoding='utf-8')
Brett Cannona71319e2003-05-14 02:18:31 +0000166 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000167 self.assertTrue(FILE.read(), "reading from temporary file failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000168 finally:
169 FILE.close()
170 os.unlink(file_location)
171
172 def test_header(self):
173 # Make sure header returned as 2nd value from urlretrieve is good.
Christian Heimesaf98da12008-01-27 15:18:18 +0000174 file_location, header = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000175 os.unlink(file_location)
Ezio Melottie9615932010-01-24 19:26:24 +0000176 self.assertIsInstance(header, email.message.Message,
177 "header is not an instance of email.message.Message")
Tim Peters813cec92003-05-16 15:35:10 +0000178
Senthil Kumaranf6c456d2010-05-01 08:29:18 +0000179 def test_data_header(self):
180 logo = "http://www.python.org/community/logos/python-logo-master-v3-TM.png"
181 file_location, fileheaders = self.urlretrieve(logo)
182 os.unlink(file_location)
183 datevalue = fileheaders.get('Date')
184 dateformat = '%a, %d %b %Y %H:%M:%S GMT'
185 try:
186 time.strptime(datevalue, dateformat)
187 except ValueError:
188 self.fail('Date value not in %r format', dateformat)
Brett Cannona71319e2003-05-14 02:18:31 +0000189
190
Skip Montanaro89feabc2003-03-30 04:54:24 +0000191def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000192 support.requires('network')
193 support.run_unittest(URLTimeoutTest,
Brett Cannona71319e2003-05-14 02:18:31 +0000194 urlopenNetworkTests,
195 urlretrieveNetworkTests)
Skip Montanaro89feabc2003-03-30 04:54:24 +0000196
197if __name__ == "__main__":
198 test_main()