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

Correct WSGI adapter handling of root path. #446

Merged
merged 1 commit into from
Mar 15, 2024
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
8 changes: 6 additions & 2 deletions asgiref/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ def build_environ(self, scope, body):
"""
Builds a scope and request body into a WSGI environ object.
"""
script_name = scope.get("root_path", "").encode("utf8").decode("latin1")
path_info = scope["path"].encode("utf8").decode("latin1")
if path_info.startswith(script_name):
path_info = path_info[len(script_name) :]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setup.cfg change was required by this.

% flake8 asgiref/wsgi.py 
asgiref/wsgi.py:60:51: E203 whitespace before ':'

environ = {
"REQUEST_METHOD": scope["method"],
"SCRIPT_NAME": scope.get("root_path", "").encode("utf8").decode("latin1"),
"PATH_INFO": scope["path"].encode("utf8").decode("latin1"),
"SCRIPT_NAME": script_name,
"PATH_INFO": path_info,
"QUERY_STRING": scope["query_string"].decode("ascii"),
"SERVER_PROTOCOL": "HTTP/%s" % scope["http_version"],
"wsgi.version": (1, 0),
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ asyncio_mode = strict

[flake8]
exclude = venv/*,tox/*,specs/*
ignore = E123,E128,E266,E402,W503,E731,W601
ignore = E123,E128,E266,E402,W503,E731,W601,E203
max-line-length = 119

[isort]
Expand Down
24 changes: 23 additions & 1 deletion tests/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import pytest

from asgiref.testing import ApplicationCommunicator
from asgiref.wsgi import WsgiToAsgi
from asgiref.wsgi import WsgiToAsgi, WsgiToAsgiInstance


@pytest.mark.asyncio
async def test_basic_wsgi():
"""
Makes sure the WSGI wrapper has basic functionality.
"""

# Define WSGI app
def wsgi_application(environ, start_response):
assert environ["HTTP_TEST_HEADER"] == "test value 1,test value 2"
Expand Down Expand Up @@ -55,11 +56,32 @@ def wsgi_application(environ, start_response):
assert (await instance.receive_output(1)) == {"type": "http.response.body"}


def test_script_name():
scope = {
"type": "http",
"http_version": "1.0",
"method": "GET",
"root_path": "/base",
"path": "/base/foo/",
"query_string": b"bar=baz",
"headers": [
[b"test-header", b"test value 1"],
[b"test-header", b"test value 2"],
],
}
adapter = WsgiToAsgiInstance(None)
adapter.scope = scope
environ = adapter.build_environ(scope, None)
assert environ["SCRIPT_NAME"] == "/base"
assert environ["PATH_INFO"] == "/foo/"


@pytest.mark.asyncio
async def test_wsgi_path_encoding():
"""
Makes sure the WSGI wrapper has basic functionality.
"""

# Define WSGI app
def wsgi_application(environ, start_response):
assert environ["SCRIPT_NAME"] == "/中国".encode().decode("latin-1")
Expand Down
Loading