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 fileTake it
The walk stopped after 200 commits: lines older than fe1f9ed point at it.
Add `.elapsed` onto Response objects tracking how long a request took. (#351)Roy Williamsbefore adoption05ac117
lines 1–3why, one step back1from datetime import timedelta2from time import sleep3Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 4–5why, one step back4import pytest5Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 6why, one step back6import httpxMinor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 7–8why, one step back78Drop sync (#544)Tom Christiebefore adoption206c537
lines 9–10why, one step back9@pytest.mark.asyncio10async def test_get(server):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 11why, one step back11 url = server.urlDrop sync (#544)Tom Christiebefore adoption206c537
lines 12–13why, one step back12 async with httpx.Client() as http:13 response = await http.get(url)Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
line 14why, one step back14 assert response.status_code == 200Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 15why, one step back15 assert response.url == urlMinor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 16–17why, one step back16 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 back18 assert response.http_version == "HTTP/1.1"Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
line 19why, one step back19 assert response.encoding == "iso-8859-1"Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 20why, one step back20 assert response.request.url == urlMinor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 21–23why, one step back21 assert response.headers22 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 back24 assert response.elapsed > timedelta(0)Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 25–26why, one step back2526Drop sync (#544)Tom Christiebefore adoption206c537
lines 27–28why, one step back27@pytest.mark.asyncio28async def test_build_request(server):Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 29–31why, one step back29 url = server.url.copy_with(path="/echo_headers")30 headers = {"Custom-header": "value"}31Drop sync (#544)Tom Christiebefore adoption206c537
lines 32–33why, one step back32 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 back34 request.headers.update(headers)Drop sync (#544)Tom Christiebefore adoption206c537
line 35why, one step back35 response = await client.send(request)Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac
lines 36–42why, one step back3637 assert response.status_code == 20038 assert response.url == url3940 assert response.json()["Custom-header"] == "value"4142Drop sync (#544)Tom Christiebefore adoption206c537
lines 43–46why, one step back43@pytest.mark.asyncio44async def test_post(server):45 async with httpx.Client() as client:46 response = await client.post(server.url, data=b"Hello, world!")Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 47–50why, one step back47 assert response.status_code == 20048 assert response.reason_phrase == "OK"4950Drop sync (#544)Tom Christiebefore adoption206c537
lines 51–54why, one step back51@pytest.mark.asyncio52async def test_post_json(server):53 async with httpx.Client() as client:54 response = await client.post(server.url, json={"text": "Hello, world!"})Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 55–58why, one step back55 assert response.status_code == 20056 assert response.reason_phrase == "OK"5758Drop sync (#544)Tom Christiebefore adoption206c537
lines 59–62why, one step back59@pytest.mark.asyncio60async def test_stream_response(server):61 async with httpx.Client() as client:62 response = await client.get(server.url, stream=True)Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
line 63why, one step back63 assert response.status_code == 200Drop sync (#544)Tom Christiebefore adoption206c537
line 64why, one step back64 content = await response.read()Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 65–67why, one step back65 assert content == b"Hello, world!"6667Drop sync (#544)Tom Christiebefore adoption206c537
lines 68–71why, one step back68@pytest.mark.asyncio69async def test_stream_iterator(server):70 async with httpx.Client() as client:71 response = await client.get(server.url, stream=True)Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 72–73why, one step back72 assert response.status_code == 20073 body = b""Drop sync (#544)Tom Christiebefore adoption206c537
line 74why, one step back74 async for chunk in response.stream():Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 75–78why, one step back75 body += chunk76 assert body == b"Hello, world!"7778Drop sync (#544)Tom Christiebefore adoption206c537
lines 79–82why, one step back79@pytest.mark.asyncio80async def test_raw_iterator(server):81 async with httpx.Client() as client:82 response = await client.get(server.url, stream=True)Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 83–84why, one step back83 assert response.status_code == 20084 body = b""Drop sync (#544)Tom Christiebefore adoption206c537
line 85why, one step back85 async for chunk in response.raw():Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 86–87why, one step back86 body += chunk87 assert body == b"Hello, world!"Drop sync (#544)Tom Christiebefore adoption206c537
line 88why, one step back88 await response.close()Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 89–90why, one step back8990Drop sync (#544)Tom Christiebefore adoption206c537
lines 91–93why, one step back91@pytest.mark.asyncio92async def test_raise_for_status(server):93 async with httpx.Client() as client:Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
line 94why, one step back94 for status_code in (200, 400, 404, 500, 505):Drop sync (#544)Tom Christiebefore adoption206c537
line 95why, one step back95 response = await client.request(Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 96why, one step back96 "GET", server.url.copy_with(path="/status/{}".format(status_code))Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 97–98why, one step back97 )98 if 400 <= status_code < 600:Make HTTPError importable from the top-level (#421)Josep Cugatbefore adoptiona028256
line 99why, one step back99 with pytest.raises(httpx.HTTPError) as exc_info:Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
line 100why, one step back100 response.raise_for_status()Added base class HTTPError with request/response attribute (#162)halbowbefore adoption92fbe5f
line 101why, one step back101 assert exc_info.value.response == responseMinor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 102–105why, one step back102 else:103 assert response.raise_for_status() is None104105Drop sync (#544)Tom Christiebefore adoption206c537
lines 106–109why, one step back106@pytest.mark.asyncio107async def test_options(server):108 async with httpx.Client() as client:109 response = await client.options(server.url)Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 110–113why, one step back110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112113Drop sync (#544)Tom Christiebefore adoption206c537
lines 114–117why, one step back114@pytest.mark.asyncio115async def test_head(server):116 async with httpx.Client() as client:117 response = await client.head(server.url)Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 118–121why, one step back118 assert response.status_code == 200119 assert response.reason_phrase == "OK"120121Drop sync (#544)Tom Christiebefore adoption206c537
lines 122–125why, one step back122@pytest.mark.asyncio123async def test_put(server):124 async with httpx.Client() as client:125 response = await client.put(server.url, data=b"Hello, world!")Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 126–129why, one step back126 assert response.status_code == 200127 assert response.reason_phrase == "OK"128129Drop sync (#544)Tom Christiebefore adoption206c537
lines 130–133why, one step back130@pytest.mark.asyncio131async def test_patch(server):132 async with httpx.Client() as client:133 response = await client.patch(server.url, data=b"Hello, world!")Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 134–137why, one step back134 assert response.status_code == 200135 assert response.reason_phrase == "OK"136137Drop sync (#544)Tom Christiebefore adoption206c537
lines 138–141why, one step back138@pytest.mark.asyncio139async def test_delete(server):140 async with httpx.Client() as client:141 response = await client.delete(server.url)Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 142–145why, one step back142 assert response.status_code == 200143 assert response.reason_phrase == "OK"144145Drop sync (#544)Tom Christiebefore adoption206c537
lines 146–147why, one step back146@pytest.mark.asyncio147async def test_base_url(server):Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 148why, one step back148 base_url = server.urlDrop sync (#544)Tom Christiebefore adoption206c537
lines 149–150why, one step back149 async with httpx.Client(base_url=base_url) as client:150 response = await client.get("/")Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
line 151why, one step back151 assert response.status_code == 200Add server.url property in tests (#300)Bakhtiyor Ruzievbefore adoptione34d280
line 152why, one step back152 assert response.url == base_urlMove HSTS preload checking to client (#184)Seth Michael Larsonbefore adoption9142a89
lines 153–154why, one step back153154Drop sync (#544)Tom Christiebefore adoption206c537
lines 155–156why, one step back155@pytest.mark.asyncio156async def test_uds(uds_server):Add support for unix domain sockets (#511)Jonas Lundbergbefore adoption7a96a2c
lines 157–159why, one step back157 url = uds_server.url158 uds = uds_server.config.uds159 assert uds is not NoneDrop sync (#544)Tom Christiebefore adoption206c537
lines 160–161why, one step back160 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 back162 assert response.status_code == 200163 assert response.text == "Hello, world!"164 assert response.encoding == "iso-8859-1"165166Move HSTS preload checking to client (#184)Seth Michael Larsonbefore adoption9142a89
lines 167–172why, one step back167def 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_sslEnforce that sync client uses asyncio-based backend (#232)Florimond Mancabefore adoption9c7d23c
lines 173–174why, one step back173174Drop sync (#544)Tom Christiebefore adoption206c537
lines 175–178why, one step back175@pytest.mark.asyncio176async 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 back179 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 back180181Drop sync (#544)Tom Christiebefore adoption206c537
lines 182–185why, one step back182@pytest.mark.asyncio183async 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 back186 server.url.copy_with(path="/slow_response/100"), stream=True187 )188 sleep(0.2)Drop sync (#544)Tom Christiebefore adoption206c537
line 189why, one step back189 await response.read()Fix flaky Response.elapsed tests (#446)Florimond Mancabefore adoption24346e2
line 190why, one step back190 assert response.elapsed.total_seconds() == pytest.approx(0.1, rel=0.2)