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

Improved wsgi middleware, mainly a copy of a2wsgi #1049

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ files =
uvicorn/_handlers,
uvicorn/__init__.py,
uvicorn/__main__.py,
uvicorn/subprocess.py
uvicorn/subprocess.py,
uvicorn/middleware/wsgi.py


[mypy-tests.*]
Expand Down
47 changes: 45 additions & 2 deletions tests/middleware/test_wsgi.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import asyncio
import sys
import threading

import httpx
import pytest

from uvicorn.middleware.wsgi import WSGIMiddleware, build_environ
from uvicorn.middleware.wsgi import Body, WSGIMiddleware, build_environ


def hello_world(environ, start_response):
Expand Down Expand Up @@ -106,6 +108,47 @@ def test_build_environ_encoding():
"query_string": b"a=123&b=456",
"headers": [(b"key", b"value1"), (b"key", b"value2")],
}
environ = build_environ(scope, b"", b"")
environ = build_environ(scope, b"")
assert environ["PATH_INFO"] == "/文".encode("utf8").decode("latin-1")
assert environ["HTTP_KEY"] == "value1,value2"


def test_body():
event_loop = asyncio.new_event_loop()
threading.Thread(target=event_loop.run_forever, daemon=True).start()

async def receive():
return {
"type": "http.request.body",
"body": b"""This is a body test.
Why do this?
To prevent memory leaks.
And cancel pre-reading.
Newline.0
Newline.1
Newline.2
Newline.3
""",
}

body = Body(event_loop, receive)
assert body.readline() == b"This is a body test.\n"
assert body.read(4) == b"Why "
assert body.readline(2) == b"do"
assert body.readline(20) == b" this?\n"

assert body.readlines(2) == [
b"To prevent memory leaks.\n",
b"And cancel pre-reading.\n",
]
for index, line in enumerate(body):
assert line == b"Newline." + str(index).encode("utf8") + b"\n"
if index == 1:
break
assert body.readlines() == [
b"Newline.2\n",
b"Newline.3\n",
]
assert body.readlines() == []
assert body.readline() == b""
assert body.read() == b""