Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

71 lines · 1.8 KiB · Python

Last changed by

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

History of this file

Take it

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 1–3why, one step back
1import asyncio
2import functools3

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 4–8why, one step back
4import pytest
56import httpcore78

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 9–12why, one step back
9def threadpool(func):
10 """11 Our sync tests should run in seperate thread to the uvicorn server.12 """

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 13why, one step back
13

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 14–16why, one step back
14 @functools.wraps(func)
15 async def wrapped(*args, **kwargs):16 nonlocal func

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 17why, one step back
17

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 18–21why, one step back
18 loop = asyncio.get_event_loop()
19 if kwargs:20 func = functools.partial(func, **kwargs)21 await loop.run_in_executor(None, func, *args)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 22why, one step back
22

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 23why, one step back
23 return pytest.mark.asyncio(wrapped)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 24why, one step back
24

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 25–29why, one step back
25
26@threadpool27def test_get(server):28 with httpcore.Client() as http:29 response = http.get("http://127.0.0.1:8000/")

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 30–31why, one step back
30 assert response.status_code == 200
31 assert response.content == b"Hello, world!"

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

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

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 33–34why, one step back
33
34

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 35–38why, one step back
35@threadpool
36def test_post(server):37 with httpcore.Client() as http:38 response = http.post("http://127.0.0.1:8000/", data=b"Hello, world!")

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 39why, one step back
39 assert response.status_code == 200

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

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

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 41–42why, one step back
41
42

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 43–46why, one step back
43@threadpool
44def test_stream_response(server):45 with httpcore.Client() as http:46 response = http.get("http://127.0.0.1:8000/", stream=True)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 47why, one step back
47 assert response.status_code == 200

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

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

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 50–51why, one step back
50
51

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 52–60why, one step back
52@threadpool
53def test_stream_iterator(server):54 with httpcore.Client() as http:55 response = http.get("http://127.0.0.1:8000/", stream=True)56 assert response.status_code == 20057 body = b""58 for chunk in response.stream():59 body += chunk60 assert body == b"Hello, world!"

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 61why, one step back
61

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 62–71why, one step back
62
63@threadpool64def test_raw_iterator(server):65 with httpcore.Client() as http:66 response = http.get("http://127.0.0.1:8000/", stream=True)67 assert response.status_code == 20068 body = b""69 for chunk in response.raw():70 body += chunk71 assert body == b"Hello, world!"