Skip to content

Commit

Permalink
Small cleanups to encode_url (#1405)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 30, 2024
1 parent 0113797 commit 5c2af26
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
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)
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

0 comments on commit 5c2af26

Please sign in to comment.