test_api.py
This file
Path
tests/test_api.py
92 lines · 2.2 KiB · Python
Last changed by
Force HTTP/1.1 on short-lived connections (#284)Seth Michael Larsonbefore adoptione7aa6d6
History of this fileTake it
Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 1–5why, one step back1import asyncio2import functools34import pytest5Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 6why, one step back6import httpxAdd top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 7–10why, one step back789def threadpool(func):10 """Fix typos in docstringsSaugat Pachhaibefore adoption3451028
line 11why, one step back11 Our sync tests should run in separate thread to the uvicorn server.Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 12–27why, one step back12 """1314 @functools.wraps(func)15 async def wrapped(*args, **kwargs):16 nonlocal func1718 loop = asyncio.get_event_loop()19 if kwargs:20 func = functools.partial(func, **kwargs)21 await loop.run_in_executor(None, func, *args)2223 return pytest.mark.asyncio(wrapped)242526@threadpool27def test_get(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 28why, one step back28 response = httpx.get("http://127.0.0.1:8000/")Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 29–31why, one step back29 assert response.status_code == 20030 assert response.reason_phrase == "OK"31 assert response.text == "Hello, world!"Force HTTP/1.1 on short-lived connections (#284)Seth Michael Larsonbefore adoptione7aa6d6
line 32why, one step back32 assert response.http_version == "HTTP/1.1"Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 33–36why, one step back333435@threadpool36def test_post(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 37why, one step back37 response = httpx.post("http://127.0.0.1:8000/", data=b"Hello, world!")Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 38–42why, one step back38 assert response.status_code == 20039 assert response.reason_phrase == "OK"404142@threadpoolSync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 43–48why, one step back43def test_post_byte_iterator(server):44 def data():45 yield b"Hello"46 yield b", "47 yield b"world!"48Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 49why, one step back49 response = httpx.post("http://127.0.0.1:8000/", data=data())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 50–54why, one step back50 assert response.status_code == 20051 assert response.reason_phrase == "OK"525354@threadpoolAdd top-level API (#75)Tom Christiebefore adoptionfc627f3
line 55why, one step back55def test_options(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 56why, one step back56 response = httpx.options("http://127.0.0.1:8000/")Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 57–62why, one step back57 assert response.status_code == 20058 assert response.reason_phrase == "OK"596061@threadpool62def test_head(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 63why, one step back63 response = httpx.head("http://127.0.0.1:8000/")Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 64–69why, one step back64 assert response.status_code == 20065 assert response.reason_phrase == "OK"666768@threadpool69def test_put(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 70why, one step back70 response = httpx.put("http://127.0.0.1:8000/", data=b"Hello, world!")Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 71–76why, one step back71 assert response.status_code == 20072 assert response.reason_phrase == "OK"737475@threadpool76def test_patch(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 77why, one step back77 response = httpx.patch("http://127.0.0.1:8000/", data=b"Hello, world!")Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 78–83why, one step back78 assert response.status_code == 20079 assert response.reason_phrase == "OK"808182@threadpool83def test_delete(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 84why, one step back84 response = httpx.delete("http://127.0.0.1:8000/")Add top-level API (#75)Tom Christiebefore adoptionfc627f3
lines 85–86why, one step back85 assert response.status_code == 20086 assert response.reason_phrase == "OK"Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed
lines 87–90why, one step back878889@threadpool90def test_get_invalid_url(server):Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 91–92why, one step back91 with pytest.raises(httpx.InvalidURL):92 httpx.get("invalid://example.org")