Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

184 lines · 5.7 KiB · Python

Last changed by

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

History of this file

Take it

The walk stopped after 200 commits: lines older than bc54dd0 point at it.

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 1–2why, one step back
1from datetime import timedelta
2

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

line 3why, one step back
3import httpcore

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 4–6why, one step back
4import pytest
56import httpx

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

line 7why, one step back
7from httpx import ASGIDispatch

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 8–9why, one step back
8
9

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 10–11why, one step back
10@pytest.mark.usefixtures("async_environment")
11async def test_get(server):

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 12why, one step back
12 url = server.url

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 13why, one step back
13 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 14–22why, one step back
14 response = await client.get(url)
15 assert response.status_code == 20016 assert response.text == "Hello, world!"17 assert response.http_version == "HTTP/1.1"18 assert response.headers19 assert repr(response) == "<Response [200 OK]>"20 assert response.elapsed > timedelta(seconds=0)2122

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

line 23why, one step back
23@pytest.mark.usefixtures("async_environment")

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

lines 24–30why, one step back
24async def test_get_invalid_url(server):
25 async with httpx.AsyncClient() as client:26 with pytest.raises(httpx.InvalidURL):27 await client.get("invalid://example.org")282930@pytest.mark.usefixtures("async_environment")

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

line 31why, one step back
31async def test_build_request(server):

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 32–33why, one step back
32 url = server.url.copy_with(path="/echo_headers")
33 headers = {"Custom-header": "value"}

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 34why, one step back
34 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 35–44why, one step back
35 request = client.build_request("GET", url)
36 request.headers.update(headers)37 response = await client.send(request)3839 assert response.status_code == 20040 assert response.url == url4142 assert response.json()["Custom-header"] == "value"4344

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 45–46why, one step back
45@pytest.mark.usefixtures("async_environment")
46async def test_post(server):

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 47why, one step back
47 url = server.url

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 48why, one step back
48 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 49–52why, one step back
49 response = await client.post(url, data=b"Hello, world!")
50 assert response.status_code == 2005152

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 53–54why, one step back
53@pytest.mark.usefixtures("async_environment")
54async def test_post_json(server):

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 55why, one step back
55 url = server.url

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 56why, one step back
56 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 57–60why, one step back
57 response = await client.post(url, json={"text": "Hello, world!"})
58 assert response.status_code == 2005960

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 61–62why, one step back
61@pytest.mark.usefixtures("async_environment")
62async def test_stream_response(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 63why, one step back
63 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

line 64why, one step back
64 async with client.stream("GET", server.url) as response:

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 65why, one step back
65 body = await response.aread()

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 66–71why, one step back
66
67 assert response.status_code == 20068 assert body == b"Hello, world!"69 assert response.content == b"Hello, world!"7071

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 72–73why, one step back
72@pytest.mark.usefixtures("async_environment")
73async def test_access_content_stream_response(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 74why, one step back
74 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 75–82why, one step back
75 async with client.stream("GET", server.url) as response:
76 pass7778 assert response.status_code == 20079 with pytest.raises(httpx.ResponseNotRead):80 response.content8182

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 83–84why, one step back
83@pytest.mark.usefixtures("async_environment")
84async def test_stream_request(server):

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 85–88why, one step back
85 async def hello_world():
86 yield b"Hello, "87 yield b"world!"88

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 89why, one step back
89 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 90–93why, one step back
90 response = await client.request("POST", server.url, data=hello_world())
91 assert response.status_code == 2009293

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 94–95why, one step back
94@pytest.mark.usefixtures("async_environment")
95async def test_raise_for_status(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 96why, one step back
96 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 97–106why, one step back
97 for status_code in (200, 400, 404, 500, 505):
98 response = await client.request(99 "GET", server.url.copy_with(path=f"/status/{status_code}")100 )101102 if 400 <= status_code < 600:103 with pytest.raises(httpx.HTTPError) as exc_info:104 response.raise_for_status()105 assert exc_info.value.response == response106 else:

Fixed mypy errors in test_async_client.py and test_client.py (#985)Hasan Ramezanibefore adoption21d7e16

line 107why, one step back
107 assert response.raise_for_status() is None # type: ignore

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 108–109why, one step back
108
109

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 110–111why, one step back
110@pytest.mark.usefixtures("async_environment")
111async def test_options(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 112why, one step back
112 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 113–117why, one step back
113 response = await client.options(server.url)
114 assert response.status_code == 200115 assert response.text == "Hello, world!"116117

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 118–119why, one step back
118@pytest.mark.usefixtures("async_environment")
119async def test_head(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 120why, one step back
120 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 121–125why, one step back
121 response = await client.head(server.url)
122 assert response.status_code == 200123 assert response.text == ""124125

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 126–127why, one step back
126@pytest.mark.usefixtures("async_environment")
127async def test_put(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 128why, one step back
128 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 129–132why, one step back
129 response = await client.put(server.url, data=b"Hello, world!")
130 assert response.status_code == 200131132

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 133–134why, one step back
133@pytest.mark.usefixtures("async_environment")
134async def test_patch(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 135why, one step back
135 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 136–139why, one step back
136 response = await client.patch(server.url, data=b"Hello, world!")
137 assert response.status_code == 200138139

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 140–141why, one step back
140@pytest.mark.usefixtures("async_environment")
141async def test_delete(server):

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 142why, one step back
142 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 143–147why, one step back
143 response = await client.delete(server.url)
144 assert response.status_code == 200145 assert response.text == "Hello, world!"146147

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 148–149why, one step back
148@pytest.mark.usefixtures("async_environment")
149async def test_100_continue(server):

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 150–152why, one step back
150 headers = {"Expect": "100-continue"}
151 data = b"Echo request body"152

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 153why, one step back
153 async with httpx.AsyncClient() as client:

Backend operations like .read(), .write() now have a manadatory timeout argument. (#611)Tom Christiebefore adoptionbc54dd0

lines 154–159why, one step back
154 response = await client.post(
155 server.url.copy_with(path="/echo_body"), headers=headers, data=data156 )157158 assert response.status_code == 200159 assert response.content == data

Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210

lines 160–184why, one step back
160
161162def test_dispatch_deprecated():163 dispatch = httpcore.AsyncHTTPTransport()164165 with pytest.warns(DeprecationWarning) as record:166 client = httpx.AsyncClient(dispatch=dispatch)167168 assert client.transport is dispatch169 assert len(record) == 1170 assert record[0].message.args[0] == (171 "The dispatch argument is deprecated since v0.13 and will be "172 "removed in a future release, please use 'transport'"173 )174175176def test_asgi_dispatch_deprecated():177 with pytest.warns(DeprecationWarning) as record:178 ASGIDispatch(None)179180 assert len(record) == 1181 assert (182 record[0].message.args[0]183 == "ASGIDispatch is deprecated, please use ASGITransport"184 )