summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_urllib2.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-09-06 18:41:39 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-09-06 18:41:39 (GMT)
commitf54c350160c16cdaf9f692e0f61ef062d4f379f4 (patch)
tree2d75c60bdc81c8837372c9e61e5f9f471c5f57a5 /Lib/test/test_urllib2.py
parent1d52096d1466ed771c22a2d97e8cae6935fcdf8e (diff)
downloadcpython-f54c350160c16cdaf9f692e0f61ef062d4f379f4.zip
cpython-f54c350160c16cdaf9f692e0f61ef062d4f379f4.tar.gz
cpython-f54c350160c16cdaf9f692e0f61ef062d4f379f4.tar.bz2
Issue #19524: Fixed resource leak in the HTTP connection when an invalid
response is received. Patch by Martin Panter.
Diffstat (limited to 'Lib/test/test_urllib2.py')
-rw-r--r--Lib/test/test_urllib2.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index 7620876..9ea39a4 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -1,5 +1,6 @@
import unittest
from test import support
+from test import test_urllib
import os
import io
@@ -13,6 +14,7 @@ import urllib.request
from urllib.request import Request, OpenerDirector, _parse_proxy, _proxy_bypass_macosx_sysconf
from urllib.parse import urlparse
import urllib.error
+import http.client
# XXX
# Request
@@ -1393,6 +1395,33 @@ class HandlerTests(unittest.TestCase):
self.assertEqual(len(http_handler.requests), 1)
self.assertFalse(http_handler.requests[0].has_header(auth_header))
+ def test_http_closed(self):
+ """Test the connection is cleaned up when the response is closed"""
+ for (transfer, data) in (
+ ("Connection: close", b"data"),
+ ("Transfer-Encoding: chunked", b"4\r\ndata\r\n0\r\n\r\n"),
+ ("Content-Length: 4", b"data"),
+ ):
+ header = "HTTP/1.1 200 OK\r\n{}\r\n\r\n".format(transfer)
+ conn = test_urllib.fakehttp(header.encode() + data)
+ handler = urllib.request.AbstractHTTPHandler()
+ req = Request("http://dummy/")
+ req.timeout = None
+ with handler.do_open(conn, req) as resp:
+ resp.read()
+ self.assertTrue(conn.fakesock.closed,
+ "Connection not closed with {!r}".format(transfer))
+
+ def test_invalid_closed(self):
+ """Test the connection is cleaned up after an invalid response"""
+ conn = test_urllib.fakehttp(b"")
+ handler = urllib.request.AbstractHTTPHandler()
+ req = Request("http://dummy/")
+ req.timeout = None
+ with self.assertRaises(http.client.BadStatusLine):
+ handler.do_open(conn, req)
+ self.assertTrue(conn.fakesock.closed, "Connection not closed")
+
class MiscTests(unittest.TestCase):