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

feat(testing): add raw_path to ASGI scope #2331

Open
wants to merge 9 commits into
base: master
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
1 change: 1 addition & 0 deletions docs/_newsfragments/2262.newandimproved.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:py:func:`falcon.testing.helpers.create_scope` preserves the raw_path. This is to keep consistency with `#2159 <https://github.com/falconry/falcon/pull/2159>`_
3 changes: 2 additions & 1 deletion falcon/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ def create_scope(
"""

http_version = _fixup_http_version(http_version)

raw_path = path.split('?')[0]
path = uri.decode(path, unquote_plus=False)

# NOTE(kgriffs): Handles both None and ''
Expand All @@ -995,6 +995,7 @@ def create_scope(
'http_version': http_version,
'method': method.upper(),
'path': path,
'raw_path': raw_path.encode(),
'query_string': query_string_bytes,
}

Expand Down
18 changes: 18 additions & 0 deletions tests/asgi/test_testing_asgi.py
aarcex3 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,21 @@ def test_immediate_disconnect():

with pytest.raises(ConnectionError):
client.simulate_get('/', asgi_disconnect_ttl=0)


@pytest.mark.parametrize(
'path, expected',
[
('/cache/http%3A%2F%2Ffalconframework.org/status', True),
(
'/cache/http%3A%2F%2Ffalconframework.org/status?param1=value1&param2=value2',
False,
),
],
)
def test_create_scope_preserve_raw_path(path, expected):
scope = testing.create_scope(path=path)
if expected:
assert scope['raw_path'] == path.encode()
else:
assert scope['raw_path'] != path.encode()
4 changes: 4 additions & 0 deletions tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,16 @@ def test_raw_path(self, asgi, app_kind, util):
result1 = falcon.testing.simulate_get(
recipe.app, url1, extras=self.path_extras(asgi, url1)
)
scope1 = falcon.testing.create_scope(url1)
assert result1.status_code == 200
assert result1.json == {'url': 'http://falconframework.org'}
assert scope1['raw_path'] == url1.encode()

url2 = '/cache/http%3A%2F%2Ffalconframework.org/status'
result2 = falcon.testing.simulate_get(
recipe.app, url2, extras=self.path_extras(asgi, url2)
)
scope2 = falcon.testing.create_scope(url2)
assert result2.status_code == 200
assert result2.json == {'cached': True}
assert scope2['raw_path'] == url2.encode()
Loading