Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

176 lines · 4.7 KiB · Python

Last changed by

Response.protocol -> Response.http_version (#250)Tom Christiebefore adoptionc1ec1c3

History of this file

Take it

1import asyncio2import functools34import pytest56import httpx789def threadpool(func):10    """11    Our sync tests should run in seperate thread to the uvicorn server.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):28    url = "http://127.0.0.1:8000/"29    with httpx.Client() as http:30        response = http.get(url)31    assert response.status_code == 20032    assert response.url == httpx.URL(url)33    assert response.content == b"Hello, world!"34    assert response.text == "Hello, world!"35    assert response.http_version == "HTTP/1.1"36    assert response.encoding == "iso-8859-1"37    assert response.request.url == httpx.URL(url)38    assert response.headers39    assert response.is_redirect is False40    assert repr(response) == "<Response [200 OK]>"414243@threadpool44def test_post(server):45    with httpx.Client() as http:46        response = http.post("http://127.0.0.1:8000/", data=b"Hello, world!")47    assert response.status_code == 20048    assert response.reason_phrase == "OK"495051@threadpool52def test_post_json(server):53    with httpx.Client() as http:54        response = http.post("http://127.0.0.1:8000/", json={"text": "Hello, world!"})55    assert response.status_code == 20056    assert response.reason_phrase == "OK"575859@threadpool60def test_stream_response(server):61    with httpx.Client() as http:62        response = http.get("http://127.0.0.1:8000/", stream=True)63    assert response.status_code == 20064    content = response.read()65    assert content == b"Hello, world!"666768@threadpool69def test_stream_iterator(server):70    with httpx.Client() as http:71        response = http.get("http://127.0.0.1:8000/", stream=True)72    assert response.status_code == 20073    body = b""74    for chunk in response.stream():75        body += chunk76    assert body == b"Hello, world!"777879@threadpool80def test_raw_iterator(server):81    with httpx.Client() as http:82        response = http.get("http://127.0.0.1:8000/", stream=True)83    assert response.status_code == 20084    body = b""85    for chunk in response.raw():86        body += chunk87    assert body == b"Hello, world!"88    response.close()  # TODO: should Response be available as context managers?899091@threadpool92def test_raise_for_status(server):93    with httpx.Client() as client:94        for status_code in (200, 400, 404, 500, 505):95            response = client.request(96                "GET", "http://127.0.0.1:8000/status/{}".format(status_code)97            )98            if 400 <= status_code < 600:99                with pytest.raises(httpx.exceptions.HTTPError) as exc_info:100                    response.raise_for_status()101                assert exc_info.value.response == response102            else:103                assert response.raise_for_status() is None104105106@threadpool107def test_options(server):108    with httpx.Client() as http:109        response = http.options("http://127.0.0.1:8000/")110    assert response.status_code == 200111    assert response.reason_phrase == "OK"112113114@threadpool115def test_head(server):116    with httpx.Client() as http:117        response = http.head("http://127.0.0.1:8000/")118    assert response.status_code == 200119    assert response.reason_phrase == "OK"120121122@threadpool123def test_put(server):124    with httpx.Client() as http:125        response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")126    assert response.status_code == 200127    assert response.reason_phrase == "OK"128129130@threadpool131def test_patch(server):132    with httpx.Client() as http:133        response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")134    assert response.status_code == 200135    assert response.reason_phrase == "OK"136137138@threadpool139def test_delete(server):140    with httpx.Client() as http:141        response = http.delete("http://127.0.0.1:8000/")142    assert response.status_code == 200143    assert response.reason_phrase == "OK"144145146@threadpool147def test_base_url(server):148    base_url = "http://127.0.0.1:8000/"149    with httpx.Client(base_url=base_url) as http:150        response = http.get("/")151    assert response.status_code == 200152    assert str(response.url) == base_url153154155def test_merge_url():156    client = httpx.Client(base_url="https://www.paypal.com/")157    url = client.merge_url("http://www.paypal.com")158159    assert url.scheme == "https"160    assert url.is_ssl161162163class DerivedFromAsyncioBackend(httpx.AsyncioBackend):164    pass165166167class AnyBackend:168    pass169170171def test_client_backend_must_be_asyncio_based():172    httpx.Client(backend=httpx.AsyncioBackend())173    httpx.Client(backend=DerivedFromAsyncioBackend())174175    with pytest.raises(ValueError):176        httpx.Client(backend=AnyBackend())