blob: a019ddd2a6a9071062fb5dafadb17dcb07c9de8a [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):
15
Senthil Kumaranbd8f1452010-12-15 04:02:45 +000016 TIMEOUT = 30.0
Skip Montanaro89feabc2003-03-30 04:54:24 +000017
18 def setUp(self):
19 socket.setdefaulttimeout(self.TIMEOUT)
20
21 def tearDown(self):
22 socket.setdefaulttimeout(None)
23
24 def testURLread(self):
Senthil Kumaranee2538b2010-10-17 10:52:12 +000025 with support.transient_internet("www.python.org"):
26 f = urllib.request.urlopen("http://www.python.org/")
Skip Montanaro89feabc2003-03-30 04:54:24 +000027 x = f.read()
28
Brett Cannona71319e2003-05-14 02:18:31 +000029class urlopenNetworkTests(unittest.TestCase):
Jeremy Hylton1afc1692008-06-18 20:49:58 +000030 """Tests urllib.reqest.urlopen using the network.
Tim Peters813cec92003-05-16 15:35:10 +000031
Brett Cannona71319e2003-05-14 02:18:31 +000032 These tests are not exhaustive. Assuming that testing using files does a
33 good job overall of some of the basic interface features. There are no
34 tests exercising the optional 'data' and 'proxies' arguments. No tests
35 for transparent redirection have been written.
Tim Peters813cec92003-05-16 15:35:10 +000036
Brett Cannona71319e2003-05-14 02:18:31 +000037 setUp is not used for always constructing a connection to
38 http://www.python.org/ since there a few tests that don't use that address
39 and making a connection is expensive enough to warrant minimizing unneeded
40 connections.
Tim Peters813cec92003-05-16 15:35:10 +000041
Brett Cannona71319e2003-05-14 02:18:31 +000042 """
43
Senthil Kumaranee2538b2010-10-17 10:52:12 +000044 def urlopen(self, *args, **kwargs):
45 resource = args[0]
Antoine Pitroub6519492011-03-26 18:36:42 +010046 cm = support.transient_internet(resource)
47 cm.__enter__()
48 self.addCleanup(cm.__exit__, None, None, None)
49 return urllib.request.urlopen(*args, **kwargs)
Christian Heimesaf98da12008-01-27 15:18:18 +000050
Brett Cannona71319e2003-05-14 02:18:31 +000051 def test_basic(self):
52 # Simple test expected to pass.
Christian Heimesaf98da12008-01-27 15:18:18 +000053 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000054 for attr in ("read", "readline", "readlines", "fileno", "close",
55 "info", "geturl"):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000056 self.assertTrue(hasattr(open_url, attr), "object returned from "
Brett Cannona71319e2003-05-14 02:18:31 +000057 "urlopen lacks the %s attribute" % attr)
58 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000059 self.assertTrue(open_url.read(), "calling 'read' failed")
Brett Cannona71319e2003-05-14 02:18:31 +000060 finally:
61 open_url.close()
62
63 def test_readlines(self):
64 # Test both readline and readlines.
Christian Heimesaf98da12008-01-27 15:18:18 +000065 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000066 try:
Ezio Melottie9615932010-01-24 19:26:24 +000067 self.assertIsInstance(open_url.readline(), bytes,
68 "readline did not return a string")
69 self.assertIsInstance(open_url.readlines(), list,
70 "readlines did not return a list")
Brett Cannona71319e2003-05-14 02:18:31 +000071 finally:
72 open_url.close()
73
74 def test_info(self):
75 # Test 'info'.
Christian Heimesaf98da12008-01-27 15:18:18 +000076 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000077 try:
78 info_obj = open_url.info()
79 finally:
80 open_url.close()
Ezio Melottie9615932010-01-24 19:26:24 +000081 self.assertIsInstance(info_obj, email.message.Message,
82 "object returned by 'info' is not an "
83 "instance of email.message.Message")
Barry Warsaw820c1202008-06-12 04:06:45 +000084 self.assertEqual(info_obj.get_content_subtype(), "html")
Brett Cannona71319e2003-05-14 02:18:31 +000085
86 def test_geturl(self):
87 # Make sure same URL as opened is returned by geturl.
88 URL = "http://www.python.org/"
Christian Heimesaf98da12008-01-27 15:18:18 +000089 open_url = self.urlopen(URL)
Brett Cannona71319e2003-05-14 02:18:31 +000090 try:
91 gotten_url = open_url.geturl()
92 finally:
93 open_url.close()
94 self.assertEqual(gotten_url, URL)
95
Christian Heimes9bd667a2008-01-20 15:14:11 +000096 def test_getcode(self):
97 # test getcode() with the fancy opener to get 404 error codes
98 URL = "http://www.python.org/XXXinvalidXXX"
Jeremy Hylton1afc1692008-06-18 20:49:58 +000099 open_url = urllib.request.FancyURLopener().open(URL)
Christian Heimes9bd667a2008-01-20 15:14:11 +0000100 try:
101 code = open_url.getcode()
102 finally:
103 open_url.close()
104 self.assertEqual(code, 404)
105
Brett Cannona71319e2003-05-14 02:18:31 +0000106 def test_fileno(self):
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000107 if sys.platform in ('win32',):
Tim Peters813cec92003-05-16 15:35:10 +0000108 # On Windows, socket handles are not file descriptors; this
109 # test can't pass on Windows.
110 return
Brett Cannona71319e2003-05-14 02:18:31 +0000111 # Make sure fd returned by fileno is valid.
Senthil Kumaranee2538b2010-10-17 10:52:12 +0000112 open_url = self.urlopen("http://www.python.org/", timeout=None)
Tim Peters813cec92003-05-16 15:35:10 +0000113 fd = open_url.fileno()
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000114 FILE = os.fdopen(fd, encoding='utf-8')
Tim Peters813cec92003-05-16 15:35:10 +0000115 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000116 self.assertTrue(FILE.read(), "reading from file created using fd "
Tim Peters813cec92003-05-16 15:35:10 +0000117 "returned by fileno failed")
118 finally:
119 FILE.close()
Brett Cannona71319e2003-05-14 02:18:31 +0000120
121 def test_bad_address(self):
122 # Make sure proper exception is raised when connecting to a bogus
123 # address.
124 self.assertRaises(IOError,
Tim Peters0aab0022003-09-20 22:16:26 +0000125 # SF patch 809915: In Sep 2003, VeriSign started
126 # highjacking invalid .com and .net addresses to
127 # boost traffic to their own site. This test
128 # started failing then. One hopes the .invalid
129 # domain will be spared to serve its defined
130 # purpose.
131 # urllib.urlopen, "http://www.sadflkjsasadf.com/")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000132 urllib.request.urlopen,
Antoine Pitrou8fd33d32008-12-15 13:08:55 +0000133 "http://sadflkjsasf.i.nvali.d/")
Brett Cannona71319e2003-05-14 02:18:31 +0000134
135class urlretrieveNetworkTests(unittest.TestCase):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000136 """Tests urllib.request.urlretrieve using the network."""
Brett Cannona71319e2003-05-14 02:18:31 +0000137
Christian Heimesaf98da12008-01-27 15:18:18 +0000138 def urlretrieve(self, *args):
Senthil Kumaranee2538b2010-10-17 10:52:12 +0000139 resource = args[0]
Antoine Pitroub6519492011-03-26 18:36:42 +0100140 cm = support.transient_internet(resource)
141 cm.__enter__()
142 self.addCleanup(cm.__exit__, None, None, None)
143 return urllib.request.urlretrieve(*args)
Christian Heimesaf98da12008-01-27 15:18:18 +0000144
Brett Cannona71319e2003-05-14 02:18:31 +0000145 def test_basic(self):
146 # Test basic functionality.
Christian Heimesaf98da12008-01-27 15:18:18 +0000147 file_location,info = self.urlretrieve("http://www.python.org/")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000148 self.assertTrue(os.path.exists(file_location), "file location returned by"
Brett Cannona71319e2003-05-14 02:18:31 +0000149 " urlretrieve is not a valid path")
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000150 FILE = open(file_location, encoding='utf-8')
Brett Cannona71319e2003-05-14 02:18:31 +0000151 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000152 self.assertTrue(FILE.read(), "reading from the file location returned"
Jeremy Hyltonbd9f5202003-07-17 16:31:00 +0000153 " by urlretrieve failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000154 finally:
155 FILE.close()
156 os.unlink(file_location)
157
158 def test_specified_path(self):
159 # Make sure that specifying the location of the file to write to works.
Christian Heimesaf98da12008-01-27 15:18:18 +0000160 file_location,info = self.urlretrieve("http://www.python.org/",
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000161 support.TESTFN)
162 self.assertEqual(file_location, support.TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000163 self.assertTrue(os.path.exists(file_location))
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000164 FILE = open(file_location, encoding='utf-8')
Brett Cannona71319e2003-05-14 02:18:31 +0000165 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000166 self.assertTrue(FILE.read(), "reading from temporary file failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000167 finally:
168 FILE.close()
169 os.unlink(file_location)
170
171 def test_header(self):
172 # Make sure header returned as 2nd value from urlretrieve is good.
Christian Heimesaf98da12008-01-27 15:18:18 +0000173 file_location, header = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000174 os.unlink(file_location)
Ezio Melottie9615932010-01-24 19:26:24 +0000175 self.assertIsInstance(header, email.message.Message,
176 "header is not an instance of email.message.Message")
Tim Peters813cec92003-05-16 15:35:10 +0000177
Senthil Kumaranf6c456d2010-05-01 08:29:18 +0000178 def test_data_header(self):
179 logo = "http://www.python.org/community/logos/python-logo-master-v3-TM.png"
180 file_location, fileheaders = self.urlretrieve(logo)
181 os.unlink(file_location)
182 datevalue = fileheaders.get('Date')
183 dateformat = '%a, %d %b %Y %H:%M:%S GMT'
184 try:
185 time.strptime(datevalue, dateformat)
186 except ValueError:
187 self.fail('Date value not in %r format', dateformat)
Brett Cannona71319e2003-05-14 02:18:31 +0000188
189
Skip Montanaro89feabc2003-03-30 04:54:24 +0000190def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000191 support.requires('network')
192 support.run_unittest(URLTimeoutTest,
Brett Cannona71319e2003-05-14 02:18:31 +0000193 urlopenNetworkTests,
194 urlretrieveNetworkTests)
Skip Montanaro89feabc2003-03-30 04:54:24 +0000195
196if __name__ == "__main__":
197 test_main()