]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
urllib: Add tests for HTTP errors to complete coverage (#154102)
authorAjob Kustra <ajob.edward.kustra@cern.ch>
Sun, 19 Jul 2026 12:29:17 +0000 (14:29 +0200)
committerGitHub <noreply@github.com>
Sun, 19 Jul 2026 12:29:17 +0000 (12:29 +0000)
* add test for httperror props such as reason and fp, and stringified urlerror test

* rm unnecessary 'reason' attr test, change url to filename and add reason and headers attr

* separate file pointer test

* prevent resource warning, close httperror exception

* exc > err

Lib/test/test_urllib.py

index 2dd739b77b8e4d1c8d021f22a00b170c9c7fcd6e..1e5f79998e7cab2f01c6cbd4131cd015997c34d6 100644 (file)
@@ -467,6 +467,25 @@ Connection: close
             finally:
                 self.unfakehttp()
 
+    def test_http_error_attribute_values(self):
+        hdrs = {
+            "Authorization": "Bearer foobar",
+            "Accept": "application/json"
+        }
+        err = urllib.error.HTTPError("http://something", 404, "foo", hdrs, None)
+        self.assertEqual(err.filename, "http://something")
+        self.assertEqual(err.code, 404)
+        self.assertEqual(err.msg, "foo")
+        self.assertEqual(err.reason, "foo")
+        self.assertEqual(err.hdrs, hdrs)
+        self.assertEqual(err.headers, hdrs)
+        err.close()
+
+    def test_http_error_default_fp(self):
+        err = urllib.error.HTTPError("http://something", 404, "foo", {}, None)
+        self.assertIsInstance(err.fp, io.BytesIO)
+        err.close()
+
     def test_empty_socket(self):
         # urlopen() raises OSError if the underlying socket does not send any
         # data. (#1680230)
@@ -513,6 +532,11 @@ Connection: close
         self.assertFalse(e.exception.filename)
         self.assertTrue(e.exception.reason)
 
+    def test_url_error_stringified(self):
+        reason = 'sixseven'
+        err = urllib.error.URLError(reason)
+        self.assertEqual(str(err), f'<urlopen error {reason}>')
+
 
 class urlopen_DataTests(unittest.TestCase):
     """Test urlopen() opening a data URL."""