diff options
Diffstat (limited to 'Lib/test/test_urllib.py')
-rw-r--r-- | Lib/test/test_urllib.py | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 68bb49efb2..f41fa2a950 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -9,6 +9,8 @@ import io import unittest from unittest.mock import patch from test import support +from test.support import os_helper +from test.support import warnings_helper import os try: import ssl @@ -50,7 +52,7 @@ def urlopen(url, data=None, proxies=None): def FancyURLopener(): - with support.check_warnings( + with warnings_helper.check_warnings( ('FancyURLopener style of invoking requests is deprecated.', DeprecationWarning)): return urllib.request.FancyURLopener() @@ -145,19 +147,19 @@ class urlopen_FileTests(unittest.TestCase): # Create a temp file to use for testing self.text = bytes("test_urllib: %s\n" % self.__class__.__name__, "ascii") - f = open(support.TESTFN, 'wb') + f = open(os_helper.TESTFN, 'wb') try: f.write(self.text) finally: f.close() - self.pathname = support.TESTFN + self.pathname = os_helper.TESTFN self.quoted_pathname = urllib.parse.quote(self.pathname) self.returned_obj = urlopen("file:%s" % self.quoted_pathname) def tearDown(self): """Shut down the open object""" self.returned_obj.close() - os.remove(support.TESTFN) + os.remove(os_helper.TESTFN) def test_interface(self): # Make sure object returned by urlopen() has the specified methods @@ -230,7 +232,7 @@ class ProxyTests(unittest.TestCase): def setUp(self): # Records changes to env vars - self.env = support.EnvironmentVarGuard() + self.env = os_helper.EnvironmentVarGuard() # Delete all proxy related env vars for k in list(os.environ): if 'proxy' in k.lower(): @@ -592,13 +594,13 @@ Connection: close self.unfakehttp() def test_URLopener_deprecation(self): - with support.check_warnings(('',DeprecationWarning)): + with warnings_helper.check_warnings(('',DeprecationWarning)): urllib.request.URLopener() @unittest.skipUnless(ssl, "ssl module required") def test_cafile_and_context(self): context = ssl.create_default_context() - with support.check_warnings(('', DeprecationWarning)): + with warnings_helper.check_warnings(('', DeprecationWarning)): with self.assertRaises(ValueError): urllib.request.urlopen( "https://localhost", cafile="/nonexistent/path", context=context @@ -699,10 +701,10 @@ class urlretrieve_FileTests(unittest.TestCase): self.tempFiles = [] # Create a temporary file. - self.registerFileForCleanUp(support.TESTFN) + self.registerFileForCleanUp(os_helper.TESTFN) self.text = b'testing urllib.urlretrieve' try: - FILE = open(support.TESTFN, 'wb') + FILE = open(os_helper.TESTFN, 'wb') FILE.write(self.text) FILE.close() finally: @@ -745,18 +747,18 @@ class urlretrieve_FileTests(unittest.TestCase): def test_basic(self): # Make sure that a local file just gets its own location returned and # a headers value is returned. - result = urllib.request.urlretrieve("file:%s" % support.TESTFN) - self.assertEqual(result[0], support.TESTFN) + result = urllib.request.urlretrieve("file:%s" % os_helper.TESTFN) + self.assertEqual(result[0], os_helper.TESTFN) self.assertIsInstance(result[1], email.message.Message, "did not get an email.message.Message instance " "as second returned value") def test_copy(self): # Test that setting the filename argument works. - second_temp = "%s.2" % support.TESTFN + second_temp = "%s.2" % os_helper.TESTFN self.registerFileForCleanUp(second_temp) result = urllib.request.urlretrieve(self.constructLocalFileUrl( - support.TESTFN), second_temp) + os_helper.TESTFN), second_temp) self.assertEqual(second_temp, result[0]) self.assertTrue(os.path.exists(second_temp), "copy of the file was not " "made") @@ -777,10 +779,10 @@ class urlretrieve_FileTests(unittest.TestCase): self.assertIsInstance(file_size, int) self.assertEqual(block_count, count_holder[0]) count_holder[0] = count_holder[0] + 1 - second_temp = "%s.2" % support.TESTFN + second_temp = "%s.2" % os_helper.TESTFN self.registerFileForCleanUp(second_temp) urllib.request.urlretrieve( - self.constructLocalFileUrl(support.TESTFN), + self.constructLocalFileUrl(os_helper.TESTFN), second_temp, hooktester) def test_reporthook_0_bytes(self): @@ -790,7 +792,7 @@ class urlretrieve_FileTests(unittest.TestCase): _report.append((block_count, block_read_size, file_size)) srcFileName = self.createNewTempFile() urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName), - support.TESTFN, hooktester) + os_helper.TESTFN, hooktester) self.assertEqual(len(report), 1) self.assertEqual(report[0][2], 0) @@ -803,7 +805,7 @@ class urlretrieve_FileTests(unittest.TestCase): _report.append((block_count, block_read_size, file_size)) srcFileName = self.createNewTempFile(b"x" * 5) urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName), - support.TESTFN, hooktester) + os_helper.TESTFN, hooktester) self.assertEqual(len(report), 2) self.assertEqual(report[0][2], 5) self.assertEqual(report[1][2], 5) @@ -817,7 +819,7 @@ class urlretrieve_FileTests(unittest.TestCase): _report.append((block_count, block_read_size, file_size)) srcFileName = self.createNewTempFile(b"x" * 8193) urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName), - support.TESTFN, hooktester) + os_helper.TESTFN, hooktester) self.assertEqual(len(report), 3) self.assertEqual(report[0][2], 8193) self.assertEqual(report[0][1], 8192) @@ -1556,7 +1558,7 @@ class URLopener_Tests(FakeHTTPMixin, unittest.TestCase): class DummyURLopener(urllib.request.URLopener): def open_spam(self, url): return url - with support.check_warnings( + with warnings_helper.check_warnings( ('DummyURLopener style of invoking requests is deprecated.', DeprecationWarning)): self.assertEqual(DummyURLopener().open( @@ -1567,9 +1569,9 @@ class URLopener_Tests(FakeHTTPMixin, unittest.TestCase): "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"), "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/") - @support.ignore_warnings(category=DeprecationWarning) + @warnings_helper.ignore_warnings(category=DeprecationWarning) def test_urlopener_retrieve_file(self): - with support.temp_dir() as tmpdir: + with os_helper.temp_dir() as tmpdir: fd, tmpfile = tempfile.mkstemp(dir=tmpdir) os.close(fd) fileurl = "file:" + urllib.request.pathname2url(tmpfile) @@ -1577,7 +1579,7 @@ class URLopener_Tests(FakeHTTPMixin, unittest.TestCase): # Some buildbots have TEMP folder that uses a lowercase drive letter. self.assertEqual(os.path.normcase(filename), os.path.normcase(tmpfile)) - @support.ignore_warnings(category=DeprecationWarning) + @warnings_helper.ignore_warnings(category=DeprecationWarning) def test_urlopener_retrieve_remote(self): url = "http://www.python.org/file.txt" self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!") @@ -1585,7 +1587,7 @@ class URLopener_Tests(FakeHTTPMixin, unittest.TestCase): filename, _ = urllib.request.URLopener().retrieve(url) self.assertEqual(os.path.splitext(filename)[1], ".txt") - @support.ignore_warnings(category=DeprecationWarning) + @warnings_helper.ignore_warnings(category=DeprecationWarning) def test_local_file_open(self): # bpo-35907, CVE-2019-9948: urllib must reject local_file:// scheme class DummyURLopener(urllib.request.URLopener): |