Skip to content

Commit

Permalink
🐛 Bug: 添加 HTTP 客户端会话上下文检查 (#2632)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu committed Apr 8, 2024
1 parent 7a232c7 commit 9b4b152
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nonebot/drivers/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ async def request(self, setup: Request) -> Response:

@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,
Expand Down
2 changes: 2 additions & 0 deletions nonebot/drivers/httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ async def request(self, setup: Request) -> Response:

@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,
Expand Down
5 changes: 5 additions & 0 deletions tests/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 9b4b152

Please sign in to comment.