Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

135 lines · 3.7 KiB · Python

Last changed by

repr change: <SyncResponse ...> -> <Response ...>Tom Christiebefore adoption73d5fca

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–27why, one step back
25
26@threadpool27def test_get(server):

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

line 28why, one step back
28 url = "http://127.0.0.1:8000/"

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

line 29why, one step back
29 with httpcore.Client() as http:

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

line 30why, one step back
30 response = http.get(url)

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

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

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

line 32why, one step back
32 assert response.url == httpcore.URL(url)

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

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

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

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

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 35–39why, one step back
35 assert response.protocol == "HTTP/1.1"
36 assert response.encoding == "iso-8859-1"37 assert response.request.url == httpcore.URL(url)38 assert response.headers39 assert response.is_redirect is False

repr change: <SyncResponse ...> -> <Response ...>Tom Christiebefore adoption73d5fca

line 40why, one step back
40 assert repr(response) == "<Response(200, '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_post(server):45 with httpcore.Client() as http:46 response = http.post("http://127.0.0.1:8000/", data=b"Hello, world!")

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

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

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

lines 49–50why, one step back
49
50

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

lines 51–54why, one step back
51@threadpool
52def test_stream_response(server):53 with httpcore.Client() as http:54 response = http.get("http://127.0.0.1:8000/", stream=True)

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

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

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

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

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

lines 58–59why, one step back
58
59

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

lines 60–68why, one step back
60@threadpool
61def test_stream_iterator(server):62 with httpcore.Client() as http:63 response = http.get("http://127.0.0.1:8000/", stream=True)64 assert response.status_code == 20065 body = b""66 for chunk in response.stream():67 body += chunk68 assert body == b"Hello, world!"

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

line 69why, one step back
69

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

lines 70–79why, one step back
70
71@threadpool72def test_raw_iterator(server):73 with httpcore.Client() as http:74 response = http.get("http://127.0.0.1:8000/", stream=True)75 assert response.status_code == 20076 body = b""77 for chunk in response.raw():78 body += chunk79 assert body == b"Hello, world!"

Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c

lines 80–135why, one step back
80 response.close() # TODO: should Response be available as context managers?
818283@threadpool84def test_raise_for_status(server):85 with httpcore.Client() as client:86 for status_code in (200, 400, 404, 500, 505):87 response = client.request(88 "GET", "http://127.0.0.1:8000/status/{}".format(status_code)89 )9091 if 400 <= status_code < 600:92 with pytest.raises(httpcore.exceptions.HttpError):93 response.raise_for_status()94 else:95 assert response.raise_for_status() is None969798@threadpool99def test_options(server):100 with httpcore.Client() as http:101 response = http.options("http://127.0.0.1:8000/")102 assert response.status_code == 200103 assert response.reason_phrase == "OK"104105106@threadpool107def test_head(server):108 with httpcore.Client() as http:109 response = http.head("http://127.0.0.1:8000/")110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112113114@threadpool115def test_put(server):116 with httpcore.Client() as http:117 response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")118 assert response.status_code == 200119 assert response.reason_phrase == "OK"120121122@threadpool123def test_patch(server):124 with httpcore.Client() as http:125 response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")126 assert response.status_code == 200127 assert response.reason_phrase == "OK"128129130@threadpool131def test_delete(server):132 with httpcore.Client() as http:133 response = http.delete("http://127.0.0.1:8000/")134 assert response.status_code == 200135 assert response.reason_phrase == "OK"