Skip to content

Commit 1df0ecd

Browse files
[3.15] urllib: Add tests for HTTP errors to complete coverage (GH-154102) (#154120)
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 b8ec956) Co-authored-by: Ajob Kustra <ajob.edward.kustra@cern.ch>
1 parent 3dd4fdc commit 1df0ecd

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lib/test/test_urllib.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,25 @@ def test_redirect_limit_independent(self):
467467
finally:
468468
self.unfakehttp()
469469

470+
def test_http_error_attribute_values(self):
471+
hdrs = {
472+
"Authorization": "Bearer foobar",
473+
"Accept": "application/json"
474+
}
475+
err = urllib.error.HTTPError("http://something", 404, "foo", hdrs, None)
476+
self.assertEqual(err.filename, "http://something")
477+
self.assertEqual(err.code, 404)
478+
self.assertEqual(err.msg, "foo")
479+
self.assertEqual(err.reason, "foo")
480+
self.assertEqual(err.hdrs, hdrs)
481+
self.assertEqual(err.headers, hdrs)
482+
err.close()
483+
484+
def test_http_error_default_fp(self):
485+
err = urllib.error.HTTPError("http://something", 404, "foo", {}, None)
486+
self.assertIsInstance(err.fp, io.BytesIO)
487+
err.close()
488+
470489
def test_empty_socket(self):
471490
# urlopen() raises OSError if the underlying socket does not send any
472491
# data. (#1680230)
@@ -513,6 +532,11 @@ def test_ftp_nonexisting(self):
513532
self.assertFalse(e.exception.filename)
514533
self.assertTrue(e.exception.reason)
515534

535+
def test_url_error_stringified(self):
536+
reason = 'sixseven'
537+
err = urllib.error.URLError(reason)
538+
self.assertEqual(str(err), f'<urlopen error {reason}>')
539+
516540

517541
class urlopen_DataTests(unittest.TestCase):
518542
"""Test urlopen() opening a data URL."""

0 commit comments

Comments
 (0)