test_async_client.py
This file
Path
tests/client/test_async_client.py
176 lines · 5.3 KiB · Python
Last changed by
Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
History of this fileTake it
The walk stopped after 200 commits: lines older than 994403b point at it.
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
lines 1–2why, one step back1from datetime import timedelta2Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 3–8why, one step back3import pytest45import httpx678async def test_get(server, backend):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 9why, one step back9 url = server.urlRefactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 10why, one step back10 async with httpx.Client() as client:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 11–16why, one step back11 response = await client.get(url)12 assert response.status_code == 20013 assert response.text == "Hello, world!"14 assert response.http_version == "HTTP/1.1"15 assert response.headers16 assert repr(response) == "<Response [200 OK]>"Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
line 17why, one step back17 assert response.elapsed > timedelta(seconds=0)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 18–19why, one step back1819Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 20–22why, one step back20async def test_build_request(server, backend):21 url = server.url.copy_with(path="/echo_headers")22 headers = {"Custom-header": "value"}Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 23why, one step back23 async with httpx.Client() as client:Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 24–33why, one step back24 request = client.build_request("GET", url)25 request.headers.update(headers)26 response = await client.send(request)2728 assert response.status_code == 20029 assert response.url == url3031 assert response.json()["Custom-header"] == "value"3233Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 34–38why, one step back34@pytest.mark.asyncio35async def test_get_no_backend(server):36 """37 Verify that the client is capable of making a simple request if not given a backend.38 """Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 39why, one step back39 url = server.urlDrop sync (#544)Tom Christiebefore adoption206c537
line 40why, one step back40 async with httpx.Client() as client:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 41–49why, one step back41 response = await client.get(url)42 assert response.status_code == 20043 assert response.text == "Hello, world!"44 assert response.http_version == "HTTP/1.1"45 assert response.headers46 assert repr(response) == "<Response [200 OK]>"474849async def test_post(server, backend):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 50why, one step back50 url = server.urlRefactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 51why, one step back51 async with httpx.Client() as client:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 52–56why, one step back52 response = await client.post(url, data=b"Hello, world!")53 assert response.status_code == 200545556async def test_post_json(server, backend):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 57why, one step back57 url = server.urlRefactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 58why, one step back58 async with httpx.Client() as client:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 59–63why, one step back59 response = await client.post(url, json={"text": "Hello, world!"})60 assert response.status_code == 200616263async def test_stream_response(server, backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 64why, one step back64 async with httpx.Client() as client:Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b
lines 65–67why, one step back65 async with client.stream("GET", server.url) as response:66 body = await response.read()67Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 68–73why, one step back68 assert response.status_code == 20069 assert body == b"Hello, world!"70 assert response.content == b"Hello, world!"717273async def test_access_content_stream_response(server, backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 74why, one step back74 async with httpx.Client() as client:Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b
lines 75–77why, one step back75 async with client.stream("GET", server.url) as response:76 pass77Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 78–87why, one step back78 assert response.status_code == 20079 with pytest.raises(httpx.ResponseNotRead):80 response.content818283async def test_stream_request(server, backend):84 async def hello_world():85 yield b"Hello, "86 yield b"world!"87Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 88why, one step back88 async with httpx.Client() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 89why, one step back89 response = await client.request("POST", server.url, data=hello_world())Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 90–93why, one step back90 assert response.status_code == 200919293async def test_raise_for_status(server, backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 94why, one step back94 async with httpx.Client() as client:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 95–96why, one step back95 for status_code in (200, 400, 404, 500, 505):96 response = await client.request(Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 97why, one step back97 "GET", server.url.copy_with(path=f"/status/{status_code}")Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 98–100why, one step back98 )99100 if 400 <= status_code < 600:Make HTTPError importable from the top-level (#421)Josep Cugatbefore adoptiona028256
line 101why, one step back101 with pytest.raises(httpx.HTTPError) as exc_info:Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 102–108why, one step back102 response.raise_for_status()103 assert exc_info.value.response == response104 else:105 assert response.raise_for_status() is None106107108async def test_options(server, backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 109why, one step back109 async with httpx.Client() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 110why, one step back110 response = await client.options(server.url)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 111–115why, one step back111 assert response.status_code == 200112 assert response.text == "Hello, world!"113114115async def test_head(server, backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 116why, one step back116 async with httpx.Client() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 117why, one step back117 response = await client.head(server.url)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 118–122why, one step back118 assert response.status_code == 200119 assert response.text == ""120121122async def test_put(server, backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 123why, one step back123 async with httpx.Client() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 124why, one step back124 response = await client.put(server.url, data=b"Hello, world!")Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 125–128why, one step back125 assert response.status_code == 200126127128async def test_patch(server, backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 129why, one step back129 async with httpx.Client() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 130why, one step back130 response = await client.patch(server.url, data=b"Hello, world!")Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 131–134why, one step back131 assert response.status_code == 200132133134async def test_delete(server, backend):Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 135why, one step back135 async with httpx.Client() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 136why, one step back136 response = await client.delete(server.url)Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 137–144why, one step back137 assert response.status_code == 200138 assert response.text == "Hello, world!"139140141async def test_100_continue(server, backend):142 headers = {"Expect": "100-continue"}143 data = b"Echo request body"144Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 145why, one step back145 async with httpx.Client() as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
lines 146–148why, one step back146 response = await client.post(147 server.url.copy_with(path="/echo_body"), headers=headers, data=data148 )Serve test server in thread (#292)Florimond Mancabefore adoption994403b
lines 149–151why, one step back149150 assert response.status_code == 200151 assert response.content == dataAdd support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 152–157why, one step back152153154async def test_uds(uds_server, backend):155 url = uds_server.url156 uds = uds_server.config.uds157 assert uds is not NoneRefactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
line 158why, one step back158 async with httpx.Client(uds=uds) as client:Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 159–162why, one step back159 response = await client.get(url)160 assert response.status_code == 200161 assert response.text == "Hello, world!"162 assert response.encoding == "iso-8859-1"Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
lines 163–176why, one step back163164165@pytest.mark.parametrize(166 "backend",167 [168 pytest.param("asyncio", marks=pytest.mark.asyncio),169 pytest.param("trio", marks=pytest.mark.trio),170 ],171)172async def test_explicit_backend(server, backend):173 async with httpx.Client(backend=backend) as client:174 response = await client.get(server.url)175 assert response.status_code == 200176 assert response.text == "Hello, world!"