mirror of https://github.com/python/cpython
Closes #26620: Fix ResourceWarning in test_urllib2_localnet
* Use context manager on urllib objects to ensure that they are closed on error * Use self.addCleanup() to cleanup resources even if a test is interrupted with CTRL+c
This commit is contained in:
parent
e321274b2e
commit
bc6b72ed06
|
@ -289,12 +289,12 @@ class BasicAuthTests(unittest.TestCase):
|
|||
def http_server_with_basic_auth_handler(*args, **kwargs):
|
||||
return BasicAuthHandler(*args, **kwargs)
|
||||
self.server = LoopbackHttpServerThread(http_server_with_basic_auth_handler)
|
||||
self.addCleanup(self.server.stop)
|
||||
self.server_url = 'http://127.0.0.1:%s' % self.server.port
|
||||
self.server.start()
|
||||
self.server.ready.wait()
|
||||
|
||||
def tearDown(self):
|
||||
self.server.stop()
|
||||
super(BasicAuthTests, self).tearDown()
|
||||
|
||||
def test_basic_auth_success(self):
|
||||
|
@ -438,17 +438,13 @@ class TestUrlopen(unittest.TestCase):
|
|||
|
||||
def setUp(self):
|
||||
super(TestUrlopen, self).setUp()
|
||||
# Ignore proxies for localhost tests.
|
||||
self.old_environ = os.environ.copy()
|
||||
os.environ['NO_PROXY'] = '*'
|
||||
self.server = None
|
||||
|
||||
def tearDown(self):
|
||||
if self.server is not None:
|
||||
self.server.stop()
|
||||
os.environ.clear()
|
||||
os.environ.update(self.old_environ)
|
||||
super(TestUrlopen, self).tearDown()
|
||||
# Ignore proxies for localhost tests.
|
||||
def restore_environ(old_environ):
|
||||
os.environ.clear()
|
||||
os.environ.update(old_environ)
|
||||
self.addCleanup(restore_environ, os.environ.copy())
|
||||
os.environ['NO_PROXY'] = '*'
|
||||
|
||||
def urlopen(self, url, data=None, **kwargs):
|
||||
l = []
|
||||
|
@ -469,6 +465,7 @@ class TestUrlopen(unittest.TestCase):
|
|||
handler = GetRequestHandler(responses)
|
||||
|
||||
self.server = LoopbackHttpServerThread(handler)
|
||||
self.addCleanup(self.server.stop)
|
||||
self.server.start()
|
||||
self.server.ready.wait()
|
||||
port = self.server.port
|
||||
|
@ -592,7 +589,8 @@ class TestUrlopen(unittest.TestCase):
|
|||
handler = self.start_server()
|
||||
req = urllib.request.Request("http://localhost:%s/" % handler.port,
|
||||
headers={"Range": "bytes=20-39"})
|
||||
urllib.request.urlopen(req)
|
||||
with urllib.request.urlopen(req):
|
||||
pass
|
||||
self.assertEqual(handler.headers_received["Range"], "bytes=20-39")
|
||||
|
||||
def test_basic(self):
|
||||
|
@ -608,22 +606,21 @@ class TestUrlopen(unittest.TestCase):
|
|||
|
||||
def test_info(self):
|
||||
handler = self.start_server()
|
||||
try:
|
||||
open_url = urllib.request.urlopen(
|
||||
"http://localhost:%s" % handler.port)
|
||||
open_url = urllib.request.urlopen(
|
||||
"http://localhost:%s" % handler.port)
|
||||
with open_url:
|
||||
info_obj = open_url.info()
|
||||
self.assertIsInstance(info_obj, email.message.Message,
|
||||
"object returned by 'info' is not an "
|
||||
"instance of email.message.Message")
|
||||
self.assertEqual(info_obj.get_content_subtype(), "plain")
|
||||
finally:
|
||||
self.server.stop()
|
||||
self.assertIsInstance(info_obj, email.message.Message,
|
||||
"object returned by 'info' is not an "
|
||||
"instance of email.message.Message")
|
||||
self.assertEqual(info_obj.get_content_subtype(), "plain")
|
||||
|
||||
def test_geturl(self):
|
||||
# Make sure same URL as opened is returned by geturl.
|
||||
handler = self.start_server()
|
||||
open_url = urllib.request.urlopen("http://localhost:%s" % handler.port)
|
||||
url = open_url.geturl()
|
||||
with open_url:
|
||||
url = open_url.geturl()
|
||||
self.assertEqual(url, "http://localhost:%s" % handler.port)
|
||||
|
||||
def test_iteration(self):
|
||||
|
|
Loading…
Reference in New Issue