]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46648: Rewrite test_urllib2.test_issue16464() with a local HTTP server (GH-31186)
authorNikita Sobolev <mail@sobolevn.me>
Mon, 7 Feb 2022 12:48:43 +0000 (15:48 +0300)
committerGitHub <noreply@github.com>
Mon, 7 Feb 2022 12:48:43 +0000 (13:48 +0100)
Re-enable test_issue16464() of test_urllib2, move it to urllib2_localnet
and use the local HTTP server rather than an external HTTP server.

Lib/test/test_urllib2.py
Lib/test/test_urllib2_localnet.py

index 0936e435a4870ce10b0fc19db934746812381cfa..2c93fcada4387874a2ab5ff07523b2c903a466f9 100644 (file)
@@ -1788,24 +1788,6 @@ class MiscTests(unittest.TestCase):
         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
index 0b2d07ce61d5c784ceeec946bf21b5d59e804ad1..1b2baf2f366b560067317a7e94adc3d85835199c 100644 (file)
@@ -660,6 +660,24 @@ class TestUrlopen(unittest.TestCase):
                              (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 = threading_helper.threading_setup()