test_sync.py
This file
Path
tests/test_sync.py
71 lines · 1.8 KiB · Python
Last changed by
Truckin' onTom Christiebefore adoption6b60d95
History of this fileTake it
Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 1–27why, one step back1import asyncio2import functools34import pytest56import httpcore789def threadpool(func):10 """11 Our sync tests should run in seperate thread to the uvicorn server.12 """1314 @functools.wraps(func)15 async def wrapped(*args, **kwargs):16 nonlocal func1718 loop = asyncio.get_event_loop()19 if kwargs:20 func = functools.partial(func, **kwargs)21 await loop.run_in_executor(None, func, *args)2223 return pytest.mark.asyncio(wrapped)242526@threadpool27def test_get(server):Truckin' onTom Christiebefore adoption6b60d95
lines 28–29why, one step back28 with httpcore.SyncClient() as http:29 response = http.get("http://127.0.0.1:8000/")Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 30why, one step back30 assert response.status_code == 200Response.contentTom Christiebefore adoption4c55113
line 31why, one step back31 assert response.content == b"Hello, world!"Add idna support. More tests.Tom Christiebefore adoptionb948798
line 32why, one step back32 assert response.text == "Hello, world!"Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 33–36why, one step back333435@threadpool36def test_post(server):Truckin' onTom Christiebefore adoption6b60d95
lines 37–38why, one step back37 with httpcore.SyncClient() as http:38 response = http.post("http://127.0.0.1:8000/", content=b"Hello, world!")Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 39why, one step back39 assert response.status_code == 200Add idna support. More tests.Tom Christiebefore adoptionb948798
line 40why, one step back40 assert response.reason_phrase == "OK"Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 41–44why, one step back414243@threadpool44def test_stream_response(server):Truckin' onTom Christiebefore adoption6b60d95
lines 45–46why, one step back45 with httpcore.SyncClient() as http:46 response = http.get("http://127.0.0.1:8000/", stream=True)Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 47why, one step back47 assert response.status_code == 200Response.contentTom Christiebefore adoption4c55113
lines 48–49why, one step back48 content = response.read()49 assert content == b"Hello, world!"Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
lines 50–53why, one step back505152@threadpool53def test_stream_iterator(server):Truckin' onTom Christiebefore adoption6b60d95
lines 54–55why, one step back54 with httpcore.SyncClient() as http:55 response = http.get("http://127.0.0.1:8000/", stream=True)Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 56why, one step back56 assert response.status_code == 200Merge pull request #21 from yeraydiazdiaz/wait-for-connection-closeTom Christiebefore adoption118d94b
line 57why, one step back57 body = b""Merge pull request #13 from encode/syncTom Christiebefore adoption903a961
line 58why, one step back58 for chunk in response.stream():Merge pull request #21 from yeraydiazdiaz/wait-for-connection-closeTom Christiebefore adoption118d94b
lines 59–64why, one step back59 body += chunk60 assert body == b"Hello, world!"616263@threadpool64def test_raw_iterator(server):Truckin' onTom Christiebefore adoption6b60d95
lines 65–66why, one step back65 with httpcore.SyncClient() as http:66 response = http.get("http://127.0.0.1:8000/", stream=True)Merge pull request #21 from yeraydiazdiaz/wait-for-connection-closeTom Christiebefore adoption118d94b
lines 67–71why, one step back67 assert response.status_code == 20068 body = b""69 for chunk in response.raw():70 body += chunk71 assert body == b"Hello, world!"