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 fileTake it
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
lines 1–3why, one step back1from datetime import timedelta2from time import sleep3Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 4–5why, one step back4import pytest5Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 6why, one step back6import httpxMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 7–8why, one step back78Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 9why, one step back9def test_get(server):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 10why, one step back10 url = server.urlRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 11why, one step back11 with httpx.Client() as http:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
line 12why, one step back12 response = http.get(url)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 13why, one step back13 assert response.status_code == 200Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 14why, one step back14 assert response.url == urlMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 15why, one step back15 assert response.content == b"Hello, world!"Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 16why, one step back16 assert response.text == "Hello, world!"Response.protocol -> Response.http_version (#250)Tom Christiebefore adoptionc1ec1c3
line 17why, one step back17 assert response.http_version == "HTTP/1.1"Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
line 18why, one step back18 assert response.encoding == "iso-8859-1"Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 19why, one step back19 assert response.request.url == urlMerge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 20–21why, one step back20 assert response.headers21 assert response.is_redirect is FalseFixed response repr. (#101)updatatodaybefore adoptionf9e3ad4
line 22why, one step back22 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 back23 assert response.elapsed > timedelta(0)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 24–25why, one step back2425Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 26–40why, one step back26def 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"3940Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 41why, one step back41def test_post(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 42why, one step back42 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 43why, one step back43 response = http.post(server.url, data=b"Hello, world!")Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 44why, one step back44 assert response.status_code == 200Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 45why, one step back45 assert response.reason_phrase == "OK"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 46–47why, one step back4647Add json interfacesTom Christiebefore adoption5a63d31
line 48why, one step back48def test_post_json(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 49why, one step back49 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 50why, one step back50 response = http.post(server.url, json={"text": "Hello, world!"})Add json interfacesTom Christiebefore adoption5a63d31
lines 51–54why, one step back51 assert response.status_code == 20052 assert response.reason_phrase == "OK"5354Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 55why, one step back55def test_stream_response(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 56why, one step back56 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 57why, one step back57 response = http.get(server.url, stream=True)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 58why, one step back58 assert response.status_code == 200Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 59–60why, one step back59 content = response.read()60 assert content == b"Hello, world!"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 61–62why, one step back6162Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 63why, one step back63def test_stream_iterator(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 64why, one step back64 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 65why, one step back65 response = http.get(server.url, stream=True)Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 66–70why, one step back66 assert response.status_code == 20067 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 back71Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 72–73why, one step back7273def test_raw_iterator(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 74why, one step back74 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 75why, one step back75 response = http.get(server.url, stream=True)Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 76–80why, one step back76 assert response.status_code == 20077 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 back81 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 back85 with httpx.Client() as client:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 86–87why, one step back86 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 back88 "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 back89 )90 if 400 <= status_code < 600:Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f
line 91why, one step back91 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 back92 response.raise_for_status()Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f
line 93why, one step back93 assert exc_info.value.response == responseMerge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 94–98why, one step back94 else:95 assert response.raise_for_status() is None969798def test_options(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 99why, one step back99 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 100why, one step back100 response = http.options(server.url)Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 101–105why, one step back101 assert response.status_code == 200102 assert response.reason_phrase == "OK"103104105def test_head(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 106why, one step back106 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 107why, one step back107 response = http.head(server.url)Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 108–112why, one step back108 assert response.status_code == 200109 assert response.reason_phrase == "OK"110111112def test_put(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 113why, one step back113 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 114why, one step back114 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 back115 assert response.status_code == 200116 assert response.reason_phrase == "OK"117118119def test_patch(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 120why, one step back120 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 121why, one step back121 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 back122 assert response.status_code == 200123 assert response.reason_phrase == "OK"124125126def test_delete(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 127why, one step back127 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 128why, one step back128 response = http.delete(server.url)Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 129–130why, one step back129 assert response.status_code == 200130 assert response.reason_phrase == "OK"Support Client(base_url=...)Tom Christiebefore adoption9d5aaff
lines 131–133why, one step back131132133def test_base_url(server):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 134why, one step back134 base_url = server.urlRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 135why, one step back135 with httpx.Client(base_url=base_url) as http:Handle early connection closes (#103)Tom Christiebefore adoptionb9c8a15
line 136why, one step back136 response = http.get("/")Support Client(base_url=...)Tom Christiebefore adoption9d5aaff
line 137why, one step back137 assert response.status_code == 200Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 138why, one step back138 assert response.url == base_urlMove HSTS preload checking to client (#184)Seth Michael Larsonbefore adoption9142a89
lines 139–146why, one step back139140141def 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_sslEnforce that sync client uses asyncio-based backend (#232)Florimond Mancabefore adoption9c7d23c
lines 147–162why, one step back147148149class 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 back163164165def 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)