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

fix TestClient to preserve raw URI #2159

Merged
merged 2 commits into from
Jul 18, 2023
Merged
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
1 change: 1 addition & 0 deletions docs/_newsfragments/2157.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`~falcon.testing.TestCase` mimics behavior of real WSGI servers following WSGI spec where is said that ``PATH_INFO`` CGI variable for WSGI app is already percent-decoded. However, this breaks routing if slash (encoded as ``%2F``) is part of path element, not a path separator, as explained in the FAQ :ref:`routing_encoded_slashes`. The workaround based on some WSGI servers' non-standard CGI variables described in :ref:`raw_url_path_recipe` recipe breaks tests because :py:func:`falcon.testing.helpers.create_environ` hard-code CGI variable ``RAW_URI`` to ``/`` instead to real path *before* percent-decoding.
5 changes: 3 additions & 2 deletions falcon/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,8 @@ def create_environ(
body = io.BytesIO(body.encode() if isinstance(body, str) else body)

# NOTE(kgriffs): wsgiref, gunicorn, and uWSGI all unescape
# the paths before setting PATH_INFO
# the paths before setting PATH_INFO but preserve raw original
raw_path = path
path = uri.decode(path, unquote_plus=False)

# NOTE(kgriffs): The decoded path may contain UTF-8 characters.
Expand Down Expand Up @@ -1194,7 +1195,7 @@ def create_environ(
'PATH_INFO': path,
'QUERY_STRING': query_string,
'REMOTE_PORT': '65133',
'RAW_URI': '/',
'RAW_URI': raw_path,
'SERVER_NAME': host,
'SERVER_PORT': port,
'wsgi.version': (1, 0),
Expand Down
7 changes: 7 additions & 0 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ def test_create_environ_default_ua_override():
assert req.user_agent == ua


def test_create_environ_preserve_raw_uri():
uri = '/cache/http%3A%2F%2Ffalconframework.org/status'
environ = testing.create_environ(path=uri)
assert environ['PATH_INFO'] == '/cache/http://falconframework.org/status'
assert environ['RAW_URI'] == uri


def test_missing_header_is_none():
req = testing.create_req()
assert req.auth is None
Expand Down