diff options
author | Brett Cannon <bcannon@gmail.com> | 2003-04-29 05:08:06 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2003-04-29 05:08:06 (GMT) |
commit | 19691360c7a8c203d1a94f6074036e810a6f0527 (patch) | |
tree | 9aff2fe100549b3af220796679673e616b9aa8d6 /Lib/test/test_urllib.py | |
parent | 6e887bb05fe619448e33a429e226ad0a5b5c7a8a (diff) | |
download | cpython-19691360c7a8c203d1a94f6074036e810a6f0527.zip cpython-19691360c7a8c203d1a94f6074036e810a6f0527.tar.gz cpython-19691360c7a8c203d1a94f6074036e810a6f0527.tar.bz2 |
Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.
Diffstat (limited to 'Lib/test/test_urllib.py')
-rw-r--r-- | Lib/test/test_urllib.py | 56 |
1 files changed, 48 insertions, 8 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 7d557a1..bc87b34 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -35,6 +35,7 @@ class urlopen_FileTests(unittest.TestCase): def tearDown(self): """Shut down the open object""" self.returned_obj.close() + os.remove(test_support.TESTFN) def test_interface(self): # Make sure object returned by urlopen() has the specified methods @@ -87,16 +88,54 @@ class urlopen_FileTests(unittest.TestCase): for line in self.returned_obj.__iter__(): self.assertEqual(line, self.text) -class urlretrieve_Tests(unittest.TestCase): +class urlretrieve_FileTests(unittest.TestCase): """Test urllib.urlretrieve() on local files""" - pass -class _urlopener_Tests(unittest.TestCase): - """Make sure urlopen() and urlretrieve() use the class assigned to - _urlopener""" - #XXX: Maybe create a custom class here that takes in a list and modifies - # it to signal that it was called? - pass + def setUp(self): + # Create a temporary file. + self.text = 'testing urllib.urlretrieve' + FILE = file(test_support.TESTFN, 'wb') + FILE.write(self.text) + FILE.close() + + def tearDown(self): + # Delete the temporary file. + os.remove(test_support.TESTFN) + + def test_basic(self): + # Make sure that a local file just gets its own location returned and + # a headers value is returned. + result = urllib.urlretrieve("file:%s" % test_support.TESTFN) + self.assertEqual(result[0], test_support.TESTFN) + self.assert_(isinstance(result[1], mimetools.Message), + "did not get a mimetools.Message instance as second " + "returned value") + + def test_copy(self): + # Test that setting the filename argument works. + second_temp = "%s.2" % test_support.TESTFN + result = urllib.urlretrieve("file:%s" % test_support.TESTFN, second_temp) + self.assertEqual(second_temp, result[0]) + self.assert_(os.path.exists(second_temp), "copy of the file was not " + "made") + FILE = file(second_temp, 'rb') + try: + text = FILE.read() + finally: + FILE.close() + self.assertEqual(self.text, text) + + def test_reporthook(self): + # Make sure that the reporthook works. + def hooktester(count, block_size, total_size, count_holder=[0]): + self.assert_(isinstance(count, int)) + self.assert_(isinstance(block_size, int)) + self.assert_(isinstance(total_size, int)) + self.assertEqual(count, count_holder[0]) + count_holder[0] = count_holder[0] + 1 + second_temp = "%s.2" % test_support.TESTFN + urllib.urlretrieve(test_support.TESTFN, second_temp, hooktester) + os.remove(second_temp) class QuotingTests(unittest.TestCase): """Tests for urllib.quote() and urllib.quote_plus() @@ -371,6 +410,7 @@ class Pathname_Tests(unittest.TestCase): def test_main(): test_suite = unittest.TestSuite() test_suite.addTest(unittest.makeSuite(urlopen_FileTests)) + test_suite.addTest(unittest.makeSuite(urlretrieve_FileTests)) test_suite.addTest(unittest.makeSuite(QuotingTests)) test_suite.addTest(unittest.makeSuite(UnquotingTests)) test_suite.addTest(unittest.makeSuite(urlencode_Tests)) |