Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small cleanups to encode_url #1405

Merged
merged 5 commits into from
Oct 30, 2024
Merged
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
22 changes: 22 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,7 @@ def test_parsing_populates_cache():
assert url._cache["raw_query_string"] == "a=b"
assert url._cache["raw_fragment"] == "frag"
assert url._cache["scheme"] == "http"
assert url._cache["raw_path"] == "/path"
assert url.raw_user == "user"
assert url.raw_password == "password"
assert url.raw_host == "example.com"
Expand All @@ -2198,13 +2199,34 @@ def test_parsing_populates_cache():
assert url.raw_query_string == "a=b"
assert url.raw_fragment == "frag"
assert url.scheme == "http"
assert url.raw_path == "/path"
assert url._cache["raw_user"] == "user"
assert url._cache["raw_password"] == "password"
assert url._cache["raw_host"] == "example.com"
assert url._cache["explicit_port"] == 80
assert url._cache["raw_query_string"] == "a=b"
assert url._cache["raw_fragment"] == "frag"
assert url._cache["scheme"] == "http"
assert url._cache["raw_path"] == "/path"


def test_relative_url_populates_cache():
"""Test that parsing a relative URL populates the cache."""
url = URL(".")
assert url._cache["raw_query_string"] == ""
assert url._cache["raw_fragment"] == ""
assert url._cache["scheme"] == ""
assert url._cache["raw_path"] == "."


def test_parsing_populates_cache_for_single_dot():
"""Test that parsing a URL populates the cache for a single dot path."""
url = URL("http://example.com/.")
# raw_path should be normalized to "/"
assert url._cache["raw_path"] == "/"
assert url._cache["raw_host"] == "example.com"
assert url._cache["scheme"] == "http"
assert url.raw_path == "/"


@pytest.mark.parametrize(
Expand Down
13 changes: 8 additions & 5 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,18 @@ def encode_url(url_str: str) -> "URL":

if path:
path = PATH_REQUOTER(path)
if netloc:
if "." in path:
path = normalize_path(path)
if netloc and "." in path:
path = normalize_path(path)
bdraco marked this conversation as resolved.
Show resolved Hide resolved
if query:
query = QUERY_REQUOTER(query)
if fragment:
fragment = FRAGMENT_REQUOTER(fragment)

query = QUERY_REQUOTER(query) if query else query
fragment = FRAGMENT_REQUOTER(fragment) if fragment else fragment
cache["scheme"] = scheme
cache["raw_path"] = "/" if not path and netloc else path
cache["raw_query_string"] = query
cache["raw_fragment"] = fragment

self = object.__new__(URL)
self._scheme = scheme
self._netloc = netloc
Expand Down