Skip to content
Myr

test_client.py

This file

Path

tests/client/test_client.py

164 lines · 4.6 KiB · Python

Last changed by

Add "Content-Length: 0" on POST, PUT, PATCH if required. (#995)Tom Christiebefore adoption7c8158a

History of this file

Take it

1from datetime import timedelta23import pytest45import httpx678def test_get(server):9    url = server.url10    with httpx.Client() as http:11        response = http.get(url)12    assert response.status_code == 20013    assert response.url == url14    assert response.content == b"Hello, world!"15    assert response.text == "Hello, world!"16    assert response.http_version == "HTTP/1.1"17    assert response.encoding == "iso-8859-1"18    assert response.request.url == url19    assert response.headers20    assert response.is_redirect is False21    assert repr(response) == "<Response [200 OK]>"22    assert response.elapsed > timedelta(0)232425def test_build_request(server):26    url = server.url.copy_with(path="/echo_headers")27    headers = {"Custom-header": "value"}2829    with httpx.Client() as client:30        request = client.build_request("GET", url)31        request.headers.update(headers)32        response = client.send(request)3334    assert response.status_code == 20035    assert response.url == url3637    assert response.json()["Custom-header"] == "value"383940def test_build_post_request(server):41    url = server.url.copy_with(path="/echo_headers")42    headers = {"Custom-header": "value"}4344    with httpx.Client() as client:45        request = client.build_request("POST", url)46        request.headers.update(headers)47        response = client.send(request)4849    assert response.status_code == 20050    assert response.url == url5152    assert response.json()["Content-length"] == "0"53    assert response.json()["Custom-header"] == "value"545556def test_post(server):57    with httpx.Client() as client:58        response = client.post(server.url, data=b"Hello, world!")59    assert response.status_code == 20060    assert response.reason_phrase == "OK"616263def test_post_json(server):64    with httpx.Client() as client:65        response = client.post(server.url, json={"text": "Hello, world!"})66    assert response.status_code == 20067    assert response.reason_phrase == "OK"686970def test_stream_response(server):71    with httpx.Client() as client:72        with client.stream("GET", server.url) as response:73            content = response.read()74    assert response.status_code == 20075    assert content == b"Hello, world!"767778def test_stream_iterator(server):79    body = b""8081    with httpx.Client() as client:82        with client.stream("GET", server.url) as response:83            for chunk in response.iter_bytes():84                body += chunk8586    assert response.status_code == 20087    assert body == b"Hello, world!"888990def test_raw_iterator(server):91    body = b""9293    with httpx.Client() as client:94        with client.stream("GET", server.url) as response:95            for chunk in response.iter_raw():96                body += chunk9798    assert response.status_code == 20099    assert body == b"Hello, world!"100101102def test_raise_for_status(server):103    with httpx.Client() as client:104        for status_code in (200, 400, 404, 500, 505):105            response = client.request(106                "GET", server.url.copy_with(path="/status/{}".format(status_code))107            )108            if 400 <= status_code < 600:109                with pytest.raises(httpx.HTTPError) as exc_info:110                    response.raise_for_status()111                assert exc_info.value.response == response112            else:113                assert response.raise_for_status() is None114115116def test_options(server):117    with httpx.Client() as client:118        response = client.options(server.url)119    assert response.status_code == 200120    assert response.reason_phrase == "OK"121122123def test_head(server):124    with httpx.Client() as client:125        response = client.head(server.url)126    assert response.status_code == 200127    assert response.reason_phrase == "OK"128129130def test_put(server):131    with httpx.Client() as client:132        response = client.put(server.url, data=b"Hello, world!")133    assert response.status_code == 200134    assert response.reason_phrase == "OK"135136137def test_patch(server):138    with httpx.Client() as client:139        response = client.patch(server.url, data=b"Hello, world!")140    assert response.status_code == 200141    assert response.reason_phrase == "OK"142143144def test_delete(server):145    with httpx.Client() as client:146        response = client.delete(server.url)147    assert response.status_code == 200148    assert response.reason_phrase == "OK"149150151def test_base_url(server):152    base_url = server.url153    with httpx.Client(base_url=base_url) as client:154        response = client.get("/")155    assert response.status_code == 200156    assert response.url == base_url157158159def test_merge_url():160    client = httpx.Client(base_url="https://www.paypal.com/")161    url = client.merge_url("http://www.paypal.com")162163    assert url.scheme == "https"164    assert url.is_ssl