Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

160 lines · 4.4 KiB · Python

Last changed by

Move HSTS preload checking to client (#184)Seth Michael Larsonbefore adoption9142a89

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 6why, one step back
6import httpx

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 32why, one step back
32 assert response.url == httpx.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"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 37why, one step back
37 assert response.request.url == httpx.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

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 45why, one step back
45 with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 53why, one step back
53 with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 61why, one step back
61 with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 70why, one step back
70 with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 81why, one step back
81 with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 93why, one step back
93 with httpx.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:

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 100why, one step back
100 with pytest.raises(httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 108why, one step back
108 with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 116why, one step back
116 with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 124why, one step back
124 with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 132why, one step back
132 with httpx.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):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 140why, one step back
140 with httpx.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"

Support Client(base_url=...)Tom Christiebefore adoption9d5aaff

lines 144–148why, one step back
144
145146@threadpool147def test_base_url(server):148 base_url = "http://127.0.0.1:8000/"

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 149why, one step back
149 with httpx.Client(base_url=base_url) as http:

Handle early connection closes (#103)Tom Christiebefore adoptionb9c8a15

line 150why, one step back
150 response = http.get("/")

Support Client(base_url=...)Tom Christiebefore adoption9d5aaff

lines 151–152why, one step back
151 assert response.status_code == 200
152 assert str(response.url) == base_url

Move HSTS preload checking to client (#184)Seth Michael Larsonbefore adoption9142a89

lines 153–160why, one step back
153
154155def test_merge_url():156 client = httpx.Client(base_url="https://www.paypal.com/")157 url = client.merge_url("http://www.paypal.com")158159 assert url.scheme == "https"160 assert url.is_ssl