Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

176 lines · 5.0 KiB · Python

Last changed by

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

History of this file

Take it

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

lines 1–3why, one step back
1from datetime import timedelta
2from time import sleep3

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

line 9why, one step back
9def test_get(server):

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 10why, one step back
10 url = server.url

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 11why, one step back
11 with httpx.Client() as http:

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

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

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 14why, one step back
14 assert response.url == url

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

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

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

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

Response.protocol -> Response.http_version (#250)Tom Christiebefore adoptionc1ec1c3

line 17why, one step back
17 assert response.http_version == "HTTP/1.1"

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

line 18why, one step back
18 assert response.encoding == "iso-8859-1"

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 19why, one step back
19 assert response.request.url == url

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

lines 20–21why, one step back
20 assert response.headers
21 assert response.is_redirect is False

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

line 22why, one step back
22 assert repr(response) == "<Response [200 OK]>"

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

line 23why, one step back
23 assert response.elapsed > timedelta(0)

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

lines 24–25why, one step back
24
25

Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac

lines 26–40why, one step back
26def test_build_request(server):
27 url = server.url.copy_with(path="/echo_headers")28 headers = {"Custom-header": "value"}2930 with httpx.Client() as http:31 request = http.build_request("GET", url)32 request.headers.update(headers)33 response = http.send(request)3435 assert response.status_code == 20036 assert response.url == url3738 assert response.json()["Custom-header"] == "value"3940

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

line 41why, one step back
41def test_post(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 42why, one step back
42 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 43why, one step back
43 response = http.post(server.url, data=b"Hello, world!")

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

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

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

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

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

lines 46–47why, one step back
46
47

Add json interfacesTom Christiebefore adoption5a63d31

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 49why, one step back
49 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 50why, one step back
50 response = http.post(server.url, json={"text": "Hello, world!"})

Add json interfacesTom Christiebefore adoption5a63d31

lines 51–54why, one step back
51 assert response.status_code == 200
52 assert response.reason_phrase == "OK"5354

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

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

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 56why, one step back
56 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 57why, one step back
57 response = http.get(server.url, stream=True)

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

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

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

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

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

lines 61–62why, one step back
61
62

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

line 63why, one step back
63def test_stream_iterator(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 64why, one step back
64 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 65why, one step back
65 response = http.get(server.url, stream=True)

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

lines 66–70why, one step back
66 assert response.status_code == 200
67 body = b""68 for chunk in response.stream():69 body += chunk70 assert body == b"Hello, world!"

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

line 71why, one step back
71

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

lines 72–73why, one step back
72
73def test_raw_iterator(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 74why, one step back
74 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 75why, one step back
75 response = http.get(server.url, stream=True)

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

lines 76–80why, one step back
76 assert response.status_code == 200
77 body = b""78 for chunk in response.raw():79 body += chunk80 assert body == b"Hello, world!"

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

lines 81–84why, one step back
81 response.close() # TODO: should Response be available as context managers?
828384def test_raise_for_status(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 85why, one step back
85 with httpx.Client() as client:

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

lines 86–87why, one step back
86 for status_code in (200, 400, 404, 500, 505):
87 response = client.request(

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 88why, one step back
88 "GET", server.url.copy_with(path="/status/{}".format(status_code))

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

lines 89–90why, one step back
89 )
90 if 400 <= status_code < 600:

Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f

line 91why, one step back
91 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:

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

line 92why, one step back
92 response.raise_for_status()

Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f

line 93why, one step back
93 assert exc_info.value.response == response

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

lines 94–98why, one step back
94 else:
95 assert response.raise_for_status() is None969798def test_options(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 99why, one step back
99 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 100why, one step back
100 response = http.options(server.url)

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

lines 101–105why, one step back
101 assert response.status_code == 200
102 assert response.reason_phrase == "OK"103104105def test_head(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 106why, one step back
106 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 107why, one step back
107 response = http.head(server.url)

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

lines 108–112why, one step back
108 assert response.status_code == 200
109 assert response.reason_phrase == "OK"110111112def test_put(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 113why, one step back
113 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 114why, one step back
114 response = http.put(server.url, data=b"Hello, world!")

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

lines 115–119why, one step back
115 assert response.status_code == 200
116 assert response.reason_phrase == "OK"117118119def test_patch(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 120why, one step back
120 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 121why, one step back
121 response = http.patch(server.url, data=b"Hello, world!")

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

lines 122–126why, one step back
122 assert response.status_code == 200
123 assert response.reason_phrase == "OK"124125126def test_delete(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 127why, one step back
127 with httpx.Client() as http:

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 128why, one step back
128 response = http.delete(server.url)

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

lines 129–130why, one step back
129 assert response.status_code == 200
130 assert response.reason_phrase == "OK"

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

lines 131–133why, one step back
131
132133def test_base_url(server):

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 134why, one step back
134 base_url = server.url

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Handle early connection closes (#103)Tom Christiebefore adoptionb9c8a15

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

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

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 138why, one step back
138 assert response.url == base_url

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

lines 139–146why, one step back
139
140141def test_merge_url():142 client = httpx.Client(base_url="https://www.paypal.com/")143 url = client.merge_url("http://www.paypal.com")144145 assert url.scheme == "https"146 assert url.is_ssl

Enforce that sync client uses asyncio-based backend (#232)Florimond Mancabefore adoption9c7d23c

lines 147–162why, one step back
147
148149class DerivedFromAsyncioBackend(httpx.AsyncioBackend):150 pass151152153class AnyBackend:154 pass155156157def test_client_backend_must_be_asyncio_based():158 httpx.Client(backend=httpx.AsyncioBackend())159 httpx.Client(backend=DerivedFromAsyncioBackend())160161 with pytest.raises(ValueError):162 httpx.Client(backend=AnyBackend())

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

lines 163–176why, one step back
163
164165def test_elapsed_delay(server):166 with httpx.Client() as http:167 response = http.get(server.url.copy_with(path="/slow_response/100"))168 assert response.elapsed.total_seconds() == pytest.approx(0.1, abs=0.01)169170171def test_elapsed_delay_ignores_read_time(server):172 with httpx.Client() as http:173 response = http.get(server.url.copy_with(path="/slow_response/50"), stream=True)174 sleep(0.1)175 response.read()176 assert response.elapsed.total_seconds() == pytest.approx(0.05, abs=0.01)