Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions starlette/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def port(self) -> int | None:
def is_secure(self) -> bool:
return self.scheme in ("https", "wss")

def relative_url(self) -> URL:
"""
Get a URL that only includes url path, query string and fragment.
"""
return self.__class__(path=self.path, query=self.query, fragment=self.fragment)

def replace(self, **kwargs: typing.Any) -> URL:
if "username" in kwargs or "password" in kwargs or "hostname" in kwargs or "port" in kwargs:
hostname = kwargs.pop("hostname", None)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ def test_hidden_password() -> None:
assert repr(u) == "URL('https://username:********@example.org/path/to/somewhere')"


def test_relative_url() -> None:
u = URL("https://example.org/path/to/somewhere")
assert u.relative_url() == URL("/path/to/somewhere")

u = URL("https://username:[email protected]/path/to/somewhere?abc=123")
assert u.relative_url() == URL("/path/to/somewhere?abc=123")

u = URL("https://[fe::2]:12345/path/to/somewhere?abc=123#anchor")
assert u.relative_url() == URL("/path/to/somewhere?abc=123#anchor")


def test_csv() -> None:
csv = CommaSeparatedStrings('"localhost", "127.0.0.1", 0.0.0.0')
assert list(csv) == ["localhost", "127.0.0.1", "0.0.0.0"]
Expand Down