Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

168 lines · 5.3 KiB · Python

Last changed by

HTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5

History of this file

Take it

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

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

lines 1–10why, one step back
1from datetime import timedelta
23import pytest45import httpx678@pytest.mark.usefixtures("async_environment")9async def test_get(server):10 url = server.url

HTTP/2 becomes fully optional (#1140)Tom Christiebefore adoption8d9dfb5

line 11why, one step back
11 async with httpx.AsyncClient(http2=True) as client:

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

lines 12–19why, one step back
12 response = await client.get(url)
13 assert response.status_code == 20014 assert response.text == "Hello, world!"15 assert response.http_version == "HTTP/1.1"16 assert response.headers17 assert repr(response) == "<Response [200 OK]>"18 assert response.elapsed > timedelta(seconds=0)19

Full coverage, with exception of URLLib3Transport (#1086)Tom Christiebefore adoption2d491c9

lines 20–22why, one step back
20 with pytest.raises(httpx.NotRedirectResponse):
21 await response.anext()22

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

line 23why, one step back
23

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

lines 24–31why, one step back
24@pytest.mark.parametrize(
25 "url",26 [27 pytest.param("invalid://example.org", id="scheme-not-http(s)"),28 pytest.param("://example.org", id="no-scheme"),29 pytest.param("http://", id="no-host"),30 ],31)

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

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

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

line 33why, one step back
33async def test_get_invalid_url(server, url):

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

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

Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722

line 35why, one step back
35 with pytest.raises((httpx.UnsupportedProtocol, httpx.LocalProtocolError)):

Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef

line 36why, one step back
36 await client.get(url)

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

lines 37–39why, one step back
37
3839@pytest.mark.usefixtures("async_environment")

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

lines 40–111why, one step back
40async def test_build_request(server):
41 url = server.url.copy_with(path="/echo_headers")42 headers = {"Custom-header": "value"}43 async with httpx.AsyncClient() as client:44 request = client.build_request("GET", url)45 request.headers.update(headers)46 response = await client.send(request)4748 assert response.status_code == 20049 assert response.url == url5051 assert response.json()["Custom-header"] == "value"525354@pytest.mark.usefixtures("async_environment")55async def test_post(server):56 url = server.url57 async with httpx.AsyncClient() as client:58 response = await client.post(url, data=b"Hello, world!")59 assert response.status_code == 200606162@pytest.mark.usefixtures("async_environment")63async def test_post_json(server):64 url = server.url65 async with httpx.AsyncClient() as client:66 response = await client.post(url, json={"text": "Hello, world!"})67 assert response.status_code == 200686970@pytest.mark.usefixtures("async_environment")71async def test_stream_response(server):72 async with httpx.AsyncClient() as client:73 async with client.stream("GET", server.url) as response:74 body = await response.aread()7576 assert response.status_code == 20077 assert body == b"Hello, world!"78 assert response.content == b"Hello, world!"798081@pytest.mark.usefixtures("async_environment")82async def test_access_content_stream_response(server):83 async with httpx.AsyncClient() as client:84 async with client.stream("GET", server.url) as response:85 pass8687 assert response.status_code == 20088 with pytest.raises(httpx.ResponseNotRead):89 response.content909192@pytest.mark.usefixtures("async_environment")93async def test_stream_request(server):94 async def hello_world():95 yield b"Hello, "96 yield b"world!"9798 async with httpx.AsyncClient() as client:99 response = await client.request("POST", server.url, data=hello_world())100 assert response.status_code == 200101102103@pytest.mark.usefixtures("async_environment")104async def test_raise_for_status(server):105 async with httpx.AsyncClient() as client:106 for status_code in (200, 400, 404, 500, 505):107 response = await client.request(108 "GET", server.url.copy_with(path=f"/status/{status_code}")109 )110111 if 400 <= status_code < 600:

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 112why, one step back
112 with pytest.raises(httpx.HTTPStatusError) as exc_info:

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

lines 113–115why, one step back
113 response.raise_for_status()
114 assert exc_info.value.response == response115 else:

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

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

Load SSL Context on init (#709)Tom Christiebefore adoption79a9748

lines 117–168why, one step back
117
118119@pytest.mark.usefixtures("async_environment")120async def test_options(server):121 async with httpx.AsyncClient() as client:122 response = await client.options(server.url)123 assert response.status_code == 200124 assert response.text == "Hello, world!"125126127@pytest.mark.usefixtures("async_environment")128async def test_head(server):129 async with httpx.AsyncClient() as client:130 response = await client.head(server.url)131 assert response.status_code == 200132 assert response.text == ""133134135@pytest.mark.usefixtures("async_environment")136async def test_put(server):137 async with httpx.AsyncClient() as client:138 response = await client.put(server.url, data=b"Hello, world!")139 assert response.status_code == 200140141142@pytest.mark.usefixtures("async_environment")143async def test_patch(server):144 async with httpx.AsyncClient() as client:145 response = await client.patch(server.url, data=b"Hello, world!")146 assert response.status_code == 200147148149@pytest.mark.usefixtures("async_environment")150async def test_delete(server):151 async with httpx.AsyncClient() as client:152 response = await client.delete(server.url)153 assert response.status_code == 200154 assert response.text == "Hello, world!"155156157@pytest.mark.usefixtures("async_environment")158async def test_100_continue(server):159 headers = {"Expect": "100-continue"}160 data = b"Echo request body"161162 async with httpx.AsyncClient() as client:163 response = await client.post(164 server.url.copy_with(path="/echo_body"), headers=headers, data=data165 )166167 assert response.status_code == 200168 assert response.content == data