Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

135 lines · 3.7 KiB · Python

Last changed by

repr change: <SyncResponse ...> -> <Response ...>Tom Christiebefore adoption73d5fca

History of this file

Take it

1import asyncio2import functools34import pytest56import httpcore789def 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 httpcore.Client() as http:30        response = http.get(url)31    assert response.status_code == 20032    assert response.url == httpcore.URL(url)33    assert response.content == b"Hello, world!"34    assert response.text == "Hello, world!"35    assert response.protocol == "HTTP/1.1"36    assert response.encoding == "iso-8859-1"37    assert response.request.url == httpcore.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 httpcore.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_stream_response(server):53    with httpcore.Client() as http:54        response = http.get("http://127.0.0.1:8000/", stream=True)55    assert response.status_code == 20056    content = response.read()57    assert content == b"Hello, world!"585960@threadpool61def test_stream_iterator(server):62    with httpcore.Client() as http:63        response = http.get("http://127.0.0.1:8000/", stream=True)64    assert response.status_code == 20065    body = b""66    for chunk in response.stream():67        body += chunk68    assert body == b"Hello, world!"697071@threadpool72def test_raw_iterator(server):73    with httpcore.Client() as http:74        response = http.get("http://127.0.0.1:8000/", stream=True)75    assert response.status_code == 20076    body = b""77    for chunk in response.raw():78        body += chunk79    assert body == b"Hello, world!"80    response.close()  # TODO: should Response be available as context managers?818283@threadpool84def test_raise_for_status(server):85    with httpcore.Client() as client:86        for status_code in (200, 400, 404, 500, 505):87            response = client.request(88                "GET", "http://127.0.0.1:8000/status/{}".format(status_code)89            )9091            if 400 <= status_code < 600:92                with pytest.raises(httpcore.exceptions.HttpError):93                    response.raise_for_status()94            else:95                assert response.raise_for_status() is None969798@threadpool99def test_options(server):100    with httpcore.Client() as http:101        response = http.options("http://127.0.0.1:8000/")102    assert response.status_code == 200103    assert response.reason_phrase == "OK"104105106@threadpool107def test_head(server):108    with httpcore.Client() as http:109        response = http.head("http://127.0.0.1:8000/")110    assert response.status_code == 200111    assert response.reason_phrase == "OK"112113114@threadpool115def test_put(server):116    with httpcore.Client() as http:117        response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")118    assert response.status_code == 200119    assert response.reason_phrase == "OK"120121122@threadpool123def test_patch(server):124    with httpcore.Client() as http:125        response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")126    assert response.status_code == 200127    assert response.reason_phrase == "OK"128129130@threadpool131def test_delete(server):132    with httpcore.Client() as http:133        response = http.delete("http://127.0.0.1:8000/")134    assert response.status_code == 200135    assert response.reason_phrase == "OK"