fix: 异步学习请求不再把同步钩子当成协程
This commit is contained in:
@@ -153,6 +153,39 @@ def test_qr_login_stores_scan_mark(monkeypatch) -> None:
|
||||
assert "secret" not in decrypt_secret(account.password_cipher)
|
||||
|
||||
|
||||
def test_async_study_hook_can_be_awaited() -> None:
|
||||
import asyncio
|
||||
import inspect
|
||||
|
||||
import httpx
|
||||
|
||||
from euniv.autostudy import StudySession
|
||||
|
||||
seen: dict[str, str | None] = {}
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
seen["host"] = request.url.host
|
||||
seen["token"] = request.headers.get("token")
|
||||
return httpx.Response(200, text="ok")
|
||||
|
||||
async def main() -> None:
|
||||
async with StudySession("secret-token", []) as session:
|
||||
hook = session._client._event_hooks["request"][0]
|
||||
assert inspect.iscoroutinefunction(hook)
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(handler),
|
||||
event_hooks={"request": [hook]},
|
||||
) as client:
|
||||
other = await client.get("https://example.com/x", headers={"token": "secret-token"})
|
||||
assert other.status_code == 200
|
||||
assert seen["token"] is None
|
||||
kept = await client.get("https://euniv.zte.com.cn/kng/view/video/x.html", headers={"token": "secret-token"})
|
||||
assert kept.status_code == 200
|
||||
assert seen["token"] == "secret-token"
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
|
||||
def test_secure_cookie_is_written_through_the_jar() -> None:
|
||||
client = EunivClient("6650000146", "secret-pass")
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user