]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-71339: Use new assertion methods in the urllib tests (GH-129056) (GH-132499)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 22 May 2025 09:40:19 +0000 (11:40 +0200)
committerGitHub <noreply@github.com>
Thu, 22 May 2025 09:40:19 +0000 (09:40 +0000)
(cherry picked from commit f98b9b4cbb7905c9af45718505389d171ca3c590)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_urllib.py
Lib/test/test_urllib2.py
Lib/test/test_urllib2_localnet.py
Lib/test/test_urllibnet.py
Lib/test/test_urlparse.py

index beb18f88c4c6e444c898007300566089bb839094..7e3607842fdbdda89f9666a24a9a824152fa097c 100644 (file)
@@ -12,6 +12,7 @@ from test import support
 from test.support import os_helper
 from test.support import socket_helper
 from test.support import warnings_helper
+from test.support.testcase import ExtraAssertions
 import os
 try:
     import ssl
@@ -139,7 +140,7 @@ class FakeFTPMixin(object):
         urllib.request.ftpwrapper = self._ftpwrapper_class
 
 
-class urlopen_FileTests(unittest.TestCase):
+class urlopen_FileTests(unittest.TestCase, ExtraAssertions):
     """Test urlopen() opening a temporary file.
 
     Try to test as much functionality as possible so as to cut down on reliance
@@ -169,9 +170,7 @@ class urlopen_FileTests(unittest.TestCase):
         # Make sure object returned by urlopen() has the specified methods
         for attr in ("read", "readline", "readlines", "fileno",
                      "close", "info", "geturl", "getcode", "__iter__"):
-            self.assertTrue(hasattr(self.returned_obj, attr),
-                         "object returned by urlopen() lacks %s attribute" %
-                         attr)
+            self.assertHasAttr(self.returned_obj, attr)
 
     def test_read(self):
         self.assertEqual(self.text, self.returned_obj.read())
@@ -601,7 +600,7 @@ Connection: close
             urllib.request.URLopener()
 
 
-class urlopen_DataTests(unittest.TestCase):
+class urlopen_DataTests(unittest.TestCase, ExtraAssertions):
     """Test urlopen() opening a data URL."""
 
     def setUp(self):
@@ -640,9 +639,7 @@ class urlopen_DataTests(unittest.TestCase):
         # Make sure object returned by urlopen() has the specified methods
         for attr in ("read", "readline", "readlines",
                      "close", "info", "geturl", "getcode", "__iter__"):
-            self.assertTrue(hasattr(self.text_url_resp, attr),
-                         "object returned by urlopen() lacks %s attribute" %
-                         attr)
+            self.assertHasAttr(self.text_url_resp, attr)
 
     def test_info(self):
         self.assertIsInstance(self.text_url_resp.info(), email.message.Message)
index 9ceba05a6e356a1cd469d2cd4ae3bb509d8bec49..725fe3493c58ae7512ae2fdd74a13d006203aeb7 100644 (file)
@@ -3,6 +3,7 @@ from test import support
 from test.support import os_helper
 from test.support import requires_subprocess
 from test.support import warnings_helper
+from test.support.testcase import ExtraAssertions
 from test import test_urllib
 from unittest import mock
 
@@ -724,7 +725,7 @@ def sanepathname2url(path):
     return urlpath
 
 
-class HandlerTests(unittest.TestCase):
+class HandlerTests(unittest.TestCase, ExtraAssertions):
 
     def test_ftp(self):
         class MockFTPWrapper:
@@ -1179,15 +1180,15 @@ class HandlerTests(unittest.TestCase):
         r = MockResponse(200, "OK", {}, "", url)
         newr = h.http_response(req, r)
         self.assertIs(r, newr)
-        self.assertFalse(hasattr(o, "proto"))  # o.error not called
+        self.assertNotHasAttr(o, "proto")  # o.error not called
         r = MockResponse(202, "Accepted", {}, "", url)
         newr = h.http_response(req, r)
         self.assertIs(r, newr)
-        self.assertFalse(hasattr(o, "proto"))  # o.error not called
+        self.assertNotHasAttr(o, "proto")  # o.error not called
         r = MockResponse(206, "Partial content", {}, "", url)
         newr = h.http_response(req, r)
         self.assertIs(r, newr)
-        self.assertFalse(hasattr(o, "proto"))  # o.error not called
+        self.assertNotHasAttr(o, "proto")  # o.error not called
         # anything else calls o.error (and MockOpener returns None, here)
         r = MockResponse(502, "Bad gateway", {}, "", url)
         self.assertIsNone(h.http_response(req, r))
@@ -1402,7 +1403,7 @@ class HandlerTests(unittest.TestCase):
                 response = opener.open('http://example.com/')
                 expected = b'GET ' + result + b' '
                 request = handler.last_buf
-                self.assertTrue(request.startswith(expected), repr(request))
+                self.assertStartsWith(request, expected)
 
     def test_redirect_head_request(self):
         from_url = "http://example.com/a.html"
@@ -1833,7 +1834,7 @@ class HandlerTests(unittest.TestCase):
         self.assertTrue(conn.fakesock.closed, "Connection not closed")
 
 
-class MiscTests(unittest.TestCase):
+class MiscTests(unittest.TestCase, ExtraAssertions):
 
     def opener_has_handler(self, opener, handler_class):
         self.assertTrue(any(h.__class__ == handler_class
@@ -1892,9 +1893,9 @@ class MiscTests(unittest.TestCase):
         url = code = fp = None
         hdrs = 'Content-Length: 42'
         err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
-        self.assertTrue(hasattr(err, 'reason'))
+        self.assertHasAttr(err, 'reason')
         self.assertEqual(err.reason, 'something bad happened')
-        self.assertTrue(hasattr(err, 'headers'))
+        self.assertHasAttr(err, 'headers')
         self.assertEqual(err.headers, 'Content-Length: 42')
         expected_errmsg = 'HTTP Error %s: %s' % (err.code, err.msg)
         self.assertEqual(str(err), expected_errmsg)
index 9cb15d61c2ad4d3cf5ea419d72dc8128ce7e46c2..23aae1704ee6d638b7e2343fe04548adf4c2a389 100644 (file)
@@ -11,6 +11,7 @@ import hashlib
 from test import support
 from test.support import hashlib_helper
 from test.support import threading_helper
+from test.support.testcase import ExtraAssertions
 
 try:
     import ssl
@@ -442,7 +443,7 @@ def GetRequestHandler(responses):
     return FakeHTTPRequestHandler
 
 
-class TestUrlopen(unittest.TestCase):
+class TestUrlopen(unittest.TestCase, ExtraAssertions):
     """Tests urllib.request.urlopen using the network.
 
     These tests are not exhaustive.  Assuming that testing using files does a
