Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

158 lines · 4.4 KiB · Python

Last changed by

Add `build_request` to `Client` (#319)Dobrovolsky Bogdanbefore adoption08edfac

History of this file

Take it

1import pytest23import httpx456def test_get(server):7    url = server.url8    with httpx.Client() as http:9        response = http.get(url)10    assert response.status_code == 20011    assert response.url == url12    assert response.content == b"Hello, world!"13    assert response.text == "Hello, world!"14    assert response.http_version == "HTTP/1.1"15    assert response.encoding == "iso-8859-1"16    assert response.request.url == url17    assert response.headers18    assert response.is_redirect is False19    assert repr(response) == "<Response [200 OK]>"202122def test_build_request(server):23    url = server.url.copy_with(path="/echo_headers")24    headers = {"Custom-header": "value"}2526    with httpx.Client() as http:27        request = http.build_request("GET", url)28        request.headers.update(headers)29        response = http.send(request)3031    assert response.status_code == 20032    assert response.url == url3334    assert response.json()["Custom-header"] == "value"353637def test_post(server):38    with httpx.Client() as http:39        response = http.post(server.url, data=b"Hello, world!")40    assert response.status_code == 20041    assert response.reason_phrase == "OK"424344def test_post_json(server):45    with httpx.Client() as http:46        response = http.post(server.url, json={"text": "Hello, world!"})47    assert response.status_code == 20048    assert response.reason_phrase == "OK"495051def test_stream_response(server):52    with httpx.Client() as http:53        response = http.get(server.url, stream=True)54    assert response.status_code == 20055    content = response.read()56    assert content == b"Hello, world!"575859def test_stream_iterator(server):60    with httpx.Client() as http:61        response = http.get(server.url, stream=True)62    assert response.status_code == 20063    body = b""64    for chunk in response.stream():65        body += chunk66    assert body == b"Hello, world!"676869def test_raw_iterator(server):70    with httpx.Client() as http:71        response = http.get(server.url, stream=True)72    assert response.status_code == 20073    body = b""74    for chunk in response.raw():75        body += chunk76    assert body == b"Hello, world!"77    response.close()  # TODO: should Response be available as context managers?787980def test_raise_for_status(server):81    with httpx.Client() as client:82        for status_code in (200, 400, 404, 500, 505):83            response = client.request(84                "GET", server.url.copy_with(path="/status/{}".format(status_code))85            )86            if 400 <= status_code < 600:87                with pytest.raises(httpx.exceptions.HTTPError) as exc_info:88                    response.raise_for_status()89                assert exc_info.value.response == response90            else:91                assert response.raise_for_status() is None929394def test_options(server):95    with httpx.Client() as http:96        response = http.options(server.url)97    assert response.status_code == 20098    assert response.reason_phrase == "OK"99100101def test_head(server):102    with httpx.Client() as http:103        response = http.head(server.url)104    assert response.status_code == 200105    assert response.reason_phrase == "OK"106107108def test_put(server):109    with httpx.Client() as http:110        response = http.put(server.url, data=b"Hello, world!")111    assert response.status_code == 200112    assert response.reason_phrase == "OK"113114115def test_patch(server):116    with httpx.Client() as http:117        response = http.patch(server.url, data=b"Hello, world!")118    assert response.status_code == 200119    assert response.reason_phrase == "OK"120121122def test_delete(server):123    with httpx.Client() as http:124        response = http.delete(server.url)125    assert response.status_code == 200126    assert response.reason_phrase == "OK"127128129def test_base_url(server):130    base_url = server.url131    with httpx.Client(base_url=base_url) as http:132        response = http.get("/")133    assert response.status_code == 200134    assert response.url == base_url135136137def test_merge_url():138    client = httpx.Client(base_url="https://www.paypal.com/")139    url = client.merge_url("http://www.paypal.com")140141    assert url.scheme == "https"142    assert url.is_ssl143144145class DerivedFromAsyncioBackend(httpx.AsyncioBackend):146    pass147148149class AnyBackend:150    pass151152153def test_client_backend_must_be_asyncio_based():154    httpx.Client(backend=httpx.AsyncioBackend())155    httpx.Client(backend=DerivedFromAsyncioBackend())156157    with pytest.raises(ValueError):158        httpx.Client(backend=AnyBackend())