Skip to content
Myr

test_async_client.py

This file

Path

tests/client/test_async_client.py

148 lines · 4.8 KiB · Python

Last changed by

Make HTTPError importable from the top-level (#421)Josep Cugatbefore adoptiona028256

History of this file

Take 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 back
1from datetime import timedelta
2

Add response.json()Tom Christiebefore adoption2116d44

lines 3–4why, one step back
3import pytest
4

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 5why, one step back
5import httpx

Add response.json()Tom Christiebefore adoption2116d44

lines 6–7why, one step back
6
7

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 8why, one step back
8async def test_get(server, backend):

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Add response.json()Tom Christiebefore adoption2116d44

lines 11–13why, one step back
11 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 back
14 assert response.http_version == "HTTP/1.1"

Add response.json()Tom Christiebefore adoption2116d44

line 15why, one step back
15 assert response.headers

Fixed response repr. (#101)updatatodaybefore adoptionf9e3ad4

line 16why, one step back
16 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 back
17 assert response.elapsed > timedelta(seconds=0)

Add response.json()Tom Christiebefore adoption2116d44

lines 18–19why, one step back
18
19

Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac

lines 20–33why, one step back
20async 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"3233

Add response.json()Tom Christiebefore adoption2116d44

line 34why, one step back
34@pytest.mark.asyncio

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 35–38why, one step back
35async 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 back
39 url = server.url

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 41–49why, one step back
41 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 back
50 url = server.url

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Add response.json()Tom Christiebefore adoption2116d44

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 56why, one step back
56async def test_post_json(server, backend):

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Add json interfacesTom Christiebefore adoption5a63d31

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

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 63–64why, one step back
63async 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 back
65 response = await client.request("GET", server.url, stream=True)

Add response.json()Tom Christiebefore adoption2116d44

lines 66–71why, one step back
66 assert response.status_code == 200
67 body = await response.read()68 assert body == b"Hello, world!"69 assert response.content == b"Hello, world!"7071

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 72–73why, one step back
72async 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 back
74 response = await client.request("GET", server.url, stream=True)

Add response.json()Tom Christiebefore adoption2116d44

line 75why, one step back
75 assert response.status_code == 200

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 76why, one step back
76 with pytest.raises(httpx.ResponseNotRead):

Add response.json()Tom Christiebefore adoption2116d44

lines 77–79why, one step back
77 response.content
7879

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 80why, one step back
80async def test_stream_request(server, backend):

Add response.json()Tom Christiebefore adoption2116d44

lines 81–84why, one step back
81 async def hello_world():
82 yield b"Hello, "83 yield b"world!"84

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

line 86why, one step back
86 response = await client.request("POST", server.url, data=hello_world())

Add response.json()Tom Christiebefore adoption2116d44

lines 87–89why, one step back
87 assert response.status_code == 200
8889

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 90–91why, one step back
90async 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 back
92 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 back
94 "GET", server.url.copy_with(path=f"/status/{status_code}")

Add response.json()Tom Christiebefore adoption2116d44

lines 95–97why, one step back
95 )
9697 if 400 <= status_code < 600:

Make HTTPError importable from the top-level (#421)Josep Cugatbefore adoptiona028256

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

Add response.json()Tom Christiebefore adoption2116d44

line 99why, one step back
99 response.raise_for_status()

Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f

line 100why, one step back
100 assert exc_info.value.response == response

Add response.json()Tom Christiebefore adoption2116d44

lines 101–104why, one step back
101 else:
102 assert response.raise_for_status() is None103104

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 105–106why, one step back
105async 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 back
107 response = await client.options(server.url)

Add response.json()Tom Christiebefore adoption2116d44

lines 108–111why, one step back
108 assert response.status_code == 200
109 assert response.text == "Hello, world!"110111

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 112–113why, one step back
112async 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 back
114 response = await client.head(server.url)

Add response.json()Tom Christiebefore adoption2116d44

lines 115–118why, one step back
115 assert response.status_code == 200
116 assert response.text == ""117118

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 119–120why, one step back
119async 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 back
121 response = await client.put(server.url, data=b"Hello, world!")

Add response.json()Tom Christiebefore adoption2116d44

lines 122–124why, one step back
122 assert response.status_code == 200
123124

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 125–126why, one step back
125async 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 back
127 response = await client.patch(server.url, data=b"Hello, world!")

Add response.json()Tom Christiebefore adoption2116d44

lines 128–130why, one step back
128 assert response.status_code == 200
129130

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

lines 131–132why, one step back
131async 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 back
133 response = await client.delete(server.url)

Add response.json()Tom Christiebefore adoption2116d44

lines 134–137why, one step back
134 assert response.status_code == 200
135 assert response.text == "Hello, world!"136137

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

line 138why, one step back
138async def test_100_continue(server, backend):

Add response.json()Tom Christiebefore adoption2116d44

lines 139–141why, one step back
139 headers = {"Expect": "100-continue"}
140 data = b"Echo request body"141

Parametrize tests with concurrency backend (#273)Florimond Mancabefore adoption3674058

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

Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280

lines 143–145why, one step back
143 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 back
146
147 assert response.status_code == 200148 assert response.content == data