Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

194 lines · 5.7 KiB · Python

Last changed by

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

History of this file

Take it

The walk stopped after 200 commits: lines older than caba785 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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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)

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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"

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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)

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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!")

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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!"})

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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–61why, one step back
59@pytest.mark.asyncio
60async def test_stream_response(server):61 async with httpx.Client() as client:

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 62–63why, one step back
62 async with client.stream("GET", server.url) as response:
63 content = await response.read()

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 64–67why, one step back
64 assert response.status_code == 200
65 assert content == b"Hello, world!"6667

Drop sync (#544)Tom Christiebefore adoption206c537

lines 68–69why, one step back
68@pytest.mark.asyncio
69async def test_stream_iterator(server):

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 70–71why, one step back
70 body = b""
71

Drop sync (#544)Tom Christiebefore adoption206c537

line 72why, one step back
72 async with httpx.Client() as client:

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

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

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

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

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 75–76why, one step back
75 body += chunk
76

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 77–80why, one step back
77 assert response.status_code == 200
78 assert body == b"Hello, world!"7980

Drop sync (#544)Tom Christiebefore adoption206c537

lines 81–82why, one step back
81@pytest.mark.asyncio
82async def test_raw_iterator(server):

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 83–84why, one step back
83 body = b""
84

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

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

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

line 87why, one step back
87 async for chunk in response.aiter_raw():

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 88–89why, one step back
88 body += chunk
89

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 90–93why, one step back
90 assert response.status_code == 200
91 assert body == b"Hello, world!"9293

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 100–101why, one step back
100 )
101 if 400 <= status_code < 600:

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

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 103–108why, one step back
103 response.raise_for_status()
104 assert exc_info.value.response == response105 else:106 assert response.raise_for_status() is None107108

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 113–116why, one step back
113 assert response.status_code == 200
114 assert response.reason_phrase == "OK"115116

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 129–132why, one step back
129 assert response.status_code == 200
130 assert response.reason_phrase == "OK"131132

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 137–140why, one step back
137 assert response.status_code == 200
138 assert response.reason_phrase == "OK"139140

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 145–148why, one step back
145 assert response.status_code == 200
146 assert response.reason_phrase == "OK"147148

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

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

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

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

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 156–157why, one step back
156
157

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 160–162why, one step back
160 url = uds_server.url
161 uds = uds_server.config.uds162 assert uds is not None

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 165–169why, one step back
165 assert response.status_code == 200
166 assert response.text == "Hello, world!"167 assert response.encoding == "iso-8859-1"168169

Release 0.7.1 (#234)Seth Michael Larsonbefore adoptioncaba785

lines 170–175why, one step back
170def test_merge_url():
171 client = httpx.Client(base_url="https://www.paypal.com/")172 url = client.merge_url("http://www.paypal.com")173174 assert url.scheme == "https"175 assert url.is_ssl

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

lines 176–177why, one step back
176
177

Drop sync (#544)Tom Christiebefore adoption206c537

lines 178–179why, one step back
178@pytest.mark.asyncio
179async def test_elapsed_delay(server):

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

line 180why, one step back
180 url = server.url.copy_with(path="/slow_response/100")

Drop sync (#544)Tom Christiebefore adoption206c537

line 181why, one step back
181 async with httpx.Client() as client:

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

line 182why, one step back
182 response = await client.get(url)

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

line 183why, one step back
183 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 184–185why, one step back
184
185

Drop sync (#544)Tom Christiebefore adoption206c537

lines 186–187why, one step back
186@pytest.mark.asyncio
187async def test_elapsed_delay_ignores_read_time(server):

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

line 188why, one step back
188 url = server.url.copy_with(path="/slow_response/100")

Drop sync (#544)Tom Christiebefore adoption206c537

line 189why, one step back
189 async with httpx.Client() as client:

Add Client.stream() method. (#600)Tom Christiebefore adoption8d8ea8b

lines 190–193why, one step back
190 async with client.stream("GET", url) as response:
191 sleep(0.2)192 await response.read()193

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

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