Skip to content

Commit

Permalink
feature: add http_status_code to internet provider
Browse files Browse the repository at this point in the history
* feature: HTTP status codes

* feature: HTTP status codes

---------

Co-authored-by: Graham Knapp <[email protected]>
  • Loading branch information
dancergraham and GrahamKnappAcernis authored Jan 22, 2024
1 parent 0c55d3b commit 0ce6220
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
82 changes: 82 additions & 0 deletions faker/providers/internet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
Expand Down Expand Up @@ -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:

Check failure on line 376 in faker/providers/internet/__init__.py

View workflow job for this annotation

GitHub Actions / typing (3.10)

Function is missing a type
"""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.
Expand Down
10 changes: 10 additions & 0 deletions tests/providers/test_internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from validators import domain as validate_domain
from validators import email as validate_email

from faker import Generator

Check failure on line 13 in tests/providers/test_internet.py

View workflow job for this annotation

GitHub Actions / flake8

'faker.Generator' imported but unused
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
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 0ce6220

Please sign in to comment.