Skip to content

Commit 8e98175

Browse files
authored
bpo-46648: Rewrite test_urllib2.test_issue16464() with a local HTTP server (GH-31186)
Re-enable test_issue16464() of test_urllib2, move it to urllib2_localnet and use the local HTTP server rather than an external HTTP server.
1 parent 59e004a commit 8e98175

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

Lib/test/test_urllib2.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,24 +1788,6 @@ class MyOtherHTTPHandler(urllib.request.HTTPHandler):
17881788
self.opener_has_handler(o, MyHTTPHandler)
17891789
self.opener_has_handler(o, MyOtherHTTPHandler)
17901790

1791-
@unittest.skipUnless(support.is_resource_enabled('network'),
1792-
'test requires network access')
1793-
# bpo-46648: test fails randomly with "http://www.example.com/" URL
1794-
@unittest.skipIf(True, "POST request to http://www.example.com/ fail randomly")
1795-
def test_issue16464(self):
1796-
with socket_helper.transient_internet("http://www.example.com/"):
1797-
opener = urllib.request.build_opener()
1798-
request = urllib.request.Request("http://www.example.com/")
1799-
self.assertEqual(None, request.data)
1800-
1801-
opener.open(request, "1".encode("us-ascii"))
1802-
self.assertEqual(b"1", request.data)
1803-
self.assertEqual("1", request.get_header("Content-length"))
1804-
1805-
opener.open(request, "1234567890".encode("us-ascii"))
1806-
self.assertEqual(b"1234567890", request.data)
1807-
self.assertEqual("10", request.get_header("Content-length"))
1808-
18091791
def test_HTTPError_interface(self):
18101792
"""
18111793
Issue 13211 reveals that HTTPError didn't implement the URLError

Lib/test/test_urllib2_localnet.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,24 @@ def test_line_iteration(self):
660660
(index, len(lines[index]), len(line)))
661661
self.assertEqual(index + 1, len(lines))
662662

663+
def test_issue16464(self):
664+
# See https://bugs.python.org/issue16464
665+
# and https://bugs.python.org/issue46648
666+
handler = self.start_server([
667+
(200, [], b'any'),
668+
(200, [], b'any'),
669+
])
670+
opener = urllib.request.build_opener()
671+
request = urllib.request.Request("http://localhost:%s" % handler.port)
672+
self.assertEqual(None, request.data)
673+
674+
opener.open(request, "1".encode("us-ascii"))
675+
self.assertEqual(b"1", request.data)
676+
self.assertEqual("1", request.get_header("Content-length"))
677+
678+
opener.open(request, "1234567890".encode("us-ascii"))
679+
self.assertEqual(b"1234567890", request.data)
680+
self.assertEqual("10", request.get_header("Content-length"))
663681

664682
def setUpModule():
665683
thread_info = threading_helper.threading_setup()

0 commit comments

Comments
 (0)