test_client.py
This file
Path
tests/client/test_client.py
176 lines · 4.7 KiB · Python
Last changed by
Enforce that sync client uses asyncio-based backend (#232)Florimond Mancabefore adoption9c7d23c
History of this fileTake it
Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 1–3why, one step back1import asyncio2import functools3Merge 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
lines 9–12why, one step back9def 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 back13Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 14–16why, one step back14 @functools.wraps(func)15 async def wrapped(*args, **kwargs):16 nonlocal funcMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 17why, one step back17Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 18–21why, one step back18 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 back22Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 23why, one step back23 return pytest.mark.asyncio(wrapped)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 24why, one step back24Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 25–27why, one step back2526@threadpool27def test_get(server):Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
line 28why, one step back28 url = "http://127.0.0.1:8000/"Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 29why, one step back29 with httpx.Client() as http:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
line 30why, one step back30 response = http.get(url)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 31why, one step back31 assert response.status_code == 200Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 32why, one step back32 assert response.url == httpx.URL(url)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 33why, one step back33 assert response.content == b"Hello, world!"Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 34why, one step back34 assert response.text == "Hello, world!"Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 35–36why, one step back35 assert response.protocol == "HTTP/1.1"36 assert response.encoding == "iso-8859-1"Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 37why, one step back37 assert response.request.url == httpx.URL(url)Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 38–39why, one step back38 assert response.headers39 assert response.is_redirect is FalseFixed response repr. (#101)updatatodaybefore adoptionf9e3ad4
line 40why, one step back40 assert repr(response) == "<Response [200 OK]>"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 41–42why, one step back4142Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 43–44why, one step back43@threadpool44def test_post(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 45why, one step back45 with httpx.Client() as http:Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 46why, one step back46 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 back47 assert response.status_code == 200Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 48why, one step back48 assert response.reason_phrase == "OK"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 49–50why, one step back4950Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 51why, one step back51@threadpoolAdd json interfacesTom Christiebefore adoption5a63d31
line 52why, one step back52def test_post_json(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 53why, one step back53 with httpx.Client() as http:Add json interfacesTom Christiebefore adoption5a63d31
lines 54–59why, one step back54 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@threadpoolMerge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 60why, one step back60def test_stream_response(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 61why, one step back61 with httpx.Client() as http:Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 62why, one step back62 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 back63 assert response.status_code == 200Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 64–65why, one step back64 content = response.read()65 assert content == b"Hello, world!"Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 66–67why, one step back6667Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 68–69why, one step back68@threadpool69def test_stream_iterator(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 70why, one step back70 with httpx.Client() as http:Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 71–76why, one step back71 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 back77Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 78–80why, one step back7879@threadpool80def test_raw_iterator(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 81why, one step back81 with httpx.Client() as http:Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 82–87why, one step back82 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 back88 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 back93 with httpx.Client() as client:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 94–98why, one step back94 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 )98 if 400 <= status_code < 600:Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f
line 99why, one step back99 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
line 100why, one step back100 response.raise_for_status()Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f
line 101why, one step back101 assert exc_info.value.response == responseMerge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 102–107why, one step back102 else:103 assert response.raise_for_status() is None104105106@threadpool107def test_options(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 108why, one step back108 with httpx.Client() as http:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 109–115why, one step back109 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 back116 with httpx.Client() as http:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 117–123why, one step back117 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 back124 with httpx.Client() as http:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 125–131why, one step back125 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 back132 with httpx.Client() as http:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 133–139why, one step back133 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 back140 with httpx.Client() as http:Merge pull request #56 from yeraydiazdiaz/test-additional-http-methodsTom Christiebefore adoption87cab4c
lines 141–143why, one step back141 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 back144145146@threadpool147def test_base_url(server):148 base_url = "http://127.0.0.1:8000/"Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 149why, one step back149 with httpx.Client(base_url=base_url) as http:Handle early connection closes (#103)Tom Christiebefore adoptionb9c8a15
line 150why, one step back150 response = http.get("/")Support Client(base_url=...)Tom Christiebefore adoption9d5aaff
lines 151–152why, one step back151 assert response.status_code == 200152 assert str(response.url) == base_urlMove HSTS preload checking to client (#184)Seth Michael Larsonbefore adoption9142a89
lines 153–160why, one step back153154155def 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_sslEnforce that sync client uses asyncio-based backend (#232)Florimond Mancabefore adoption9c7d23c
lines 161–176why, one step back161162163class DerivedFromAsyncioBackend(httpx.AsyncioBackend):164 pass165166167class AnyBackend:168 pass169170171def test_client_backend_must_be_asyncio_based():172 httpx.Client(backend=httpx.AsyncioBackend())173 httpx.Client(backend=DerivedFromAsyncioBackend())174175 with pytest.raises(ValueError):176 httpx.Client(backend=AnyBackend())