Skip to content

Commit

Permalink
percent-encode path components in add_path (python-hyper#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
André Busche-Rittich committed May 8, 2023
1 parent dda8bea commit 58917d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/rfc3986/builder.py
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_builder.py
Expand Up @@ -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):
Expand Down

0 comments on commit 58917d7

Please sign in to comment.