test_async_client.py
This file
Path
tests/client/test_async_client.py
162 lines · 5.1 KiB · Python
Last changed by
Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b
History of this fileTake it
The walk stopped after 200 commits: lines older than 45177d2 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 timedelta2Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 3–7why, one step back3import pytest45import httpx67Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 8why, one step back8async def test_get(server, backend):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 9why, one step back9 url = server.urlDrop sync (#544)Tom Christiebefore adoption206c537
line 10why, one step back10 async with httpx.Client(backend=backend) as client:Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 11–13why, one step back11 response = await client.get(url)12 assert response.status_code == 20013 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"Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 15–16why, one step back15 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)Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
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"}Drop sync (#544)Tom Christiebefore adoption206c537
line 23why, one step back23 async with httpx.Client(backend=backend) 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"3233Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
line 34why, one step back34@pytest.mark.asyncioParametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 35–38why, one step back35async 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:Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
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.urlDrop sync (#544)Tom Christiebefore adoption206c537
line 51why, one step back51 async with httpx.Client(backend=backend) as client:Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 52–55why, one step back52 response = await client.post(url, data=b"Hello, world!")53 assert response.status_code == 2005455Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 56why, one step back56async def test_post_json(server, backend):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 57why, one step back57 url = server.urlDrop sync (#544)Tom Christiebefore adoption206c537
line 58why, one step back58 async with httpx.Client(backend=backend) as client:Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 59–62why, one step back59 response = await client.post(url, json={"text": "Hello, world!"})60 assert response.status_code == 2006162Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 63why, one step back63async def test_stream_response(server, backend):Drop sync (#544)Tom Christiebefore adoption206c537
line 64why, one step back64 async with httpx.Client(backend=backend) 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()67Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 68–72why, one step back68 assert response.status_code == 20069 assert body == b"Hello, world!"70 assert response.content == b"Hello, world!"7172Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 73why, one step back73async def test_access_content_stream_response(server, backend):Drop sync (#544)Tom Christiebefore adoption206c537
line 74why, one step back74 async with httpx.Client(backend=backend) 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 pass77Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 78–82why, one step back78 assert response.status_code == 20079 with pytest.raises(httpx.ResponseNotRead):80 response.content8182Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 83why, one step back83async def test_stream_request(server, backend):Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 84–87why, one step back84 async def hello_world():85 yield b"Hello, "86 yield b"world!"87Drop sync (#544)Tom Christiebefore adoption206c537
line 88why, one step back88 async with httpx.Client(backend=backend) 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())Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 90–92why, one step back90 assert response.status_code == 2009192Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 93why, one step back93async def test_raise_for_status(server, backend):Drop sync (#544)Tom Christiebefore adoption206c537
line 94why, one step back94 async with httpx.Client(backend=backend) as client:Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
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}")Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
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:Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 102–107why, one step back102 response.raise_for_status()103 assert exc_info.value.response == response104 else:105 assert response.raise_for_status() is None106107Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 108why, one step back108async def test_options(server, backend):Drop sync (#544)Tom Christiebefore adoption206c537
line 109why, one step back109 async with httpx.Client(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 110why, one step back110 response = await client.options(server.url)Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 111–114why, one step back111 assert response.status_code == 200112 assert response.text == "Hello, world!"113114Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 115why, one step back115async def test_head(server, backend):Drop sync (#544)Tom Christiebefore adoption206c537
line 116why, one step back116 async with httpx.Client(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 117why, one step back117 response = await client.head(server.url)Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 118–121why, one step back118 assert response.status_code == 200119 assert response.text == ""120121Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 122why, one step back122async def test_put(server, backend):Drop sync (#544)Tom Christiebefore adoption206c537
line 123why, one step back123 async with httpx.Client(backend=backend) 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!")Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 125–127why, one step back125 assert response.status_code == 200126127Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 128why, one step back128async def test_patch(server, backend):Drop sync (#544)Tom Christiebefore adoption206c537
line 129why, one step back129 async with httpx.Client(backend=backend) 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!")Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 131–133why, one step back131 assert response.status_code == 200132133Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 134why, one step back134async def test_delete(server, backend):Drop sync (#544)Tom Christiebefore adoption206c537
line 135why, one step back135 async with httpx.Client(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 136why, one step back136 response = await client.delete(server.url)Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 137–140why, one step back137 assert response.status_code == 200138 assert response.text == "Hello, world!"139140Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 141why, one step back141async def test_100_continue(server, backend):Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
lines 142–144why, one step back142 headers = {"Expect": "100-continue"}143 data = b"Echo request body"144Drop sync (#544)Tom Christiebefore adoption206c537
line 145why, one step back145 async with httpx.Client(backend=backend) 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 )Rely on IRIReference.encode() for IDNA encoding (#210)Can Sarıgölbefore adoption45177d2
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 NoneDrop sync (#544)Tom Christiebefore adoption206c537
line 158why, one step back158 async with httpx.Client(backend=backend, 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"