Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

143 lines · 3.9 KiB · Python

Last changed by

Int status codes (#92)Tom Christiebefore adoption56a7943

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–5why, one step back
4import pytest
5

http3 (#86)Tom Christiebefore adoptionc9747aa

line 6why, one step back
6import http3

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

lines 7–8why, one step back
7
8

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/"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 29why, one step back
29 with http3.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

http3 (#86)Tom Christiebefore adoptionc9747aa

line 32why, one step back
32 assert response.url == http3.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–36why, one step back
35 assert response.protocol == "HTTP/1.1"
36 assert response.encoding == "iso-8859-1"

http3 (#86)Tom Christiebefore adoptionc9747aa

line 37why, one step back
37 assert response.request.url == http3.URL(url)

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

lines 38–39why, one step back
38 assert response.headers
39 assert response.is_redirect is False

Int status codes (#92)Tom Christiebefore adoption56a7943

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–44why, one step back
43@threadpool
44def test_post(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 45why, one step back
45 with http3.Client() as http:

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

line 46why, one step back
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

line 52why, one step back
52def test_post_json(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 53why, one step back
53 with http3.Client() as http:

Add json interfacesTom Christiebefore adoption5a63d31

lines 54–59why, one step back
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

line 60why, one step back
60def test_stream_response(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 61why, one step back
61 with http3.Client() as http:

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

line 62why, one step back
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–69why, one step back
68@threadpool
69def test_stream_iterator(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 70why, one step back
70 with http3.Client() as http:

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

lines 71–76why, one step back
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–80why, one step back
78
79@threadpool80def test_raw_iterator(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 81why, one step back
81 with http3.Client() as http:

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

lines 82–87why, one step back
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–92why, one step back
88 response.close() # TODO: should Response be available as context managers?
899091@threadpool92def test_raise_for_status(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 93why, one step back
93 with http3.Client() as client:

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

lines 94–99why, one step back
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:

http3 (#86)Tom Christiebefore adoptionc9747aa

line 100why, one step back
100 with pytest.raises(http3.exceptions.HttpError):

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

lines 101–107why, one step back
101 response.raise_for_status()
102 else:103 assert response.raise_for_status() is None104105106@threadpool107def test_options(server):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 108why, one step back
108 with http3.Client() as http:

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

lines 109–115why, one step back
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):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 116why, one step back
116 with http3.Client() as http:

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

lines 117–123why, one step back
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):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 124why, one step back
124 with http3.Client() as http:

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

lines 125–131why, one step back
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):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 132why, one step back
132 with http3.Client() as http:

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

lines 133–139why, one step back
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):

http3 (#86)Tom Christiebefore adoptionc9747aa

line 140why, one step back
140 with http3.Client() as http:

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

lines 141–143why, one step back
141 response = http.delete("http://127.0.0.1:8000/")
142 assert response.status_code == 200143 assert response.reason_phrase == "OK"