Skip to content

Commit

Permalink
Correct WSGI adapter handling of root path.
Browse files Browse the repository at this point in the history
Fixes #442
  • Loading branch information
carltongibson committed Mar 15, 2024
1 parent 8cf847a commit 4209b6c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
8 changes: 6 additions & 2 deletions asgiref/wsgi.py
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) :]
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
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
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

0 comments on commit 4209b6c

Please sign in to comment.