Skip to content

Commit 5c2af26

Browse files
authored
Small cleanups to encode_url (#1405)
1 parent 0113797 commit 5c2af26

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

tests/test_url.py

+22
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,7 @@ def test_parsing_populates_cache():
21832183
assert url._cache["raw_query_string"] == "a=b"
21842184
assert url._cache["raw_fragment"] == "frag"
21852185
assert url._cache["scheme"] == "http"
2186+
assert url._cache["raw_path"] == "/path"
21862187
assert url.raw_user == "user"
21872188
assert url.raw_password == "password"
21882189
assert url.raw_host == "example.com"
@@ -2198,13 +2199,34 @@ def test_parsing_populates_cache():
21982199
assert url.raw_query_string == "a=b"
21992200
assert url.raw_fragment == "frag"
22002201
assert url.scheme == "http"
2202+
assert url.raw_path == "/path"
22012203
assert url._cache["raw_user"] == "user"
22022204
assert url._cache["raw_password"] == "password"
22032205
assert url._cache["raw_host"] == "example.com"
22042206
assert url._cache["explicit_port"] == 80
22052207
assert url._cache["raw_query_string"] == "a=b"
22062208
assert url._cache["raw_fragment"] == "frag"
22072209
assert url._cache["scheme"] == "http"
2210+
assert url._cache["raw_path"] == "/path"
2211+
2212+
2213+
def test_relative_url_populates_cache():
2214+
"""Test that parsing a relative URL populates the cache."""
2215+
url = URL(".")
2216+
assert url._cache["raw_query_string"] == ""
2217+
assert url._cache["raw_fragment"] == ""
2218+
assert url._cache["scheme"] == ""
2219+
assert url._cache["raw_path"] == "."
2220+
2221+
2222+
def test_parsing_populates_cache_for_single_dot():
2223+
"""Test that parsing a URL populates the cache for a single dot path."""
2224+
url = URL("http://example.com/.")
2225+
# raw_path should be normalized to "/"
2226+
assert url._cache["raw_path"] == "/"
2227+
assert url._cache["raw_host"] == "example.com"
2228+
assert url._cache["scheme"] == "http"
2229+
assert url.raw_path == "/"
22082230

22092231

22102232
@pytest.mark.parametrize(

yarl/_url.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,18 @@ def encode_url(url_str: str) -> "URL":
182182

183183
if path:
184184
path = PATH_REQUOTER(path)
185-
if netloc:
186-
if "." in path:
187-
path = normalize_path(path)
185+
if netloc and "." in path:
186+
path = normalize_path(path)
187+
if query:
188+
query = QUERY_REQUOTER(query)
189+
if fragment:
190+
fragment = FRAGMENT_REQUOTER(fragment)
188191

189-
query = QUERY_REQUOTER(query) if query else query
190-
fragment = FRAGMENT_REQUOTER(fragment) if fragment else fragment
191192
cache["scheme"] = scheme
193+
cache["raw_path"] = "/" if not path and netloc else path
192194
cache["raw_query_string"] = query
193195
cache["raw_fragment"] = fragment
196+
194197
self = object.__new__(URL)
195198
self._scheme = scheme
196199
self._netloc = netloc

0 commit comments

Comments
 (0)