Skip to content
Myr

test_sync.py

This file

Path

tests/test_sync.py

60 lines · 1.6 KiB · Python

Last changed by

Add idna support. More tests.Tom Christiebefore adoptionb948798

History of this file

Take it

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 1–30why, one step back
1import asyncio
2import 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):28 with httpcore.SyncConnectionPool() as http:29 response = http.request("GET", "http://127.0.0.1:8000/")30 assert response.status_code == 200

Response.contentTom Christiebefore adoption4c55113

line 31why, one step back
31 assert response.content == b"Hello, world!"

Add idna support. More tests.Tom Christiebefore adoptionb948798

line 32why, one step back
32 assert response.text == "Hello, world!"

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 33–39why, one step back
33
3435@threadpool36def test_post(server):37 with httpcore.SyncConnectionPool() as http:38 response = http.request("POST", "http://127.0.0.1:8000/", body=b"Hello, world!")39 assert response.status_code == 200

Add idna support. More tests.Tom Christiebefore adoptionb948798

line 40why, one step back
40 assert response.reason_phrase == "OK"

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 41–47why, one step back
41
4243@threadpool44def test_stream_response(server):45 with httpcore.SyncConnectionPool() as http:46 response = http.request("GET", "http://127.0.0.1:8000/", stream=True)47 assert response.status_code == 200

Response.contentTom Christiebefore adoption4c55113

lines 48–49why, one step back
48 content = response.read()
49 assert content == b"Hello, world!"

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

lines 50–56why, one step back
50
5152@threadpool53def test_stream_iterator(server):54 with httpcore.SyncConnectionPool() as http:55 response = http.request("GET", "http://127.0.0.1:8000/", stream=True)56 assert response.status_code == 200

Response.contentTom Christiebefore adoption4c55113

line 57why, one step back
57 content = b""

Merge pull request #13 from encode/syncTom Christiebefore adoption903a961

line 58why, one step back
58 for chunk in response.stream():

Response.contentTom Christiebefore adoption4c55113

lines 59–60why, one step back
59 content += chunk
60 assert content == b"Hello, world!"