test_client.py
This file
Path
tests/client/test_client.py
193 lines · 5.5 KiB · Python
Last changed by
Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
History of this fileTake it
The walk stopped after 200 commits: lines older than 869714f point at it.
Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 1–7why, one step back1from datetime import timedelta23import pytest45import httpx67Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 8why, one step back8def test_get(server):Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
line 9why, one step back9 url = server.urlReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 10–11why, one step back10 with httpx.Client() as http:11 response = http.get(url)Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 12–24why, one step back12 assert response.status_code == 20013 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)2324Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
lines 25–33why, one step back25@pytest.mark.parametrize(26 "url",27 [28 pytest.param("invalid://example.org", id="scheme-not-http(s)"),29 pytest.param("://example.org", id="no-scheme"),30 pytest.param("http://", id="no-host"),31 ],32)33def test_get_invalid_url(server, url):Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351
lines 34–35why, one step back34 with httpx.Client() as client:35 with pytest.raises(httpx.InvalidURL):Parameterize invalid URL tests (#1079)Tom Christiebefore adoption696c1ef
line 36why, one step back36 client.get(url)Drop `URL(allow_relative=bool)` (#1073)Tom Christiebefore adoption7e6e351
lines 37–38why, one step back3738Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 39why, one step back39def test_build_request(server):Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 40–42why, one step back40 url = server.url.copy_with(path="/echo_headers")41 headers = {"Custom-header": "value"}42Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 43why, one step back43 with httpx.Client() as client:Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 44–45why, one step back44 request = client.build_request("GET", url)45 request.headers.update(headers)Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 46why, one step back46 response = client.send(request)Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 47–53why, one step back4748 assert response.status_code == 20049 assert response.url == url5051 assert response.json()["Custom-header"] == "value"5253Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a
lines 54–69why, one step back54def test_build_post_request(server):55 url = server.url.copy_with(path="/echo_headers")56 headers = {"Custom-header": "value"}5758 with httpx.Client() as client:59 request = client.build_request("POST", url)60 request.headers.update(headers)61 response = client.send(request)6263 assert response.status_code == 20064 assert response.url == url6566 assert response.json()["Content-length"] == "0"67 assert response.json()["Custom-header"] == "value"6869Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 70–72why, one step back70def test_post(server):71 with httpx.Client() as client:72 response = client.post(server.url, data=b"Hello, world!")Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 73–76why, one step back73 assert response.status_code == 20074 assert response.reason_phrase == "OK"7576Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 77–79why, one step back77def test_post_json(server):78 with httpx.Client() as client:79 response = client.post(server.url, json={"text": "Hello, world!"})Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 80–83why, one step back80 assert response.status_code == 20081 assert response.reason_phrase == "OK"8283Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 84–87why, one step back84def test_stream_response(server):85 with httpx.Client() as client:86 with client.stream("GET", server.url) as response:87 content = response.read()Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 88–91why, one step back88 assert response.status_code == 20089 assert content == b"Hello, world!"9091Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 92why, one step back92def test_stream_iterator(server):Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 93–94why, one step back93 body = b""94Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 95–97why, one step back95 with httpx.Client() as client:96 with client.stream("GET", server.url) as response:97 for chunk in response.iter_bytes():Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 98–103why, one step back98 body += chunk99100 assert response.status_code == 200101 assert body == b"Hello, world!"102103Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 104why, one step back104def test_raw_iterator(server):Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 105–106why, one step back105 body = b""106Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 107–109why, one step back107 with httpx.Client() as client:108 with client.stream("GET", server.url) as response:109 for chunk in response.iter_raw():Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 110–115why, one step back110 body += chunk111112 assert response.status_code == 200113 assert body == b"Hello, world!"114115Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 116–117why, one step back116def test_raise_for_status(server):117 with httpx.Client() as client:Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
line 118why, one step back118 for status_code in (200, 400, 404, 500, 505):Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 119why, one step back119 response = client.request(Increased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 120why, one step back120 "GET", server.url.copy_with(path=f"/status/{status_code}")Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 121–122why, one step back121 )122 if 400 <= status_code < 600:Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc
line 123why, one step back123 with pytest.raises(httpx.HTTPStatusError) as exc_info:Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 124–125why, one step back124 response.raise_for_status()125 assert exc_info.value.response == responseIncreased test coverage & cleanup (#1003)Josep Cugatbefore adoption8c84210
line 126why, one step back126 assert exc_info.value.request.url.path == f"/status/{status_code}"Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
line 127why, one step back127 else:Fixed mypy errors in test_async_client.py and test_client.py (#985)Hasan Ramezanibefore adoption21d7e16
line 128why, one step back128 assert response.raise_for_status() is None # type: ignoreDrop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 129–130why, one step back129130Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 131–133why, one step back131def test_options(server):132 with httpx.Client() as client:133 response = client.options(server.url)Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 134–137why, one step back134 assert response.status_code == 200135 assert response.reason_phrase == "OK"136137Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 138–140why, one step back138def test_head(server):139 with httpx.Client() as client:140 response = client.head(server.url)Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 141–144why, one step back141 assert response.status_code == 200142 assert response.reason_phrase == "OK"143144Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 145–147why, one step back145def test_put(server):146 with httpx.Client() as client:147 response = client.put(server.url, data=b"Hello, world!")Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 148–151why, one step back148 assert response.status_code == 200149 assert response.reason_phrase == "OK"150151Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 152–154why, one step back152def test_patch(server):153 with httpx.Client() as client:154 response = client.patch(server.url, data=b"Hello, world!")Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 155–158why, one step back155 assert response.status_code == 200156 assert response.reason_phrase == "OK"157158Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 159–161why, one step back159def test_delete(server):160 with httpx.Client() as client:161 response = client.delete(server.url)Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 162–165why, one step back162 assert response.status_code == 200163 assert response.reason_phrase == "OK"164165Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 166why, one step back166def test_base_url(server):Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
line 167why, one step back167 base_url = server.urlReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 168–169why, one step back168 with httpx.Client(base_url=base_url) as client:169 response = client.get("/")Drop unused methods from BaseEvent (#626)Tom Christiebefore adoption869714f
lines 170–174why, one step back170 assert response.status_code == 200171 assert response.url == base_url172173174def test_merge_url():Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 175why, one step back175 client = httpx.Client(base_url="https://www.paypal.com/")Tighten public client methods (#997)Tom Christiebefore adoptione107e0f
lines 176–178why, one step back176 request = client.build_request("GET", "http://www.paypal.com")177 assert request.url.scheme == "https"178 assert request.url.is_sslSkip HSTS preloading on single-label domains (#1074)François Voronbefore adoption0641606
lines 179–193why, one step back179180181@pytest.mark.parametrize(182 "url,scheme,is_ssl",183 [184 ("http://www.paypal.com", "https", True),185 ("http://app", "http", False),186 ("http://192.168.1.42", "http", False),187 ],188)189def test_merge_url_hsts(url: str, scheme: str, is_ssl: bool):190 client = httpx.Client()191 request = client.build_request("GET", url)192 assert request.url.scheme == scheme193 assert request.url.is_ssl == is_ssl