Skip to content

Commit

Permalink
Added asgi version and spec_version to scope (#597)
Browse files Browse the repository at this point in the history
* Added asgi version and spec_version to scope
Minor import sort corrected in a test

* Added correct asgi version depending on interface used

* Lint

* Forgot websockets

* Added test for auto discovery of asgi interface

* Seperated config scope for asgi and protocol scope test

* Testing asgi_version

* Dont need anything in tested fake apps

* Flake8 !
  • Loading branch information
euri10 authored May 4, 2020
1 parent 779fcf7 commit ae0fd31
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/protocols/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,29 @@ def test_supported_upgrade_request(protocol_cls):
protocol.data_received(UPGRADE_REQUEST)

assert b"HTTP/1.1 426 " in protocol.transport.buffer


async def asgi3app(scope, receive, send):
pass


def asgi2app(scope):
async def asgi(receive, send):
pass

return asgi


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


@pytest.mark.parametrize("asgi2or3_app, expected_scopes", asgi_scope_data)
@pytest.mark.parametrize("protocol_cls", HTTP_PROTOCOLS)
def test_scopes(asgi2or3_app, expected_scopes, protocol_cls):
protocol = get_connected_protocol(asgi2or3_app, protocol_cls,)
protocol.data_received(SIMPLE_GET_REQUEST)
protocol.loop.run_one()
assert expected_scopes == protocol.scope.get("asgi")
16 changes: 16 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ def test_ssl_config(certfile_and_keyfile):
config.load()

assert config.is_ssl is True


def asgi2_app(scope):
async def asgi(receive, send):
pass

return asgi


@pytest.mark.parametrize(
"app, expected_interface", [(asgi_app, "3.0",), (asgi2_app, "2.0",)]
)
def test_asgi_version(app, expected_interface):
config = Config(app=app)
config.load()
assert config.asgi_version == expected_interface
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

0 comments on commit ae0fd31

Please sign in to comment.