🐛 fix missing endpoint name for routing

This commit is contained in:
yanyongyu 2021-12-22 18:50:03 +08:00
parent fecdb5367a
commit b984f99643
5 changed files with 69 additions and 14 deletions

View File

@ -173,17 +173,19 @@ class Driver(ReverseDriver):
self._server_app.add_api_route(
setup.path.path,
_handle,
name=setup.name,
methods=[setup.method],
)
@overrides(ReverseDriver)
def setup_websocket_server(self, setup: WebSocketServerSetup) -> None:
async def _handle(websocket: WebSocket):
async def _handle(websocket: WebSocket) -> None:
await self._handle_ws(websocket, setup)
self._server_app.add_api_websocket_route(
setup.path.path,
_handle,
name=setup.name,
)
@overrides(ReverseDriver)

View File

@ -8,7 +8,6 @@ Quart 驱动适配
https://pgjones.gitlab.io/quart/index.html
"""
from functools import partial
from typing import List, TypeVar, Callable, Optional, Coroutine
import uvicorn
@ -140,17 +139,25 @@ class Driver(ReverseDriver):
@overrides(ReverseDriver)
def setup_http_server(self, setup: HTTPServerSetup):
async def _handle() -> Response:
return await self._handle_http(setup)
self._server_app.add_url_rule(
setup.path.path,
endpoint=setup.name,
methods=[setup.method],
view_func=partial(self._handle_http, setup=setup),
view_func=_handle,
)
@overrides(ReverseDriver)
def setup_websocket_server(self, setup: WebSocketServerSetup) -> None:
async def _handle() -> None:
return await self._handle_ws(setup)
self._server_app.add_websocket(
setup.path.path,
view_func=partial(self._handle_ws, setup=setup),
endpoint=setup.name,
view_func=_handle,
)
@overrides(ReverseDriver)

13
poetry.lock generated
View File

@ -787,21 +787,22 @@ python-versions = ">=3.6"
[[package]]
name = "quart"
version = "0.15.1"
version = "0.16.2"
description = "A Python ASGI web microframework with the same API as Flask"
category = "main"
optional = true
python-versions = ">=3.7.0"
python-versions = ">=3.7"
[package.dependencies]
aiofiles = "*"
blinker = "*"
click = "*"
hypercorn = ">=0.11.2"
importlib_metadata = {version = "*", markers = "python_version < \"3.8\""}
itsdangerous = "*"
jinja2 = "*"
toml = "*"
typing-extensions = {version = "*", markers = "python_version < \"3.8\""}
typing_extensions = {version = "*", markers = "python_version < \"3.8\""}
werkzeug = ">=2.0.0"
[package.extras]
@ -1193,7 +1194,7 @@ quart = ["Quart"]
[metadata]
lock-version = "1.1"
python-versions = "^3.7.3"
content-hash = "3cef667f39a760749d4c1d35883be4cec9c224e8079ee28752f6aa7f58688442"
content-hash = "e32c99ae82181567b69bc70580e3c0d816a522d0174fce2ce04c773219f1bb50"
[metadata.files]
aiodns = [
@ -1994,8 +1995,8 @@ pyyaml = [
{file = "PyYAML-6.0.tar.gz", hash = "sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2"},
]
quart = [
{file = "Quart-0.15.1-py3-none-any.whl", hash = "sha256:f35134fb1d81af61624e6d89bca33cd611dcedce2dc4e291f527ab04395f4e1a"},
{file = "Quart-0.15.1.tar.gz", hash = "sha256:f80c91d1e0588662483e22dd9c368a5778886b62e128c5399d2cc1b1898482cf"},
{file = "Quart-0.16.2-py3-none-any.whl", hash = "sha256:c1bcc4989c7e0b6c3301df10b6285c09ee0a1d75fa4cebd936f0d17312294e36"},
{file = "Quart-0.16.2.tar.gz", hash = "sha256:356f4fd795fbf5a7a97bdeb7ca908b5051d0e6e4c0499b2b7c30b743a6938a7e"},
]
requests = [
{file = "requests-2.26.0-py2.py3-none-any.whl", hash = "sha256:6c1246513ecd5ecd4528a0906f910e8f0f9c6b8ec72030dc9fd154dc1a6efd24"},

View File

@ -30,7 +30,7 @@ tomlkit = "^0.7.0"
fastapi = "^0.70.0"
websockets = ">=9.1"
typing-extensions = ">=3.10.0,<5.0.0"
Quart = { version = "^0.15.0", optional = true }
Quart = { version = "^0.16.0", optional = true }
httpx = { version = ">=0.20.0, <1.0.0", extras = ["http2"] }
pydantic = { version = "~1.8.0", extras = ["dotenv"] }
uvicorn = { version = "^0.15.0", extras = ["standard"] }

View File

@ -1,11 +1,56 @@
from typing import cast
import pytest
from nonebug import App
@pytest.mark.asyncio
@pytest.mark.parametrize(
"nonebug_init",
[{"driver": "nonebot.drivers.fastapi"}],
[
pytest.param({"driver": "nonebot.drivers.fastapi:Driver"}, id="fastapi"),
pytest.param({"driver": "nonebot.drivers.quart:Driver"}, id="quart"),
],
indirect=True,
)
async def test_driver(nonebug_init):
...
async def test_reverse_driver(app: App):
import nonebot
from nonebot.drivers import (
URL,
Request,
Response,
WebSocket,
ReverseDriver,
HTTPServerSetup,
WebSocketServerSetup,
)
driver = cast(ReverseDriver, nonebot.get_driver())
async def _handle_http(request: Request) -> Response:
assert request.content in (b"test", "test")
return Response(200, content="test")
async def _handle_ws(ws: WebSocket) -> None:
await ws.accept()
data = await ws.receive()
assert data == "ping"
await ws.send("pong")
await ws.close()
http_setup = HTTPServerSetup(URL("/http_test"), "POST", "http_test", _handle_http)
driver.setup_http_server(http_setup)
ws_setup = WebSocketServerSetup(URL("/ws_test"), "ws_test", _handle_ws)
driver.setup_websocket_server(ws_setup)
async with app.test_server() as ctx:
client = ctx.get_client()
response = await client.post("/http_test", data="test")
assert response.status_code == 200
assert response.text == "test"
# FIXME: https://github.com/vinissimus/async-asgi-testclient/issues/43
# async with client.websocket_connect("/ws_test") as ws:
# await ws.send_text("ping")
# assert await ws.receive_text() == "pong"