Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

percent-encode path components in add_path (#104) #106

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/rfc3986/builder.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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