Skip to content

Commit

Permalink
Add schemes and deep argument to uri method
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella committed Aug 31, 2023
1 parent 83b9dae commit 33e36b1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
26 changes: 16 additions & 10 deletions faker/providers/internet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@ class Provider(BaseProvider):
"www.{{domain_name}}/",
"{{domain_name}}/",
)
uri_formats: ElementsType[str] = (
"{{url}}",
"{{url}}{{uri_page}}/",
"{{url}}{{uri_page}}{{uri_extension}}",
"{{url}}{{uri_path}}/{{uri_page}}/",
"{{url}}{{uri_path}}/{{uri_page}}{{uri_extension}}",
)
image_placeholder_services: ElementsType[str] = (
"https://picsum.photos/{width}/{height}",
"https://dummyimage.com/{width}x{height}",
Expand Down Expand Up @@ -624,9 +617,22 @@ def uri_path(self, deep: Optional[int] = None) -> str:
def uri_extension(self) -> str:
return self.random_element(self.uri_extensions)

def uri(self) -> str:
pattern: str = self.random_element(self.uri_formats)
return self.generator.parse(pattern)
def uri(self, schemes: Optional[List[str]] = None, deep: Optional[int] = None) -> str:
"""
:param schemes: a list of strings to use as schemes, one will chosen randomly.
If None, it will generate http and https uris.
Passing an empty list will result in schemeless uri generation like "://domain.com/index.html".
:param deep: an integer specifying how many path components the URI should have..
:return: a random url string.
"""
if schemes is None:
schemes = ["http", "https"]

pattern: str = f'{self.random_element(schemes) if schemes else ""}://{self.random_element(self.url_formats)}'
path = self.uri_path(deep=deep)
page = self.uri_page()
extension = self.uri_extension()
return f"{self.generator.parse(pattern)}{path}{page}{extension}"

@slugify
def slug(self, value: Optional[str] = None) -> str:
Expand Down
44 changes: 44 additions & 0 deletions tests/providers/test_internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,50 @@ def test_url_empty_schemes_list_generate_schemeless_urls(self, faker):
assert url.startswith("://")


class TestInternetProviderUri:
"""Test internet uri generation"""

@staticmethod
def is_correct_scheme(uri, schemes):
return any(uri.startswith(f"{scheme}://") for scheme in schemes)

def test_uri_default_schemes(self, faker):
for _ in range(100):
uri = faker.uri()
assert self.is_correct_scheme(uri, ["http", "https"])

def test_uri_custom_schemes(self, faker):
schemes_sets = [
["usb"],
["ftp", "file"],
["usb", "telnet", "http"],
]
for _, schemes in zip(range(100), cycle(schemes_sets)):
uri = faker.uri(schemes=schemes)
assert self.is_correct_scheme(uri, schemes)

def test_uri_empty_schemes_list_generate_schemeless_urls(self, faker):
for _ in range(100):
uri = faker.uri(schemes=[])
assert not uri.startswith("http")
assert uri.startswith("://")

def test_uri_extension(self, faker):
uri = faker.uri()
assert "." in uri

def test_uri_component(self, faker):
uri = faker.uri()
assert "/" in uri

def test_uri_deep(self, faker):
uri = faker.uri(deep=1).replace("://", "")
assert uri.count("/") == 1

uri = faker.uri(deep=3).replace("://", "")
assert uri.count("/") == 3


class TestJaJp:
"""Test ja_JP internet provider methods"""

Expand Down

0 comments on commit 33e36b1

Please sign in to comment.