test_client.py
This file
Path
tests/client/test_client.py
158 lines · 4.4 KiB · Python
Last changed by
Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
History of this fileTake it
Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 1–2why, one step back1import pytest2Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 3why, one step back3import httpxMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 4–5why, one step back45Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 6why, one step back6def test_get(server):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 7why, one step back7 url = server.urlRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 8why, one step back8 with httpx.Client() as http:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
line 9why, one step back9 response = http.get(url)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 10why, one step back10 assert response.status_code == 200Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 11why, one step back11 assert response.url == urlMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 12why, one step back12 assert response.content == b"Hello, world!"Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 13why, one step back13 assert response.text == "Hello, world!"Response.protocol -> Response.http_version (#250)Tom Christiebefore adoptionc1ec1c3
line 14why, one step back14 assert response.http_version == "HTTP/1.1"Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
line 15why, one step back15 assert response.encoding == "iso-8859-1"Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 16why, one step back16 assert response.request.url == urlMerge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 17–18why, one step back17 assert response.headers18 assert response.is_redirect is FalseFixed response repr. (#101)updatatodaybefore adoptionf9e3ad4
line 19why, one step back19 assert repr(response) == "<Response [200 OK]>"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 20–21why, one step back2021Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 22–36why, one step back22def test_build_request(server):23 url = server.url.copy_with(path="/echo_headers")24 headers = {"Custom-header": "value"}2526 with httpx.Client() as http:27 request = http.build_request("GET", url)28 request.headers.update(headers)29 response = http.send(request)3031 assert response.status_code == 20032 assert response.url == url3334 assert response.json()["Custom-header"] == "value"3536Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 37why, one step back37def test_post(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 38why, one step back38 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 39why, one step back39 response = http.post(server.url, data=b"Hello, world!")Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 40why, one step back40 assert response.status_code == 200Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 41why, one step back41 assert response.reason_phrase == "OK"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 42–43why, one step back4243Add json interfacesTom Christiebefore adoption5a63d31
line 44why, one step back44def test_post_json(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 45why, one step back45 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 46why, one step back46 response = http.post(server.url, json={"text": "Hello, world!"})Add json interfacesTom Christiebefore adoption5a63d31
lines 47–50why, one step back47 assert response.status_code == 20048 assert response.reason_phrase == "OK"4950Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 51why, one step back51def test_stream_response(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 52why, one step back52 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 53why, one step back53 response = http.get(server.url, stream=True)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 54why, one step back54 assert response.status_code == 200Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 55–56why, one step back55 content = response.read()56 assert content == b"Hello, world!"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 57–58why, one step back5758Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 59why, one step back59def test_stream_iterator(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 60why, one step back60 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 61why, one step back61 response = http.get(server.url, stream=True)Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 62–66why, one step back62 assert response.status_code == 20063 body = b""64 for chunk in response.stream():65 body += chunk66 assert body == b"Hello, world!"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 67why, one step back67Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 68–69why, one step back6869def test_raw_iterator(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 70why, one step back70 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 71why, one step back71 response = http.get(server.url, stream=True)Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 72–76why, one step back72 assert response.status_code == 20073 body = b""74 for chunk in response.raw():75 body += chunk76 assert body == b"Hello, world!"Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 77–80why, one step back77 response.close() # TODO: should Response be available as context managers?787980def test_raise_for_status(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 81why, one step back81 with httpx.Client() as client:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 82–83why, one step back82 for status_code in (200, 400, 404, 500, 505):83 response = client.request(Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 84why, one step back84 "GET", server.url.copy_with(path="/status/{}".format(status_code))Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 85–86why, one step back85 )86 if 400 <= status_code < 600:Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f
line 87why, one step back87 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
line 88why, one step back88 response.raise_for_status()Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f
line 89why, one step back89 assert exc_info.value.response == responseMerge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 90–94why, one step back90 else:91 assert response.raise_for_status() is None929394def test_options(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 95why, one step back95 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 96why, one step back96 response = http.options(server.url)Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 97–101why, one step back97 assert response.status_code == 20098 assert response.reason_phrase == "OK"99100101def test_head(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 102why, one step back102 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 103why, one step back103 response = http.head(server.url)Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 104–108why, one step back104 assert response.status_code == 200105 assert response.reason_phrase == "OK"106107108def test_put(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 109why, one step back109 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 110why, one step back110 response = http.put(server.url, data=b"Hello, world!")Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 111–115why, one step back111 assert response.status_code == 200112 assert response.reason_phrase == "OK"113114115def test_patch(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 116why, one step back116 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 117why, one step back117 response = http.patch(server.url, data=b"Hello, world!")Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 118–122why, one step back118 assert response.status_code == 200119 assert response.reason_phrase == "OK"120121122def test_delete(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 123why, one step back123 with httpx.Client() as http:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 124why, one step back124 response = http.delete(server.url)Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 125–126why, one step back125 assert response.status_code == 200126 assert response.reason_phrase == "OK"Support Client(base_url=...)Tom Christiebefore adoption9d5aaff
lines 127–129why, one step back127128129def test_base_url(server):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 130why, one step back130 base_url = server.urlRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 131why, one step back131 with httpx.Client(base_url=base_url) as http:Handle early connection closes (#103)Tom Christiebefore adoptionb9c8a15
line 132why, one step back132 response = http.get("/")Support Client(base_url=...)Tom Christiebefore adoption9d5aaff
line 133why, one step back133 assert response.status_code == 200Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 134why, one step back134 assert response.url == base_urlMove HSTS preload checking to client (#184)Seth Michael Larsonbefore adoption9142a89
lines 135–142why, one step back135136137def test_merge_url():138 client = httpx.Client(base_url="https://www.paypal.com/")139 url = client.merge_url("http://www.paypal.com")140141 assert url.scheme == "https"142 assert url.is_sslEnforce that sync client uses asyncio-based backend (#232)Florimond Mancabefore adoption9c7d23c
lines 143–158why, one step back143144145class DerivedFromAsyncioBackend(httpx.AsyncioBackend):146 pass147148149class AnyBackend:150 pass151152153def test_client_backend_must_be_asyncio_based():154 httpx.Client(backend=httpx.AsyncioBackend())155 httpx.Client(backend=DerivedFromAsyncioBackend())156157 with pytest.raises(ValueError):158 httpx.Client(backend=AnyBackend())