blob: c8851031e894b30f316cbfccd317a92ba13b402c [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]
46 with support.transient_internet(resource):
47 return urllib.request.urlopen(*args, **kwargs)
Christian Heimesaf98da12008-01-27 15:18:18 +000048
Brett Cannona71319e2003-05-14 02:18:31 +000049 def test_basic(self):
50 # Simple test expected to pass.
Christian Heimesaf98da12008-01-27 15:18:18 +000051 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000052 for attr in ("read", "readline", "readlines", "fileno", "close",
53 "info", "geturl"):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000054 self.assertTrue(hasattr(open_url, attr), "object returned from "
Brett Cannona71319e2003-05-14 02:18:31 +000055 "urlopen lacks the %s attribute" % attr)
56 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000057 self.assertTrue(open_url.read(), "calling 'read' failed")
Brett Cannona71319e2003-05-14 02:18:31 +000058 finally:
59 open_url.close()
60
61 def test_readlines(self):
62 # Test both readline and readlines.
Christian Heimesaf98da12008-01-27 15:18:18 +000063 open_url = self.urlopen("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +000064 try:
Ezio Melottie9615932010-01-24 19:26:24 +000065 self.assertIsInstance(open_url.readline(), bytes,
66 "readline did not return a string")
67 self.assertIsInstance(open_url.readlines(), list,
68 "readlines did not return a list")
Brett Cannona71319e2003-05-14 02:18:31 +000069 finally:
70 open_url.close()
71
72 def test_info(self):
73 # Test 'info'.
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:
76 info_obj = open_url.info()
77 finally:
78 open_url.close()
Ezio Melottie9615932010-01-24 19:26:24 +000079 self.assertIsInstance(info_obj, email.message.Message,
80 "object returned by 'info' is not an "
81 "instance of email.message.Message")
Barry Warsaw820c1202008-06-12 04:06:45 +000082 self.assertEqual(info_obj.get_content_subtype(), "html")
Brett Cannona71319e2003-05-14 02:18:31 +000083
84 def test_geturl(self):
85 # Make sure same URL as opened is returned by geturl.
86 URL = "http://www.python.org/"
Christian Heimesaf98da12008-01-27 15:18:18 +000087 open_url = self.urlopen(URL)
Brett Cannona71319e2003-05-14 02:18:31 +000088 try:
89 gotten_url = open_url.geturl()
90 finally:
91 open_url.close()
92 self.assertEqual(gotten_url, URL)
93
Christian Heimes9bd667a2008-01-20 15:14:11 +000094 def test_getcode(self):
95 # test getcode() with the fancy opener to get 404 error codes
96 URL = "http://www.python.org/XXXinvalidXXX"
Jeremy Hylton1afc1692008-06-18 20:49:58 +000097 open_url = urllib.request.FancyURLopener().open(URL)
Christian Heimes9bd667a2008-01-20 15:14:11 +000098 try:
99 code = open_url.getcode()
100 finally:
101 open_url.close()
102 self.assertEqual(code, 404)
103
Brett Cannona71319e2003-05-14 02:18:31 +0000104 def test_fileno(self):
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000105 if sys.platform in ('win32',):
Tim Peters813cec92003-05-16 15:35:10 +0000106 # On Windows, socket handles are not file descriptors; this
107 # test can't pass on Windows.
108 return
Brett Cannona71319e2003-05-14 02:18:31 +0000109 # Make sure fd returned by fileno is valid.
Senthil Kumaranee2538b2010-10-17 10:52:12 +0000110 open_url = self.urlopen("http://www.python.org/", timeout=None)
Tim Peters813cec92003-05-16 15:35:10 +0000111 fd = open_url.fileno()
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000112 FILE = os.fdopen(fd, encoding='utf-8')
Tim Peters813cec92003-05-16 15:35:10 +0000113 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000114 self.assertTrue(FILE.read(), "reading from file created using fd "
Tim Peters813cec92003-05-16 15:35:10 +0000115 "returned by fileno failed")
116 finally:
117 FILE.close()
Brett Cannona71319e2003-05-14 02:18:31 +0000118
119 def test_bad_address(self):
120 # Make sure proper exception is raised when connecting to a bogus
121 # address.
122 self.assertRaises(IOError,
Tim Peters0aab0022003-09-20 22:16:26 +0000123 # SF patch 809915: In Sep 2003, VeriSign started
124 # highjacking invalid .com and .net addresses to
125 # boost traffic to their own site. This test
126 # started failing then. One hopes the .invalid
127 # domain will be spared to serve its defined
128 # purpose.
129 # urllib.urlopen, "http://www.sadflkjsasadf.com/")
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000130 urllib.request.urlopen,
Antoine Pitrou8fd33d32008-12-15 13:08:55 +0000131 "http://sadflkjsasf.i.nvali.d/")
Brett Cannona71319e2003-05-14 02:18:31 +0000132
133class urlretrieveNetworkTests(unittest.TestCase):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000134 """Tests urllib.request.urlretrieve using the network."""
Brett Cannona71319e2003-05-14 02:18:31 +0000135
Christian Heimesaf98da12008-01-27 15:18:18 +0000136 def urlretrieve(self, *args):
Senthil Kumaranee2538b2010-10-17 10:52:12 +0000137 resource = args[0]
138 with support.transient_internet(resource):
139 return urllib.request.urlretrieve(*args)
Christian Heimesaf98da12008-01-27 15:18:18 +0000140
Brett Cannona71319e2003-05-14 02:18:31 +0000141 def test_basic(self):
142 # Test basic functionality.
Christian Heimesaf98da12008-01-27 15:18:18 +0000143 file_location,info = self.urlretrieve("http://www.python.org/")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000144 self.assertTrue(os.path.exists(file_location), "file location returned by"
Brett Cannona71319e2003-05-14 02:18:31 +0000145 " urlretrieve is not a valid path")
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000146 FILE = open(file_location, encoding='utf-8')
Brett Cannona71319e2003-05-14 02:18:31 +0000147 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000148 self.assertTrue(FILE.read(), "reading from the file location returned"
Jeremy Hyltonbd9f5202003-07-17 16:31:00 +0000149 " by urlretrieve failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000150 finally:
151 FILE.close()
152 os.unlink(file_location)
153
154 def test_specified_path(self):
155 # Make sure that specifying the location of the file to write to works.
Christian Heimesaf98da12008-01-27 15:18:18 +0000156 file_location,info = self.urlretrieve("http://www.python.org/",
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000157 support.TESTFN)
158 self.assertEqual(file_location, support.TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000159 self.assertTrue(os.path.exists(file_location))
Amaury Forgeot d'Arcbdbddf82008-08-01 00:06:49 +0000160 FILE = open(file_location, encoding='utf-8')
Brett Cannona71319e2003-05-14 02:18:31 +0000161 try:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000162 self.assertTrue(FILE.read(), "reading from temporary file failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000163 finally:
164 FILE.close()
165 os.unlink(file_location)
166
167 def test_header(self):
168 # Make sure header returned as 2nd value from urlretrieve is good.
Christian Heimesaf98da12008-01-27 15:18:18 +0000169 file_location, header = self.urlretrieve("http://www.python.org/")
Brett Cannona71319e2003-05-14 02:18:31 +0000170 os.unlink(file_location)
Ezio Melottie9615932010-01-24 19:26:24 +0000171 self.assertIsInstance(header, email.message.Message,
172 "header is not an instance of email.message.Message")
Tim Peters813cec92003-05-16 15:35:10 +0000173
Senthil Kumaranf6c456d2010-05-01 08:29:18 +0000174 def test_data_header(self):
175 logo = "http://www.python.org/community/logos/python-logo-master-v3-TM.png"
176 file_location, fileheaders = self.urlretrieve(logo)
177 os.unlink(file_location)
178 datevalue = fileheaders.get('Date')
179 dateformat = '%a, %d %b %Y %H:%M:%S GMT'
180 try:
181 time.strptime(datevalue, dateformat)
182 except ValueError:
183 self.fail('Date value not in %r format', dateformat)
Brett Cannona71319e2003-05-14 02:18:31 +0000184
185
Skip Montanaro89feabc2003-03-30 04:54:24 +0000186def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000187 support.requires('network')
188 support.run_unittest(URLTimeoutTest,
Brett Cannona71319e2003-05-14 02:18:31 +0000189 urlopenNetworkTests,
190 urlretrieveNetworkTests)
Skip Montanaro89feabc2003-03-30 04:54:24 +0000191
192if __name__ == "__main__":
193 test_main()