From 58917d71719d1e4d79c8f4d803f4ed3f583e46fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Busche-Rittich?= Date: Mon, 8 May 2023 12:47:15 +0200 Subject: [PATCH] percent-encode path components in add_path (#104) --- src/rfc3986/builder.py | 12 ++++++++---- tests/test_builder.py | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/rfc3986/builder.py b/src/rfc3986/builder.py index 2826b74..65fb806 100644 --- a/src/rfc3986/builder.py +++ b/src/rfc3986/builder.py @@ -211,7 +211,7 @@ def add_port(self, port): fragment=self.fragment, ) - def add_path(self, path): + def add_path(self, path, encoding="utf-8"): """Add a path to the URI. .. code-block:: python @@ -233,15 +233,19 @@ def add_path(self, path): userinfo=self.userinfo, host=self.host, port=self.port, - path=normalizers.normalize_path(path), + path=normalizers.normalize_path( + normalizers.encode_component(path, encoding) + ), query=self.query, fragment=self.fragment, ) - def extend_path(self, path): + def extend_path(self, path, encoding="utf-8"): """Extend the existing path value with the provided value. .. versionadded:: 1.5.0 + .. versionchanged:: 2.0.0 + Added encoding (see Github issue 104) .. code-block:: python @@ -265,7 +269,7 @@ def extend_path(self, path): existing_path = self.path or "" path = "{}/{}".format(existing_path.rstrip("/"), path.lstrip("/")) - return self.add_path(path) + return self.add_path(path, encoding) def add_query_from(self, query_items): """Generate and add a query a dictionary or list of tuples. diff --git a/tests/test_builder.py b/tests/test_builder.py index 338b3a2..893a268 100644 --- a/tests/test_builder.py +++ b/tests/test_builder.py @@ -231,6 +231,16 @@ def test_add_fragment(): "/sigmavirus24", "/users/sigmavirus24", ), + ( + "https://api.github.com/user s", + "/sigmavirus24", + "/user%20s/sigmavirus24", + ), + ( + "https://api.github.com/users", + "/sigmavirus 24", + "/users/sigmavirus%2024", + ), ], ) def test_extend_path(uri, extend_with, expected_path):