blob: f38a2d0f9b39755d1cc4c0811b2ac4c8eef0b5b4 [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.
28
29 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.
33
34 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.
38
39 """
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:
57 self.assert_(isinstance(open_url.readline(), basestring),
58 "readline did not return a string")
59 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
86 def test_fileno(self):
87 # Make sure fd returned by fileno is valid.
88 if hasattr(os, 'fdopen'):
89 open_url = urllib.urlopen("http://www.python.org/")
90 fd = open_url.fileno()
91 FILE = os.fdopen(fd)
92 try:
93 self.assert_(FILE.read(), "reading from file created using fd "
94 "returned by fileno failed")
95 finally:
96 FILE.close()
97
98 def test_bad_address(self):
99 # Make sure proper exception is raised when connecting to a bogus
100 # address.
101 self.assertRaises(IOError,
102 urllib.urlopen, "http://www.sadflkjsasadf.com/")
103
104class urlretrieveNetworkTests(unittest.TestCase):
105 """Tests urllib.urlretrieve using the network."""
106
107 def test_basic(self):
108 # Test basic functionality.
109 file_location,info = urllib.urlretrieve("http://www.python.org/")
110 self.assert_(os.path.exists(file_location), "file location returned by"
111 " urlretrieve is not a valid path")
112 FILE = file(file_location)
113 try:
114 self.assert_(FILE.read(), "reading from the file location returned "
115 "by urlretrieve failed")
116 finally:
117 FILE.close()
118 os.unlink(file_location)
119
120 def test_specified_path(self):
121 # Make sure that specifying the location of the file to write to works.
122 file_location,info = urllib.urlretrieve("http://www.python.org/",
123 test_support.TESTFN)
124 self.assertEqual(file_location, test_support.TESTFN)
125 self.assert_(os.path.exists(file_location))
126 FILE = file(file_location)
127 try:
128 self.assert_(FILE.read(), "reading from temporary file failed")
129 finally:
130 FILE.close()
131 os.unlink(file_location)
132
133 def test_header(self):
134 # Make sure header returned as 2nd value from urlretrieve is good.
135 file_location, header = urllib.urlretrieve("http://www.python.org/")
136 os.unlink(file_location)
137 self.assert_(isinstance(header, mimetools.Message),
138 "header is not an instance of mimetools.Message")
139
140
141
Skip Montanaro89feabc2003-03-30 04:54:24 +0000142def test_main():
143 test_support.requires('network')
Brett Cannona71319e2003-05-14 02:18:31 +0000144 test_support.run_unittest(URLTimeoutTest,
145 urlopenNetworkTests,
146 urlretrieveNetworkTests)
Skip Montanaro89feabc2003-03-30 04:54:24 +0000147
148if __name__ == "__main__":
149 test_main()