Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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"
start_response("200 OK", [["X-Colour", "Blue"]])
yield b"first chunk "
yield b"second chunk"
# Wrap it
application = WsgiToAsgi(wsgi_application)
# Launch it as a test application
instance = ApplicationCommunicator(
application,
{
"type": "http",
"http_version": "1.0",
"method": "GET",
"path": "/foo/",
"query_string": b"bar=baz",
"headers": [[b"test-header", b"test value"]],
},
)
await instance.send_input({"type": "http.request"})
# Check they send stuff
assert (await instance.receive_output(1)) == {
"type": "http.response.start",
async def test_receive_nothing():
"""
Tests ApplicationCommunicator.receive_nothing to return the correct value.
"""
# Get an ApplicationCommunicator instance
def wsgi_application(environ, start_response):
start_response("200 OK", [])
yield b"content"
application = WsgiToAsgi(wsgi_application)
instance = ApplicationCommunicator(
application,
{
"type": "http",
"http_version": "1.0",
"method": "GET",
"path": "/foo/",
"query_string": b"bar=baz",
"headers": [],
},
)
# No event
assert await instance.receive_nothing() is True
# Produce 3 events to receive
"""
if "chat" in websocket.scope["subprotocols"]:
subprotocol = "chat"
else:
subprotocol = None
await websocket.accept(subprotocol=subprotocol)
try:
while True:
message = await websocket.receive_text()
await websocket.send_text(message)
except WebSocketDisconnect:
pass
app.mount("/httpbin", WsgiToAsgi(httpbin.app))
app.mount("/", StaticFiles(directory=os.path.join(ROOT, "htdocs"), html=True))