@@ -606,8 +607,7 @@ class TestUrlopen(unittest.TestCase):
         handler = self.start_server()
         with urllib.request.urlopen("http://localhost:%s" % handler.port) as open_url:
             for attr in ("read", "close", "info", "geturl"):
-                self.assertTrue(hasattr(open_url, attr), "object returned from "
-                             "urlopen lacks the %s attribute" % attr)
+                self.assertHasAttr(open_url, attr)
             self.assertTrue(open_url.read(), "calling 'read' failed")
 
     def test_info(self):
index 49a3b5afdebb2f684214e9795c5e48ec36c0aded..6733fe9c6eaaf7e030edeb70e863b1b1f96d460c 100644 (file)
@@ -2,6 +2,7 @@ import unittest
 from test import support
 from test.support import os_helper
 from test.support import socket_helper
+from test.support.testcase import ExtraAssertions
 
 import contextlib
 import socket
@@ -34,7 +35,7 @@ class URLTimeoutTest(unittest.TestCase):
             f.read()
 
 
-class urlopenNetworkTests(unittest.TestCase):
+class urlopenNetworkTests(unittest.TestCase, ExtraAssertions):
     """Tests urllib.request.urlopen using the network.
 
     These tests are not exhaustive.  Assuming that testing using files does a
@@ -70,8 +71,7 @@ class urlopenNetworkTests(unittest.TestCase):
         with self.urlopen(self.url) as open_url:
             for attr in ("read", "readline", "readlines", "fileno", "close",
                          "info", "geturl"):
-                self.assertTrue(hasattr(open_url, attr), "object returned from "
-                                "urlopen lacks the %s attribute" % attr)
+                self.assertHasAttr(open_url, attr)
             self.assertTrue(open_url.read(), "calling 'read' failed")
 
     def test_readlines(self):
index 5e429b9259fee72480b4a829d05d133e5e0a1090..1fa27257c3c423cf47c195d6512011bebc2b106d 100644 (file)
@@ -2,6 +2,7 @@ import sys
 import unicodedata
 import unittest
 import urllib.parse
+from test.support.testcase import ExtraAssertions
 
 RFC1808_BASE = "http://a/b/c/d;p?q#f"
 RFC2396_BASE = "http://a/b/c/d;p?q"
@@ -101,7 +102,7 @@ parse_qs_test_cases = [
     (b"%81=%A9", {b'\x81': [b'\xa9']}),
 ]
 
-class UrlParseTestCase(unittest.TestCase):
+class UrlParseTestCase(unittest.TestCase, ExtraAssertions):
 
     def checkRoundtrips(self, url, parsed, split, url2=None):
         if url2 is None:
@@ -1033,14 +1034,13 @@ class UrlParseTestCase(unittest.TestCase):
                 with self.subTest(url=url, function=func):
                     result = func(url, allow_fragments=False)
                     self.assertEqual(result.fragment, "")
-                    self.assertTrue(
-                            getattr(result, attr).endswith("#" + expected_frag))
+                    self.assertEndsWith(getattr(result, attr),
+                                        "#" + expected_frag)
                     self.assertEqual(func(url, "", False).fragment, "")
 
                     result = func(url, allow_fragments=True)
                     self.assertEqual(result.fragment, expected_frag)
-                    self.assertFalse(
-                            getattr(result, attr).endswith(expected_frag))
+                    self.assertNotEndsWith(getattr(result, attr), expected_frag)
                     self.assertEqual(func(url, "", True).fragment,
                                      expected_frag)
                     self.assertEqual(func(url).fragment, expected_frag)