Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

182 lines · 5.3 KiB · Python

Last changed by

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

History of this file

Take it

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 3–7why, one step back
3import pytest
45import httpx67

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 13–22why, one step back
13 assert response.status_code == 200
14 assert response.url == url15 assert response.content == b"Hello, world!"16 assert response.text == "Hello, world!"17 assert response.http_version == "HTTP/1.1"18 assert response.encoding == "iso-8859-1"19 assert response.request.url == url20 assert response.headers21 assert response.is_redirect is False22 assert repr(response) == "<Response [200 OK]>"

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

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 24–25why, one step back
24
25

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 35–41why, one step back
35
36 assert response.status_code == 20037 assert response.url == url3839 assert response.json()["Custom-header"] == "value"4041

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 46–49why, one step back
46 assert response.status_code == 200
47 assert response.reason_phrase == "OK"4849

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 54–57why, one step back
54 assert response.status_code == 200
55 assert response.reason_phrase == "OK"5657

Drop sync (#544)Tom Christiebefore adoption206c537

lines 58–60why, one step back
58@pytest.mark.asyncio
59async def test_stream_response(server):60 async with httpx.Client() as client:

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

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

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

line 62why, one step back
62 content = await response.aread()

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 63–66why, one step back
63 assert response.status_code == 200
64 assert content == b"Hello, world!"6566

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

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

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

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

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 76–79why, one step back
76 assert response.status_code == 200
77 assert body == b"Hello, world!"7879

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

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

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

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

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 89–92why, one step back
89 assert response.status_code == 200
90 assert body == b"Hello, world!"9192

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 98–100why, one step back
98 "GET", server.url.copy_with(path="/status/{}".format(status_code))
99 )100 if 400 <= status_code < 600:

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

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 102–107why, one step back
102 response.raise_for_status()
103 assert exc_info.value.response == response104 else:105 assert response.raise_for_status() is None106107

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 112–115why, one step back
112 assert response.status_code == 200
113 assert response.reason_phrase == "OK"114115

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 120–123why, one step back
120 assert response.status_code == 200
121 assert response.reason_phrase == "OK"122123

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 128–131why, one step back
128 assert response.status_code == 200
129 assert response.reason_phrase == "OK"130131

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 136–139why, one step back
136 assert response.status_code == 200
137 assert response.reason_phrase == "OK"138139

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 144–147why, one step back
144 assert response.status_code == 200
145 assert response.reason_phrase == "OK"146147

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 153–156why, one step back
153 assert response.status_code == 200
154 assert response.url == base_url155156

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 169–176why, one step back
169def test_merge_url():
170 client = httpx.Client(base_url="https://www.paypal.com/")171 url = client.merge_url("http://www.paypal.com")172173 assert url.scheme == "https"174 assert url.is_ssl175176

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

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

Trivial-case test for response.elapsedTom Christiebefore adoption02d16bf

line 182why, one step back
182 assert response.elapsed.total_seconds() > 0.0