Skip to content

Commit

Permalink
fix(testing): preserve raw path in TestClient methods (#2159)
Browse files Browse the repository at this point in the history
* fix TestClient to preserve raw URI

* add test and changelog
  • Loading branch information
liborjelinek committed Jul 18, 2023
1 parent 7191be4 commit 69cdcd6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/_newsfragments/2157.bugfix.rst
@@ -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
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
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

0 comments on commit 69cdcd6

Please sign in to comment.