Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

164 lines · 4.6 KiB · Python

Last changed by

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

History of this file

Take it

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

Tweak docstringTom Christiebefore adoptiona738327

lines 1–7why, one step back
1from datetime import timedelta
23import pytest45import httpx67

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 12–24why, one step back
12 assert response.status_code == 200
13 assert response.url == url14 assert response.content == b"Hello, world!"15 assert response.text == "Hello, world!"16 assert response.http_version == "HTTP/1.1"17 assert response.encoding == "iso-8859-1"18 assert response.request.url == url19 assert response.headers20 assert response.is_redirect is False21 assert repr(response) == "<Response [200 OK]>"22 assert response.elapsed > timedelta(0)2324

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 25why, one step back
25def test_build_request(server):

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 30–31why, one step back
30 request = client.build_request("GET", url)
31 request.headers.update(headers)

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 33–39why, one step back
33
34 assert response.status_code == 20035 assert response.url == url3637 assert response.json()["Custom-header"] == "value"3839

Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a

lines 40–55why, one step back
40def test_build_post_request(server):
41 url = server.url.copy_with(path="/echo_headers")42 headers = {"Custom-header": "value"}4344 with httpx.Client() as client:45 request = client.build_request("POST", url)46 request.headers.update(headers)47 response = client.send(request)4849 assert response.status_code == 20050 assert response.url == url5152 assert response.json()["Content-length"] == "0"53 assert response.json()["Custom-header"] == "value"5455

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 56–58why, one step back
56def test_post(server):
57 with httpx.Client() as client:58 response = client.post(server.url, data=b"Hello, world!")

Tweak docstringTom Christiebefore adoptiona738327

lines 59–62why, one step back
59 assert response.status_code == 200
60 assert response.reason_phrase == "OK"6162

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 63–65why, one step back
63def test_post_json(server):
64 with httpx.Client() as client:65 response = client.post(server.url, json={"text": "Hello, world!"})

Tweak docstringTom Christiebefore adoptiona738327

lines 66–69why, one step back
66 assert response.status_code == 200
67 assert response.reason_phrase == "OK"6869

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 70–73why, one step back
70def test_stream_response(server):
71 with httpx.Client() as client:72 with client.stream("GET", server.url) as response:73 content = response.read()

Tweak docstringTom Christiebefore adoptiona738327

lines 74–77why, one step back
74 assert response.status_code == 200
75 assert content == b"Hello, world!"7677

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 78why, one step back
78def test_stream_iterator(server):

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

lines 79–80why, one step back
79 body = b""
80

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 81–83why, one step back
81 with httpx.Client() as client:
82 with client.stream("GET", server.url) as response:83 for chunk in response.iter_bytes():

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

lines 84–85why, one step back
84 body += chunk
85

Tweak docstringTom Christiebefore adoptiona738327

lines 86–89why, one step back
86 assert response.status_code == 200
87 assert body == b"Hello, world!"8889

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 90why, one step back
90def test_raw_iterator(server):

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

lines 91–92why, one step back
91 body = b""
92

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 93–95why, one step back
93 with httpx.Client() as client:
94 with client.stream("GET", server.url) as response:95 for chunk in response.iter_raw():

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

lines 96–97why, one step back
96 body += chunk
97

Tweak docstringTom Christiebefore adoptiona738327

lines 98–101why, one step back
98 assert response.status_code == 200
99 assert body == b"Hello, world!"100101

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 102–103why, one step back
102def test_raise_for_status(server):
103 with httpx.Client() as client:

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 106–112why, one step back
106 "GET", server.url.copy_with(path="/status/{}".format(status_code))
107 )108 if 400 <= status_code < 600:109 with pytest.raises(httpx.HTTPError) as exc_info:110 response.raise_for_status()111 assert exc_info.value.response == response112 else:

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

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

Tweak docstringTom Christiebefore adoptiona738327

lines 114–115why, one step back
114
115

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 116–118why, one step back
116def test_options(server):
117 with httpx.Client() as client:118 response = client.options(server.url)

Tweak docstringTom Christiebefore adoptiona738327

lines 119–122why, one step back
119 assert response.status_code == 200
120 assert response.reason_phrase == "OK"121122

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 123–125why, one step back
123def test_head(server):
124 with httpx.Client() as client:125 response = client.head(server.url)

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 130–132why, one step back
130def test_put(server):
131 with httpx.Client() as client:132 response = client.put(server.url, data=b"Hello, world!")

Tweak docstringTom Christiebefore adoptiona738327

lines 133–136why, one step back
133 assert response.status_code == 200
134 assert response.reason_phrase == "OK"135136

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 137–139why, one step back
137def test_patch(server):
138 with httpx.Client() as client:139 response = client.patch(server.url, data=b"Hello, world!")

Tweak docstringTom Christiebefore adoptiona738327

lines 140–143why, one step back
140 assert response.status_code == 200
141 assert response.reason_phrase == "OK"142143

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

lines 144–146why, one step back
144def test_delete(server):
145 with httpx.Client() as client:146 response = client.delete(server.url)

Tweak docstringTom Christiebefore adoptiona738327

lines 147–150why, one step back
147 assert response.status_code == 200
148 assert response.reason_phrase == "OK"149150

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 151why, one step back
151def test_base_url(server):

Tweak docstringTom Christiebefore adoptiona738327

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

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

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

Tweak docstringTom Christiebefore adoptiona738327

lines 155–159why, one step back
155 assert response.status_code == 200
156 assert response.url == base_url157158159def test_merge_url():

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 160why, one step back
160 client = httpx.Client(base_url="https://www.paypal.com/")

Tweak docstringTom Christiebefore adoptiona738327

lines 161–164why, one step back
161 url = client.merge_url("http://www.paypal.com")
162163 assert url.scheme == "https"164 assert url.is_ssl