From 0ce62203c4202d55e8562fa0e4ff4099e90a6693 Mon Sep 17 00:00:00 2001 From: Graham Knapp <32717635+dancergraham@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:53:24 +0100 Subject: [PATCH] feature: add `http_status_code` to internet provider * feature: HTTP status codes * feature: HTTP status codes --------- Co-authored-by: Graham Knapp --- faker/providers/internet/__init__.py | 82 ++++++++++++++++++++++++++++ tests/providers/test_internet.py | 10 ++++ 2 files changed, 92 insertions(+) diff --git a/faker/providers/internet/__init__.py b/faker/providers/internet/__init__.py index 9898c96eb2..ea2ecaeb10 100644 --- a/faker/providers/internet/__init__.py +++ b/faker/providers/internet/__init__.py @@ -131,6 +131,71 @@ class Provider(BaseProvider): "TRACE", "PATCH", ) + http_assigned_codes: ElementsType[int] = ( + 100, + 101, + 100, + 101, + 102, + 103, + 200, + 201, + 202, + 203, + 204, + 205, + 206, + 207, + 208, + 226, + 300, + 301, + 302, + 303, + 304, + 305, + 307, + 308, + 400, + 401, + 402, + 403, + 404, + 405, + 406, + 407, + 408, + 409, + 410, + 411, + 412, + 413, + 414, + 415, + 416, + 417, + 421, + 422, + 423, + 424, + 425, + 426, + 428, + 429, + 431, + 451, + 500, + 501, + 502, + 503, + 504, + 505, + 506, + 507, + 508, + 510, + 511, + ) user_name_formats: ElementsType[str] = ( "{{last_name}}.{{first_name}}", @@ -308,6 +373,23 @@ def http_method(self) -> str: return self.random_element(self.http_methods) + def http_status_code(self, include_unassigned=True) -> int: + """Returns random HTTP status code + https://www.rfc-editor.org/rfc/rfc9110#name-status-codes + :param include_unassigned: Whether to include status codes which have + not yet been assigned or are unused + + :return: a random three digit status code + :rtype: int + + :example: 404 + + """ + if include_unassigned: + return self.random_int(min=100, max=599) + else: + return self.random_element(self.http_assigned_codes) + def url(self, schemes: Optional[List[str]] = None) -> str: """ :param schemes: a list of strings to use as schemes, one will chosen randomly. diff --git a/tests/providers/test_internet.py b/tests/providers/test_internet.py index 7a877ce5ab..9954cf7e15 100644 --- a/tests/providers/test_internet.py +++ b/tests/providers/test_internet.py @@ -10,6 +10,7 @@ from validators import domain as validate_domain from validators import email as validate_email +from faker import Generator from faker.providers.internet import Provider as InternetProvider from faker.providers.internet.az_AZ import Provider as AzAzInternetProvider from faker.providers.internet.en_GB import Provider as EnGbInternetProvider @@ -339,6 +340,15 @@ def test_http_method(self, faker, num_samples): assert expected_methods == sorted(got_methods) + def test_http_status_code(self, faker, num_samples): + provider = InternetProvider(faker) + status_code = provider.http_status_code() + assert isinstance(status_code, int) + assert 100 <= status_code <= 599 + status_code = provider.http_status_code(include_unassigned=False) + assert isinstance(status_code, int) + assert 100 <= status_code <= 599 + def test_dga(self, faker): assert faker.dga() != faker.dga()