From ac8bb59ace25699e0c6f03f1f45fe12b24aac60c Mon Sep 17 00:00:00 2001 From: Ajob Kustra Date: Sun, 19 Jul 2026 14:29:17 +0200 Subject: [PATCH] [3.13] urllib: Add tests for HTTP errors to complete coverage (GH-154102) * 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 (cherry picked from commit b8ec956716c183430a93929e6415ceed74089af1) Co-authored-by: Ajob Kustra --- Lib/test/test_urllib.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index bfcabeeae5c05e..c6a3157698d3ad 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -514,6 +514,25 @@ def test_redirect_limit_independent(self): 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) @@ -566,6 +585,11 @@ def test_ftp_cache_pruning(self): finally: self.unfakeftp() + def test_url_error_stringified(self): + reason = 'sixseven' + err = urllib.error.URLError(reason) + self.assertEqual(str(err), f'') + def test_userpass_inurl(self): self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!") try: