blob: a7d3805e91d05f0087307a2eb047c05c552ab597 [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
10import mimetools
Skip Montanaro89feabc2003-03-30 04:54:24 +000011
12class URLTimeoutTest(unittest.TestCase):
13
14 TIMEOUT = 10.0
15
16 def setUp(self):
17 socket.setdefaulttimeout(self.TIMEOUT)
18
19 def tearDown(self):
20 socket.setdefaulttimeout(None)
21
22 def testURLread(self):
Brett Cannona71319e2003-05-14 02:18:31 +000023 f = urllib.urlopen("http://www.python.org/")
Skip Montanaro89feabc2003-03-30 04:54:24 +000024 x = f.read()
25
Brett Cannona71319e2003-05-14 02:18:31 +000026class urlopenNetworkTests(unittest.TestCase):
27 """Tests urllib.urlopen using the network.
Tim Peters813cec92003-05-16 15:35:10 +000028
Brett Cannona71319e2003-05-14 02:18:31 +000029 These tests are not exhaustive. Assuming that testing using files does a
30 good job overall of some of the basic interface features. There are no
31 tests exercising the optional 'data' and 'proxies' arguments. No tests
32 for transparent redirection have been written.
Tim Peters813cec92003-05-16 15:35:10 +000033
Brett Cannona71319e2003-05-14 02:18:31 +000034 setUp is not used for always constructing a connection to
35 http://www.python.org/ since there a few tests that don't use that address
36 and making a connection is expensive enough to warrant minimizing unneeded
37 connections.
Tim Peters813cec92003-05-16 15:35:10 +000038
Brett Cannona71319e2003-05-14 02:18:31 +000039 """
40
41 def test_basic(self):
42 # Simple test expected to pass.
43 open_url = urllib.urlopen("http://www.python.org/")
44 for attr in ("read", "readline", "readlines", "fileno", "close",
45 "info", "geturl"):
46 self.assert_(hasattr(open_url, attr), "object returned from "
47 "urlopen lacks the %s attribute" % attr)
48 try:
49 self.assert_(open_url.read(), "calling 'read' failed")
50 finally:
51 open_url.close()
52
53 def test_readlines(self):
54 # Test both readline and readlines.
55 open_url = urllib.urlopen("http://www.python.org/")
56 try:
Jeremy Hylton3e186152007-08-04 03:46:11 +000057 self.assert_(isinstance(open_url.readline(), bytes),
58 "readline did not return bytes")
Brett Cannona71319e2003-05-14 02:18:31 +000059 self.assert_(isinstance(open_url.readlines(), list),
60 "readlines did not return a list")
61 finally:
62 open_url.close()
63
64 def test_info(self):
65 # Test 'info'.
66 open_url = urllib.urlopen("http://www.python.org/")
67 try:
68 info_obj = open_url.info()
69 finally:
70 open_url.close()
71 self.assert_(isinstance(info_obj, mimetools.Message),
72 "object returned by 'info' is not an instance of "
73 "mimetools.Message")
74 self.assertEqual(info_obj.getsubtype(), "html")
75
76 def test_geturl(self):
77 # Make sure same URL as opened is returned by geturl.
78 URL = "http://www.python.org/"
79 open_url = urllib.urlopen(URL)
80 try:
81 gotten_url = open_url.geturl()
82 finally:
83 open_url.close()
84 self.assertEqual(gotten_url, URL)
85
Christian Heimes9bd667a2008-01-20 15:14:11 +000086 def test_getcode(self):
87 # test getcode() with the fancy opener to get 404 error codes
88 URL = "http://www.python.org/XXXinvalidXXX"
89 open_url = urllib.FancyURLopener().open(URL)
90 try:
91 code = open_url.getcode()
92 finally:
93 open_url.close()
94 self.assertEqual(code, 404)
95
Brett Cannona71319e2003-05-14 02:18:31 +000096 def test_fileno(self):
Tim Petersf545baa2003-06-15 23:26:30 +000097 if (sys.platform in ('win32',) or
Tim Peters813cec92003-05-16 15:35:10 +000098 not hasattr(os, 'fdopen')):
99 # On Windows, socket handles are not file descriptors; this
100 # test can't pass on Windows.
101 return
Brett Cannona71319e2003-05-14 02:18:31 +0000102 # Make sure fd returned by fileno is valid.
Tim Peters813cec92003-05-16 15:35:10 +0000103 open_url = urllib.urlopen("http://www.python.org/")
104 fd = open_url.fileno()
105 FILE = os.fdopen(fd)
106 try:
107 self.assert_(FILE.read(), "reading from file created using fd "
108 "returned by fileno failed")
109 finally:
110 FILE.close()
Brett Cannona71319e2003-05-14 02:18:31 +0000111
112 def test_bad_address(self):
113 # Make sure proper exception is raised when connecting to a bogus
114 # address.
115 self.assertRaises(IOError,
Tim Peters0aab0022003-09-20 22:16:26 +0000116 # SF patch 809915: In Sep 2003, VeriSign started
117 # highjacking invalid .com and .net addresses to
118 # boost traffic to their own site. This test
119 # started failing then. One hopes the .invalid
120 # domain will be spared to serve its defined
121 # purpose.
122 # urllib.urlopen, "http://www.sadflkjsasadf.com/")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000123 urllib.urlopen, "http://www.python.invalid./")
Brett Cannona71319e2003-05-14 02:18:31 +0000124
125class urlretrieveNetworkTests(unittest.TestCase):
126 """Tests urllib.urlretrieve using the network."""
127
128 def test_basic(self):
129 # Test basic functionality.
130 file_location,info = urllib.urlretrieve("http://www.python.org/")
131 self.assert_(os.path.exists(file_location), "file location returned by"
132 " urlretrieve is not a valid path")
Alex Martelli01c77c62006-08-24 02:58:11 +0000133 FILE = open(file_location)
Brett Cannona71319e2003-05-14 02:18:31 +0000134 try:
Jeremy Hyltonbd9f5202003-07-17 16:31:00 +0000135 self.assert_(FILE.read(), "reading from the file location returned"
136 " by urlretrieve failed")
Brett Cannona71319e2003-05-14 02:18:31 +0000137 finally:
138 FILE.close()
139 os.unlink(file_location)
140
141 def test_specified_path(self):
142 # Make sure that specifying the location of the file to write to works.
143 file_location,info = urllib.urlretrieve("http://www.python.org/",
144 test_support.TESTFN)
145 self.assertEqual(file_location, test_support.TESTFN)
146 self.assert_(os.path.exists(file_location))
Alex Martelli01c77c62006-08-24 02:58:11 +0000147 FILE = open(file_location)
Brett Cannona71319e2003-05-14 02:18:31 +0000148 try:
149 self.assert_(FILE.read(), "reading from temporary file failed")
150 finally:
151 FILE.close()
152 os.unlink(file_location)
153
154 def test_header(self):
155 # Make sure header returned as 2nd value from urlretrieve is good.
156 file_location, header = urllib.urlretrieve("http://www.python.org/")
157 os.unlink(file_location)
158 self.assert_(isinstance(header, mimetools.Message),
159 "header is not an instance of mimetools.Message")
Tim Peters813cec92003-05-16 15:35:10 +0000160
Brett Cannona71319e2003-05-14 02:18:31 +0000161
162
Skip Montanaro89feabc2003-03-30 04:54:24 +0000163def test_main():
164 test_support.requires('network')
Brett Cannona71319e2003-05-14 02:18:31 +0000165 test_support.run_unittest(URLTimeoutTest,
166 urlopenNetworkTests,
167 urlretrieveNetworkTests)
Skip Montanaro89feabc2003-03-30 04:54:24 +0000168
169if __name__ == "__main__":
170 test_main()