Skip to content
Myr

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 file

Take it

Add top-level API (#75)Tom Christiebefore adoptionfc627f3

lines 1–5why, one step back
1import asyncio
2import functools34import pytest5

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 6why, one step back
6import httpx

Add top-level API (#75)Tom Christiebefore adoptionfc627f3

lines 7–10why, one step back
7
89def threadpool(func):10 """

Fix typos in docstringsSaugat Pachhaibefore adoption3451028

line 11why, one step back
11 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 back
12 """
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 back
28 response = httpx.get("http://127.0.0.1:8000/")

Add top-level API (#75)Tom Christiebefore adoptionfc627f3

lines 29–31why, one step back
29 assert response.status_code == 200
30 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 back
32 assert response.http_version == "HTTP/1.1"

Add top-level API (#75)Tom Christiebefore adoptionfc627f3

lines 33–36why, one step back
33
3435@threadpool36def test_post(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 37why, one step back
37 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 back
38 assert response.status_code == 200
39 assert response.reason_phrase == "OK"404142@threadpool

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 43–48why, one step back
43def test_post_byte_iterator(server):
44 def data():45 yield b"Hello"46 yield b", "47 yield b"world!"48

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 49why, one step back
49 response = httpx.post("http://127.0.0.1:8000/", data=data())

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 50–54why, one step back
50 assert response.status_code == 200
51 assert response.reason_phrase == "OK"525354@threadpool

Add top-level API (#75)Tom Christiebefore adoptionfc627f3

line 55why, one step back
55def test_options(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 56why, one step back
56 response = httpx.options("http://127.0.0.1:8000/")

Add top-level API (#75)Tom Christiebefore adoptionfc627f3

lines 57–62why, one step back
57 assert response.status_code == 200
58 assert response.reason_phrase == "OK"596061@threadpool62def test_head(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 63why, one step back
63 response = httpx.head("http://127.0.0.1:8000/")

Add top-level API (#75)Tom Christiebefore adoptionfc627f3

lines 64–69why, one step back
64 assert response.status_code == 200
65 assert response.reason_phrase == "OK"666768@threadpool69def test_put(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 70why, one step back
70 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 back
71 assert response.status_code == 200
72 assert response.reason_phrase == "OK"737475@threadpool76def test_patch(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 77why, one step back
77 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 back
78 assert response.status_code == 200
79 assert response.reason_phrase == "OK"808182@threadpool83def test_delete(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 84why, one step back
84 response = httpx.delete("http://127.0.0.1:8000/")

Add top-level API (#75)Tom Christiebefore adoptionfc627f3

lines 85–86why, one step back
85 assert response.status_code == 200
86 assert response.reason_phrase == "OK"

Minor tweaks in line with requests behavior (#110)Tom Christiebefore adoptionfe1f9ed

lines 87–90why, one step back
87
8889@threadpool90def test_get_invalid_url(server):

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

lines 91–92why, one step back
91 with pytest.raises(httpx.InvalidURL):
92 httpx.get("invalid://example.org")