Пример #1
0
def test_stream_iterator(server):
    with http3.Client() as http:
        response = http.get("http://127.0.0.1:8000/", stream=True)
    assert response.status_code == 200
    body = b""
    for chunk in response.stream():
        body += chunk
    assert body == b"Hello, world!"
Пример #2
0
def test_raw_iterator(server):
    with http3.Client() as http:
        response = http.get("http://127.0.0.1:8000/", stream=True)
    assert response.status_code == 200
    body = b""
    for chunk in response.raw():
        body += chunk
    assert body == b"Hello, world!"
    response.close()  # TODO: should Response be available as context managers?
Пример #3
0
def test_raise_for_status(server):
    with http3.Client() as client:
        for status_code in (200, 400, 404, 500, 505):
            response = client.request(
                "GET", "http://127.0.0.1:8000/status/{}".format(status_code))

            if 400 <= status_code < 600:
                with pytest.raises(http3.exceptions.HttpError):
                    response.raise_for_status()
            else:
                assert response.raise_for_status() is None
Пример #4
0
def test_get(server):
    url = "http://127.0.0.1:8000/"
    with http3.Client() as http:
        response = http.get(url)
    assert response.status_code == 200
    assert response.url == http3.URL(url)
    assert response.content == b"Hello, world!"
    assert response.text == "Hello, world!"
    assert response.protocol == "HTTP/1.1"
    assert response.encoding == "iso-8859-1"
    assert response.request.url == http3.URL(url)
    assert response.headers
    assert response.is_redirect is False
    assert repr(response) == "<Response [200 OK]>"
Пример #5
0
def blocking_request(method,
                     url,
                     timeout,
                     *,
                     data=None,
                     headers=None,
                     stream=False,
                     client=None,
                     **kwargs):
    """Returns a Response object."""
    if not client:
        client = http3.Client()
    with client as http:
        r = http.request(method=method,
                         url=url,
                         headers=headers,
                         stream=stream,
                         data=data,
                         **kwargs)
        return r
Пример #6
0
def test_stream_response(server):
    with http3.Client() as http:
        response = http.get("http://127.0.0.1:8000/", stream=True)
    assert response.status_code == 200
    content = response.read()
    assert content == b"Hello, world!"
Пример #7
0
def test_post_json(server):
    with http3.Client() as http:
        response = http.post("http://127.0.0.1:8000/", json={"text": "Hello, world!"})
    assert response.status_code == 200
    assert response.reason_phrase == "OK"
Пример #8
0
def test_base_url(server):
    base_url = "http://127.0.0.1:8000/"
    with http3.Client(base_url=base_url) as http:
        response = http.get('/')
    assert response.status_code == 200
    assert str(response.url) == base_url
Пример #9
0
def test_delete(server):
    with http3.Client() as http:
        response = http.delete("http://127.0.0.1:8000/")
    assert response.status_code == 200
    assert response.reason_phrase == "OK"
Пример #10
0
def test_patch(server):
    with http3.Client() as http:
        response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")
    assert response.status_code == 200
    assert response.reason_phrase == "OK"
Пример #11
0
def test_asgi_exc_after_response():
    client = http3.Client(app=raise_exc_after_response)
    with pytest.raises(ValueError):
        response = client.get("http://www.example.org/")
Пример #12
0
def test_asgi_upload():
    client = http3.Client(app=echo_body)
    response = client.post("http://www.example.org/", data=b"example")
    assert response.status_code == 200
    assert response.text == "example"
Пример #13
0
def test_asgi():
    client = http3.Client(app=hello_world)
    response = client.get("http://www.example.org/")
    assert response.status_code == 200
    assert response.text == "Hello, World!"