From 8f77d3b1b692272f4ed668a4c51eca7621859925 Mon Sep 17 00:00:00 2001 From: elciok Date: Mon, 1 Nov 2021 14:37:33 -0300 Subject: [PATCH] Stop reencoding URL when calling with_port() (#623) * Stop reencoding URL when calling with_port() * Added change note * Fixing CHANGES/623.bugfix.rst code formatting. Co-authored-by: Sviatoslav Sydorenko Co-authored-by: Elcio Nakashima Co-authored-by: Sviatoslav Sydorenko --- CHANGES/623.bugfix.rst | 1 + tests/test_url_update_netloc.py | 5 +++++ yarl/_url.py | 4 +--- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 CHANGES/623.bugfix.rst diff --git a/CHANGES/623.bugfix.rst b/CHANGES/623.bugfix.rst new file mode 100644 index 000000000..fb2d39d6a --- /dev/null +++ b/CHANGES/623.bugfix.rst @@ -0,0 +1 @@ +Changed call in ``with_port()`` to stop reencoding parts of the URL that were already encoded. diff --git a/tests/test_url_update_netloc.py b/tests/test_url_update_netloc.py index 1c9ba5c0c..0dfc43039 100644 --- a/tests/test_url_update_netloc.py +++ b/tests/test_url_update_netloc.py @@ -202,6 +202,11 @@ def test_with_port_keeps_query_and_fragment(): assert str(url.with_port(8888)) == "http://example.com:8888/?a=1#frag" +def test_with_port_percent_encoded(): + url = URL("http://user%name:pass%word@example.com/") + assert str(url.with_port(808)) == "http://user%25name:pass%25word@example.com:808/" + + def test_with_port_for_relative_url(): with pytest.raises(ValueError): URL("path/to").with_port(1234) diff --git a/yarl/_url.py b/yarl/_url.py index e5c1e2ce9..bb33fce3a 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -883,9 +883,7 @@ def with_port(self, port): val = self._val return URL( self._val._replace( - netloc=self._make_netloc( - val.username, val.password, val.hostname, port, encode=True - ) + netloc=self._make_netloc(val.username, val.password, val.hostname, port) ), encoded=True, )