Repository: joke2k/faker

Issue:
Idea: Add Http status codes
* Faker version: 22.2.0
* OS: all

Feature suggestion : add http status code to `providers.internet` as a random integer between 100 and 599

source: https://www.rfc-editor.org/rfc/rfc9110#name-status-codes



Discussion:
dancergraham: ouch - segfault in pypy - not sure why ?
fcurella: > ouch - segfault in pypy - not sure why ?

yeah, that's been happening for. while and I'm not sure why, it's been erratic 🤷🏼 

I've re-run the tests and the build is green now

Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index d6c5e210..046cb1b3 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -17,7 +17,7 @@ jobs:
     steps:
     - uses: actions/checkout@v4
     - name: Setup pip cache
-      uses: actions/cache@v3
+      uses: actions/cache@v4
       id: pipcache
       with:
         path: ~/.cache/pip
@@ -42,7 +42,7 @@ jobs:
     steps:
     - uses: actions/checkout@v4
     - name: Setup pip cache
-      uses: actions/cache@v3
+      uses: actions/cache@v4
       id: pipcache
       with:
         path: ~/.cache/pip
@@ -66,7 +66,7 @@ jobs:
     steps:
     - uses: actions/checkout@v4
     - name: Setup pip cache
-      uses: actions/cache@v3
+      uses: actions/cache@v4
       id: pipcache
       with:
         path: ~/.cache/pip
@@ -91,7 +91,7 @@ jobs:
     steps:
     - uses: actions/checkout@v4
     - name: Setup pip cache
-      uses: actions/cache@v3
+      uses: actions/cache@v4
       id: pipcache
       with:
         path: ~/.cache/pip
@@ -115,7 +115,7 @@ jobs:
     steps:
     - uses: actions/checkout@v4
     - name: Setup pip cache
-      uses: actions/cache@v3
+      uses: actions/cache@v4
       id: pipcache
       with:
         path: ~/.cache/pip
@@ -143,7 +143,7 @@ jobs:
     steps:
     - uses: actions/checkout@v4
     - name: Setup pip cache
-      uses: actions/cache@v3
+      uses: actions/cache@v4
       id: pipcache
       with:
         path: ~/.cache/pip
@@ -173,7 +173,7 @@ jobs:
     steps:
       - uses: actions/checkout@v4
       - name: Setup pip cache
-        uses: actions/cache@v3
+        uses: actions/cache@v4
         id: pipcache
         with:
           path: ~/.cache/pip
@@ -215,7 +215,7 @@ jobs:
     steps:
       - uses: actions/checkout@v4
       - name: Setup pip cache
-        uses: actions/cache@v3
+        uses: actions/cache@v4
         id: pipcache
         with:
           path: ~/.cache/pip
@@ -246,7 +246,7 @@ jobs:
     steps:
     - uses: actions/checkout@v4
     - name: Setup pip cache
-      uses: actions/cache@v3
+      uses: actions/cache@v4
       id: pipcache
       with:
         path: ~/.cache/pip
diff --git a/faker/providers/internet/__init__.py b/faker/providers/internet/__init__.py
index 9898c96e..ea2ecaeb 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 @@ class Provider(BaseProvider):
 
         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.


Test patch:
diff --git a/tests/providers/test_internet.py b/tests/providers/test_internet.py
index 7a877ce5..9954cf7e 100644
--- a/tests/providers/test_internet.py
+++ b/tests/providers/test_internet.py
@@ -10,6 +10,7 @@ import pytest
 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 @@ class TestInternetProvider:
 
         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()