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

Added asgi version and spec_version to scope #597

Merged
merged 10 commits into from
May 4, 2020
26 changes: 26 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,29 @@ def test_ssl_config(certfile_and_keyfile):
config.load()

assert config.is_ssl is True


async def asgi3app(scope, receive, send):
pass
euri10 marked this conversation as resolved.
Show resolved Hide resolved


def asgi2app(scope):
async def asgi(receive, send):
raise RuntimeError("Something went wrong")

return asgi
euri10 marked this conversation as resolved.
Show resolved Hide resolved


asgi_scope_data = [
(asgi3app, "asgi3", {"asgi": {"version": "3.0", "spec_version": "2.1"}}),
(asgi2app, "asgi2", {"asgi": {"version": "2.0", "spec_version": "2.1"}}),
]


@pytest.mark.parametrize(
"asgi2or3_app, expected_interface, expected_scope", asgi_scope_data
)
def test_interface_config(asgi2or3_app, expected_interface, expected_scope):
config = Config(app=asgi2or3_app)
config.load()
assert config.interface == expected_interface
euri10 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import threading
import time
import asyncio

import requests

Expand Down
4 changes: 4 additions & 0 deletions uvicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ def __init__(
else:
self.forwarded_allow_ips = forwarded_allow_ips

@property
def asgi_version(self) -> str:
return {"asgi2": "2.0", "asgi3": "3.0"}[self.interface]

@property
def is_ssl(self) -> bool:
return bool(self.ssl_keyfile or self.ssl_certfile)
Expand Down
4 changes: 4 additions & 0 deletions uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def handle_events(self):
raw_path, _, query_string = event.target.partition(b"?")
self.scope = {
"type": "http",
"asgi": {
"version": self.config.asgi_version,
"spec_version": "2.1",
},
"http_version": event.http_version.decode("ascii"),
"server": self.server,
"client": self.client,
Expand Down
1 change: 1 addition & 0 deletions uvicorn/protocols/http/httptools_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def on_url(self, url):
self.headers = []
self.scope = {
"type": "http",
"asgi": {"version": self.config.asgi_version, "spec_version": "2.1"},
"http_version": "1.1",
"server": self.server,
"client": self.client,
Expand Down
1 change: 1 addition & 0 deletions uvicorn/protocols/websockets/websockets_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ async def process_request(self, path, headers):

self.scope = {
"type": "websocket",
"asgi": {"version": self.config.asgi_version, "spec_version": "2.1"},
"scheme": self.scheme,
"server": self.server,
"client": self.client,
Expand Down
1 change: 1 addition & 0 deletions uvicorn/protocols/websockets/wsproto_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def handle_connect(self, event):
raw_path, _, query_string = event.target.partition("?")
self.scope = {
"type": "websocket",
"asgi": {"version": self.config.asgi_version, "spec_version": "2.1"},
"http_version": "1.1",
"scheme": self.scheme,
"server": self.server,
Expand Down