[Python-checkins] bpo-46648: Rewrite test_urllib2.test_issue16464() with a local HTTP server (GH-31186)

miss-islington webhook-mailer at python.org
Mon Feb 7 08:14:19 EST 2022


https://github.com/python/cpython/commit/0d74efc54fa811def386d2cde00986204ba18569
commit: 0d74efc54fa811def386d2cde00986204ba18569
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-02-07T05:14:06-08:00
summary:

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.
(cherry picked from commit 8e98175a03fe03d62822d96007a74e5273013764)

Co-authored-by: Nikita Sobolev <mail at sobolevn.me>

files:
M Lib/test/test_urllib2.py
M Lib/test/test_urllib2_localnet.py

diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index d8aab53089bc4..648f65d6257d7 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -1786,24 +1786,6 @@ class MyOtherHTTPHandler(urllib.request.HTTPHandler):
         self.opener_has_handler(o, MyHTTPHandler)
         self.opener_has_handler(o, MyOtherHTTPHandler)
 
-    @unittest.skipUnless(support.is_resource_enabled('network'),
-                         'test requires network access')
-    # bpo-46648: test fails randomly with "http://www.example.com/" URL
-    @unittest.skipIf(True, "POST request to http://www.example.com/ fail randomly")
-    def test_issue16464(self):
-        with socket_helper.transient_internet("http://www.example.com/"):
-            opener = urllib.request.build_opener()
-            request = urllib.request.Request("http://www.example.com/")
-            self.assertEqual(None, request.data)
-
-            opener.open(request, "1".encode("us-ascii"))
-            self.assertEqual(b"1", request.data)
-            self.assertEqual("1", request.get_header("Content-length"))
-
-            opener.open(request, "1234567890".encode("us-ascii"))
-            self.assertEqual(b"1234567890", request.data)
-            self.assertEqual("10", request.get_header("Content-length"))
-
     def test_HTTPError_interface(self):
         """
         Issue 13211 reveals that HTTPError didn't implement the URLError
diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py
index 7c716341c7023..74374c08684da 100644
--- a/Lib/test/test_urllib2_localnet.py
+++ b/Lib/test/test_urllib2_localnet.py
@@ -659,6 +659,24 @@ def test_line_iteration(self):
                              (index, len(lines[index]), len(line)))
         self.assertEqual(index + 1, len(lines))
 
+    def test_issue16464(self):
+        # See https://bugs.python.org/issue16464
+        # and https://bugs.python.org/issue46648
+        handler = self.start_server([
+            (200, [], b'any'),
+            (200, [], b'any'),
+        ])
+        opener = urllib.request.build_opener()
+        request = urllib.request.Request("http://localhost:%s" % handler.port)
+        self.assertEqual(None, request.data)
+
+        opener.open(request, "1".encode("us-ascii"))
+        self.assertEqual(b"1", request.data)
+        self.assertEqual("1", request.get_header("Content-length"))
+
+        opener.open(request, "1234567890".encode("us-ascii"))
+        self.assertEqual(b"1234567890", request.data)
+        self.assertEqual("10", request.get_header("Content-length"))
 
 def setUpModule():
     thread_info = support.threading_setup()



More information about the Python-checkins mailing list