Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

190 lines · 5.6 KiB · Python

Last changed by

Drop sync (#544)Tom Christiebefore adoption206c537

History of this file

Take it

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

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

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

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 4–8why, one step back
4import pytest
56import httpx78

Drop sync (#544)Tom Christiebefore adoption206c537

lines 9–10why, one step back
9@pytest.mark.asyncio
10async def test_get(server):

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

lines 12–13why, one step back
12 async with httpx.Client() as http:
13 response = await http.get(url)

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

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

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

line 15why, one step back
15 assert response.url == url

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 16–17why, one step back
16 assert response.content == b"Hello, world!"
17 assert response.text == "Hello, world!"

Response.protocol -> Response.http_version (#250)Tom Christiebefore adoptionc1ec1c3

line 18why, one step back
18 assert response.http_version == "HTTP/1.1"

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

line 19why, one step back
19 assert response.encoding == "iso-8859-1"

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

line 20why, one step back
20 assert response.request.url == url

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 21–23why, one step back
21 assert response.headers
22 assert response.is_redirect is False23 assert repr(response) == "<Response [200 OK]>"

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

line 24why, one step back
24 assert response.elapsed > timedelta(0)

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 25–26why, one step back
25
26

Drop sync (#544)Tom Christiebefore adoption206c537

lines 27–28why, one step back
27@pytest.mark.asyncio
28async def test_build_request(server):

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

lines 32–33why, one step back
32 async with httpx.Client() as client:
33 request = client.build_request("GET", url)

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

line 34why, one step back
34 request.headers.update(headers)

Drop sync (#544)Tom Christiebefore adoption206c537

line 35why, one step back
35 response = await client.send(request)

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

lines 36–42why, one step back
36
37 assert response.status_code == 20038 assert response.url == url3940 assert response.json()["Custom-header"] == "value"4142

Drop sync (#544)Tom Christiebefore adoption206c537

lines 43–46why, one step back
43@pytest.mark.asyncio
44async def test_post(server):45 async with httpx.Client() as client:46 response = await client.post(server.url, data=b"Hello, world!")

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 47–50why, one step back
47 assert response.status_code == 200
48 assert response.reason_phrase == "OK"4950

Drop sync (#544)Tom Christiebefore adoption206c537

lines 51–54why, one step back
51@pytest.mark.asyncio
52async def test_post_json(server):53 async with httpx.Client() as client:54 response = await client.post(server.url, json={"text": "Hello, world!"})

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 55–58why, one step back
55 assert response.status_code == 200
56 assert response.reason_phrase == "OK"5758

Drop sync (#544)Tom Christiebefore adoption206c537

lines 59–62why, one step back
59@pytest.mark.asyncio
60async def test_stream_response(server):61 async with httpx.Client() as client:62 response = await client.get(server.url, stream=True)

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 64why, one step back
64 content = await response.read()

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 65–67why, one step back
65 assert content == b"Hello, world!"
6667

Drop sync (#544)Tom Christiebefore adoption206c537

lines 68–71why, one step back
68@pytest.mark.asyncio
69async def test_stream_iterator(server):70 async with httpx.Client() as client:71 response = await client.get(server.url, stream=True)

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 72–73why, one step back
72 assert response.status_code == 200
73 body = b""

Drop sync (#544)Tom Christiebefore adoption206c537

line 74why, one step back
74 async for chunk in response.stream():

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 75–78why, one step back
75 body += chunk
76 assert body == b"Hello, world!"7778

Drop sync (#544)Tom Christiebefore adoption206c537

lines 79–82why, one step back
79@pytest.mark.asyncio
80async def test_raw_iterator(server):81 async with httpx.Client() as client:82 response = await client.get(server.url, stream=True)

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 83–84why, one step back
83 assert response.status_code == 200
84 body = b""

Drop sync (#544)Tom Christiebefore adoption206c537

line 85why, one step back
85 async for chunk in response.raw():

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 86–87why, one step back
86 body += chunk
87 assert body == b"Hello, world!"

Drop sync (#544)Tom Christiebefore adoption206c537

line 88why, one step back
88 await response.close()

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 89–90why, one step back
89
90

Drop sync (#544)Tom Christiebefore adoption206c537

lines 91–93why, one step back
91@pytest.mark.asyncio
92async def test_raise_for_status(server):93 async with httpx.Client() as client:

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

line 94why, one step back
94 for status_code in (200, 400, 404, 500, 505):

Drop sync (#544)Tom Christiebefore adoption206c537

line 95why, one step back
95 response = await client.request(

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

line 96why, one step back
96 "GET", server.url.copy_with(path="/status/{}".format(status_code))

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

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

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

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

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 100–105why, one step back
100 response.raise_for_status()
101 assert exc_info.value.response == response102 else:103 assert response.raise_for_status() is None104105

Drop sync (#544)Tom Christiebefore adoption206c537

lines 106–109why, one step back
106@pytest.mark.asyncio
107async def test_options(server):108 async with httpx.Client() as client:109 response = await client.options(server.url)

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 110–113why, one step back
110 assert response.status_code == 200
111 assert response.reason_phrase == "OK"112113

Drop sync (#544)Tom Christiebefore adoption206c537

lines 114–117why, one step back
114@pytest.mark.asyncio
115async def test_head(server):116 async with httpx.Client() as client:117 response = await client.head(server.url)

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 118–121why, one step back
118 assert response.status_code == 200
119 assert response.reason_phrase == "OK"120121

Drop sync (#544)Tom Christiebefore adoption206c537

lines 122–125why, one step back
122@pytest.mark.asyncio
123async def test_put(server):124 async with httpx.Client() as client:125 response = await client.put(server.url, data=b"Hello, world!")

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 126–129why, one step back
126 assert response.status_code == 200
127 assert response.reason_phrase == "OK"128129

Drop sync (#544)Tom Christiebefore adoption206c537

lines 130–133why, one step back
130@pytest.mark.asyncio
131async def test_patch(server):132 async with httpx.Client() as client:133 response = await client.patch(server.url, data=b"Hello, world!")

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 134–137why, one step back
134 assert response.status_code == 200
135 assert response.reason_phrase == "OK"136137

Drop sync (#544)Tom Christiebefore adoption206c537

lines 138–141why, one step back
138@pytest.mark.asyncio
139async def test_delete(server):140 async with httpx.Client() as client:141 response = await client.delete(server.url)

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 142–145why, one step back
142 assert response.status_code == 200
143 assert response.reason_phrase == "OK"144145

Drop sync (#544)Tom Christiebefore adoption206c537

lines 146–147why, one step back
146@pytest.mark.asyncio
147async def test_base_url(server):

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

line 148why, one step back
148 base_url = server.url

Drop sync (#544)Tom Christiebefore adoption206c537

lines 149–150why, one step back
149 async with httpx.Client(base_url=base_url) as client:
150 response = await client.get("/")

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

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

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

line 152why, one step back
152 assert response.url == base_url

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 153–154why, one step back
153
154

Drop sync (#544)Tom Christiebefore adoption206c537

lines 155–156why, one step back
155@pytest.mark.asyncio
156async def test_uds(uds_server):

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 157–159why, one step back
157 url = uds_server.url
158 uds = uds_server.config.uds159 assert uds is not None

Drop sync (#544)Tom Christiebefore adoption206c537

lines 160–161why, one step back
160 async with httpx.Client(uds=uds) as client:
161 response = await client.get(url)

Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c

lines 162–166why, one step back
162 assert response.status_code == 200
163 assert response.text == "Hello, world!"164 assert response.encoding == "iso-8859-1"165166

Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf

lines 167–172why, one step back
167def test_merge_url():
168 client = httpx.Client(base_url="https://www.paypal.com/")169 url = client.merge_url("http://www.paypal.com")170171 assert url.scheme == "https"172 assert url.is_ssl

Enforce that sync client uses asyncio-based backend (#232)Florimond Mancabefore adoption9c7d23c

lines 173–174why, one step back
173
174

Drop sync (#544)Tom Christiebefore adoption206c537

lines 175–178why, one step back
175@pytest.mark.asyncio
176async def test_elapsed_delay(server):177 async with httpx.Client() as client:178 response = await client.get(server.url.copy_with(path="/slow_response/100"))

Fix flaky Response.elapsed tests (#446)Florimond Mancabefore adoption24346e2

line 179why, one step back
179 assert response.elapsed.total_seconds() == pytest.approx(0.1, rel=0.2)

Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117

lines 180–181why, one step back
180
181

Drop sync (#544)Tom Christiebefore adoption206c537

lines 182–185why, one step back
182@pytest.mark.asyncio
183async def test_elapsed_delay_ignores_read_time(server):184 async with httpx.Client() as client:185 response = await client.get(

Fix flaky Response.elapsed tests (#446)Florimond Mancabefore adoption24346e2

lines 186–188why, one step back
186 server.url.copy_with(path="/slow_response/100"), stream=True
187 )188 sleep(0.2)

Drop sync (#544)Tom Christiebefore adoption206c537

line 189why, one step back
189 await response.read()

Fix flaky Response.elapsed tests (#446)Florimond Mancabefore adoption24346e2

line 190why, one step back
190 assert response.elapsed.total_seconds() == pytest.approx(0.1, rel=0.2)