mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #19524: Fixed resource leak in the HTTP connection when an invalid
response is received. Patch by Martin Panter.
This commit is contained in:
parent
1d52096d14
commit
f54c350160
5 changed files with 86 additions and 45 deletions
|
@ -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):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue