Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

143 lines · 4.0 KiB · Python

Last changed by

Add json interfacesTom Christiebefore adoption5a63d31

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

line 51why, one step back
51@threadpool

Add json interfacesTom Christiebefore adoption5a63d31

lines 52–59why, one step back
52def test_post_json(server):
53 with httpcore.Client() as http:54 response = http.post("http://127.0.0.1:8000/", json={"text": "Hello, world!"})55 assert response.status_code == 20056 assert response.reason_phrase == "OK"575859@threadpool

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

lines 60–62why, one step back
60def test_stream_response(server):
61 with httpcore.Client() as http:62 response = http.get("http://127.0.0.1:8000/", stream=True)

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

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

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

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

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

lines 66–67why, one step back
66
67

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

lines 68–76why, one step back
68@threadpool
69def test_stream_iterator(server):70 with httpcore.Client() as http:71 response = http.get("http://127.0.0.1:8000/", stream=True)72 assert response.status_code == 20073 body = b""74 for chunk in response.stream():75 body += chunk76 assert body == b"Hello, world!"

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

line 77why, one step back
77

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

lines 78–87why, one step back
78
79@threadpool80def test_raw_iterator(server):81 with httpcore.Client() as http:82 response = http.get("http://127.0.0.1:8000/", stream=True)83 assert response.status_code == 20084 body = b""85 for chunk in response.raw():86 body += chunk87 assert body == b"Hello, world!"

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

lines 88–143why, one step back
88 response.close() # TODO: should Response be available as context managers?
899091@threadpool92def test_raise_for_status(server):93 with httpcore.Client() as client:94 for status_code in (200, 400, 404, 500, 505):95 response = client.request(96 "GET", "http://127.0.0.1:8000/status/{}".format(status_code)97 )9899 if 400 <= status_code < 600:100 with pytest.raises(httpcore.exceptions.HttpError):101 response.raise_for_status()102 else:103 assert response.raise_for_status() is None104105106@threadpool107def test_options(server):108 with httpcore.Client() as http:109 response = http.options("http://127.0.0.1:8000/")110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112113114@threadpool115def test_head(server):116 with httpcore.Client() as http:117 response = http.head("http://127.0.0.1:8000/")118 assert response.status_code == 200119 assert response.reason_phrase == "OK"120121122@threadpool123def test_put(server):124 with httpcore.Client() as http:125 response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")126 assert response.status_code == 200127 assert response.reason_phrase == "OK"128129130@threadpool131def test_patch(server):132 with httpcore.Client() as http:133 response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")134 assert response.status_code == 200135 assert response.reason_phrase == "OK"136137138@threadpool139def test_delete(server):140 with httpcore.Client() as http:141 response = http.delete("http://127.0.0.1:8000/")142 assert response.status_code == 200143 assert response.reason_phrase == "OK"