test_async_client.py
This file
Path
tests/client/test_async_client.py
148 lines · 4.9 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
The walk stopped after 200 commits: lines older than 2116d44 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 timedelta2Add response.json()Tom Christiebefore adoption2116d44
lines 3–4why, one step back3import pytest4Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 5why, one step back5import httpxAdd response.json()Tom Christiebefore adoption2116d44
lines 6–7why, one step back67Parametrize 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.urlParametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 10why, one step back10 async with httpx.AsyncClient(backend=backend) as client:Add response.json()Tom Christiebefore adoption2116d44
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"Add response.json()Tom Christiebefore adoption2116d44
line 15why, one step back15 assert response.headersFixed response repr. (#101)updatatodaybefore adoptionf9e3ad4
line 16why, one step back16 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)Add response.json()Tom Christiebefore adoption2116d44
lines 18–19why, one step back1819Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 20–33why, one step back20async def test_build_request(server, backend):21 url = server.url.copy_with(path="/echo_headers")22 headers = {"Custom-header": "value"}23 async with httpx.AsyncClient(backend=backend) as client:24 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"3233Add response.json()Tom Christiebefore adoption2116d44
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.urlRenaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 40why, one step back40 async with httpx.AsyncClient() 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.urlParametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 51why, one step back51 async with httpx.AsyncClient(backend=backend) as client:Add response.json()Tom Christiebefore adoption2116d44
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.urlParametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 58why, one step back58 async with httpx.AsyncClient(backend=backend) as client:Add json interfacesTom Christiebefore adoption5a63d31
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
lines 63–64why, one step back63async def test_stream_response(server, backend):64 async with httpx.AsyncClient(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 65why, one step back65 response = await client.request("GET", server.url, stream=True)Add response.json()Tom Christiebefore adoption2116d44
lines 66–71why, one step back66 assert response.status_code == 20067 body = await response.read()68 assert body == b"Hello, world!"69 assert response.content == b"Hello, world!"7071Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 72–73why, one step back72async def test_access_content_stream_response(server, backend):73 async with httpx.AsyncClient(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 74why, one step back74 response = await client.request("GET", server.url, stream=True)Add response.json()Tom Christiebefore adoption2116d44
line 75why, one step back75 assert response.status_code == 200Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 76why, one step back76 with pytest.raises(httpx.ResponseNotRead):Add response.json()Tom Christiebefore adoption2116d44
lines 77–79why, one step back77 response.content7879Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 80why, one step back80async def test_stream_request(server, backend):Add response.json()Tom Christiebefore adoption2116d44
lines 81–84why, one step back81 async def hello_world():82 yield b"Hello, "83 yield b"world!"84Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 85why, one step back85 async with httpx.AsyncClient(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 86why, one step back86 response = await client.request("POST", server.url, data=hello_world())Add response.json()Tom Christiebefore adoption2116d44
lines 87–89why, one step back87 assert response.status_code == 2008889Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 90–91why, one step back90async def test_raise_for_status(server, backend):91 async with httpx.AsyncClient(backend=backend) as client:Add response.json()Tom Christiebefore adoption2116d44
lines 92–93why, one step back92 for status_code in (200, 400, 404, 500, 505):93 response = await client.request(Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 94why, one step back94 "GET", server.url.copy_with(path=f"/status/{status_code}")Add response.json()Tom Christiebefore adoption2116d44
lines 95–97why, one step back95 )9697 if 400 <= status_code < 600:Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f
line 98why, one step back98 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:Add response.json()Tom Christiebefore adoption2116d44
line 99why, one step back99 response.raise_for_status()Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f
line 100why, one step back100 assert exc_info.value.response == responseAdd response.json()Tom Christiebefore adoption2116d44
lines 101–104why, one step back101 else:102 assert response.raise_for_status() is None103104Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 105–106why, one step back105async def test_options(server, backend):106 async with httpx.AsyncClient(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 107why, one step back107 response = await client.options(server.url)Add response.json()Tom Christiebefore adoption2116d44
lines 108–111why, one step back108 assert response.status_code == 200109 assert response.text == "Hello, world!"110111Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 112–113why, one step back112async def test_head(server, backend):113 async with httpx.AsyncClient(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 114why, one step back114 response = await client.head(server.url)Add response.json()Tom Christiebefore adoption2116d44
lines 115–118why, one step back115 assert response.status_code == 200116 assert response.text == ""117118Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 119–120why, one step back119async def test_put(server, backend):120 async with httpx.AsyncClient(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 121why, one step back121 response = await client.put(server.url, data=b"Hello, world!")Add response.json()Tom Christiebefore adoption2116d44
lines 122–124why, one step back122 assert response.status_code == 200123124Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 125–126why, one step back125async def test_patch(server, backend):126 async with httpx.AsyncClient(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 127why, one step back127 response = await client.patch(server.url, data=b"Hello, world!")Add response.json()Tom Christiebefore adoption2116d44
lines 128–130why, one step back128 assert response.status_code == 200129130Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
lines 131–132why, one step back131async def test_delete(server, backend):132 async with httpx.AsyncClient(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 133why, one step back133 response = await client.delete(server.url)Add response.json()Tom Christiebefore adoption2116d44
lines 134–137why, one step back134 assert response.status_code == 200135 assert response.text == "Hello, world!"136137Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 138why, one step back138async def test_100_continue(server, backend):Add response.json()Tom Christiebefore adoption2116d44
lines 139–141why, one step back139 headers = {"Expect": "100-continue"}140 data = b"Echo request body"141Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058
line 142why, one step back142 async with httpx.AsyncClient(backend=backend) as client:Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
lines 143–145why, one step back143 response = await client.post(144 server.url.copy_with(path="/echo_body"), headers=headers, data=data145 )Add response.json()Tom Christiebefore adoption2116d44
lines 146–148why, one step back146147 assert response.status_code == 200148 assert response.content == data