From 9b4b1526b1a4ddd98048ab260b29bdcd1f73f7a0 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Mon, 8 Apr 2024 20:17:03 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20Bug:=20=E6=B7=BB=E5=8A=A0=20HTTP=20?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E4=BC=9A=E8=AF=9D=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E6=96=87=E6=A3=80=E6=9F=A5=20(#2632)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/drivers/aiohttp.py | 2 ++ nonebot/drivers/httpx.py | 2 ++ tests/test_driver.py | 5 +++++ 3 files changed, 9 insertions(+) diff --git a/nonebot/drivers/aiohttp.py b/nonebot/drivers/aiohttp.py index b12d204b..6b437e22 100644 --- a/nonebot/drivers/aiohttp.py +++ b/nonebot/drivers/aiohttp.py @@ -124,6 +124,8 @@ class Session(HTTPClientSession): @override async def setup(self) -> None: + if self._client is not None: + raise RuntimeError("Session has already been initialized") self._client = aiohttp.ClientSession( cookies=self._cookies, headers=self._headers, diff --git a/nonebot/drivers/httpx.py b/nonebot/drivers/httpx.py index 8300323b..68eed464 100644 --- a/nonebot/drivers/httpx.py +++ b/nonebot/drivers/httpx.py @@ -93,6 +93,8 @@ class Session(HTTPClientSession): @override async def setup(self) -> None: + if self._client is not None: + raise RuntimeError("Session has already been initialized") self._client = httpx.AsyncClient( params=self._params, headers=self._headers, diff --git a/tests/test_driver.py b/tests/test_driver.py index 546cfeec..636e02c3 100644 --- a/tests/test_driver.py +++ b/tests/test_driver.py @@ -328,6 +328,11 @@ async def test_http_client_session(driver: Driver, server_url: URL): with pytest.raises(RuntimeError): await session.request(request) + with pytest.raises(RuntimeError): # noqa: PT012 + async with session: + async with session: + ... + async with session as session: # simple post with query, headers, cookies and content request = Request(