diff options
author | Alexandru Mărășteanu <alexei@users.noreply.github.com> | 2022-08-30 18:11:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-30 18:11:44 (GMT) |
commit | 0ed778835d34bc1f39d2c6cdbc0c1709f6bcfd61 (patch) | |
tree | 30e5ed320372a5d705b156f6b3eb5de335e575be /Lib | |
parent | 13c309f1101dc86ca0138f239d45cb009d0e898d (diff) | |
download | cpython-0ed778835d34bc1f39d2c6cdbc0c1709f6bcfd61.zip cpython-0ed778835d34bc1f39d2c6cdbc0c1709f6bcfd61.tar.gz cpython-0ed778835d34bc1f39d2c6cdbc0c1709f6bcfd61.tar.bz2 |
gh-95149: Enhance `http.HTTPStatus` with properties that indicate the HTTP status category (GH-95453)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/enum.py | 2 | ||||
-rw-r--r-- | Lib/http/__init__.py | 20 | ||||
-rw-r--r-- | Lib/test/test_httplib.py | 45 |
3 files changed, 66 insertions, 1 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 8ef6958..e7375e1 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -1887,7 +1887,7 @@ def _test_simple_enum(checked_enum, simple_enum): else: checked_value = checked_dict[key] simple_value = simple_dict[key] - if callable(checked_value): + if callable(checked_value) or isinstance(checked_value, bltns.property): continue if key == '__doc__': # remove all spaces/tabs diff --git a/Lib/http/__init__.py b/Lib/http/__init__.py index cd2885d..e093a1f 100644 --- a/Lib/http/__init__.py +++ b/Lib/http/__init__.py @@ -31,6 +31,26 @@ class HTTPStatus: obj.description = description return obj + @property + def is_informational(self): + return 100 <= self <= 199 + + @property + def is_success(self): + return 200 <= self <= 299 + + @property + def is_redirection(self): + return 300 <= self <= 399 + + @property + def is_client_error(self): + return 400 <= self <= 499 + + @property + def is_server_error(self): + return 500 <= self <= 599 + # informational CONTINUE = 100, 'Continue', 'Request received, please continue' SWITCHING_PROTOCOLS = (101, 'Switching Protocols', diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index 15dab03..b3d94e0 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -553,6 +553,27 @@ class BasicTest(TestCase): obj.phrase = phrase obj.description = description return obj + + @property + def is_informational(self): + return 100 <= self <= 199 + + @property + def is_success(self): + return 200 <= self <= 299 + + @property + def is_redirection(self): + return 300 <= self <= 399 + + @property + def is_client_error(self): + return 400 <= self <= 499 + + @property + def is_server_error(self): + return 500 <= self <= 599 + # informational CONTINUE = 100, 'Continue', 'Request received, please continue' SWITCHING_PROTOCOLS = (101, 'Switching Protocols', @@ -669,6 +690,30 @@ class BasicTest(TestCase): 'The client needs to authenticate to gain network access') enum._test_simple_enum(CheckedHTTPStatus, HTTPStatus) + def test_httpstatus_range(self): + """Checks that the statuses are in the 100-599 range""" + + for member in HTTPStatus.__members__.values(): + self.assertGreaterEqual(member, 100) + self.assertLessEqual(member, 599) + + def test_httpstatus_category(self): + """Checks that the statuses belong to the standard categories""" + + categories = ( + ((100, 199), "is_informational"), + ((200, 299), "is_success"), + ((300, 399), "is_redirection"), + ((400, 499), "is_client_error"), + ((500, 599), "is_server_error"), + ) + for member in HTTPStatus.__members__.values(): + for (lower, upper), category in categories: + category_indicator = getattr(member, category) + if lower <= member <= upper: + self.assertTrue(category_indicator) + else: + self.assertFalse(category_indicator) def test_status_lines(self): # Test HTTP status lines |