Esempio n. 1
0
async def test_json_body(mocked_server: helpers.MockedServer) -> None:
    mocked_server.mock_proto_cls = PostEchoProtocol

    async with HttpClient() as client:
        response = await client.post(
            f"http://localhost:{mocked_server.port}",
            json={
                "a": "b",
                "c": [1, 2]
            },
        )

    assert response.request.headers == {
        "user-agent": helpers.get_version_str(),
        "content-type": "application/json",
        "content-length": "23",
        "accept": "*/*",
        "host": f"localhost:{mocked_server.port}",
    }

    proto = mocked_server.select_proto()

    initial_bytes, body = b"".join(proto.data_chunks).split(b"\r\n\r\n", 1)

    proto.assert_initial(
        b"POST / HTTP/1.1",
        b"User-Agent: %(self_ver_bytes)s",
        b"Content-Type: application/json",
        b"Content-Length: 23",
        b"Accept: */*",
        f"Host: localhost:{mocked_server.port}".encode(),
    )

    assert body == b'{"a": "b", "c": [1, 2]}'
Esempio n. 2
0
async def test_simple(mocked_server: helpers.MockedServer) -> None:
    mocked_server.mock_proto_cls = HeadEchoProtocol

    response = await head(f"http://localhost:{mocked_server.port}")

    assert response.status_code == 200
    assert response.body == b""
    assert response.version == HttpVersion.V1_1
    assert response.headers == {"content-length": "13"}

    assert response.request.method == HttpRequestMethod.HEAD
    assert response.request.version == HttpVersion.V1_1
    assert response.request.uri == "/"
    assert response.request.authority == f"localhost:{mocked_server.port}"
    assert not hasattr(response.request, "scheme")
    assert response.request.headers == {
        "user-agent": helpers.get_version_str(),
        "accept": "*/*",
        "host": f"localhost:{mocked_server.port}",
    }

    mocked_server.select_proto().assert_initial(
        b"HEAD / HTTP/1.1",
        b"User-Agent: %(self_ver_bytes)s",
        b"Accept: */*",
        f"Host: localhost:{mocked_server.port}".encode(),
    )
Esempio n. 3
0
async def test_json(mocked_server: helpers.MockedServer) -> None:
    mocked_server.mock_proto_cls = JsonResponseProtocol
    async with HttpClient() as client:
        response = await client.get(f"http://localhost:{mocked_server.port}")

        assert response.status_code == 200
        assert response.body.to_json() == {"a": "b"}

        mocked_server.select_proto().assert_initial(
            b"GET / HTTP/1.1",
            b"User-Agent: %(self_ver_bytes)s",
            b"Accept: */*",
            f"Host: localhost:{mocked_server.port}".encode(),
        )
Esempio n. 4
0
async def test_response_404(mocked_server: helpers.MockedServer, ) -> None:
    mocked_server.mock_proto_cls = Http404Protocol
    async with HttpClient() as client:
        with pytest.raises(HttpError) as exc_info:
            await client.get(f"http://localhost:{mocked_server.port}/")

        assert exc_info.value.status_code == 404
        assert exc_info.value.status_description == "Not Found"
Esempio n. 5
0
async def test_default_no_redirect(
    mocked_server: helpers.MockedServer, ) -> None:
    mocked_server.mock_proto_cls = AlwaysRedirectProtocol
    async with HttpClient() as client:
        response = await client.get(f"http://localhost:{mocked_server.port}/")

        assert response.status_code == 302
        assert response.headers["location"] == "/"
Esempio n. 6
0
async def test_path_args(mocked_server: helpers.MockedServer) -> None:
    mocked_server.mock_proto_cls = GetEchoProtocol

    async with HttpClient() as client:
        response = await client.get(
            f"http://localhost:{mocked_server.port}/?a=b",
            path_args={"c": "d"})

        assert response.status_code == 200
        assert response.body == b"Hello, World!"

        mocked_server.select_proto().assert_initial(
            b"GET /?a=b&c=d HTTP/1.1",
            b"User-Agent: %(self_ver_bytes)s",
            b"Accept: */*",
            f"Host: localhost:{mocked_server.port}".encode(),
        )
Esempio n. 7
0
async def test_response_404_no_raise(
    mocked_server: helpers.MockedServer, ) -> None:
    mocked_server.mock_proto_cls = Http404Protocol
    async with HttpClient(raise_error=False) as client:
        response = await client.get(f"http://localhost:{mocked_server.port}/")

        assert response.status_code == 404
        assert response.body == b"HTTP 404: Not Found"
Esempio n. 8
0
async def test_too_many_redirects(
    mocked_server: helpers.MockedServer, ) -> None:
    mocked_server.mock_proto_cls = AlwaysRedirectProtocol
    async with HttpClient() as client:
        with pytest.raises(TooManyRedirects):
            await client.get(
                f"http://localhost:{mocked_server.port}/",
                follow_redirection=True,
            )
Esempio n. 9
0
async def test_simple(mocked_server: helpers.MockedServer) -> None:
    mocked_server.mock_proto_cls = MultipartEchoProtocol
    await post(
        f"http://localhost:{mocked_server.port}/",
        body={
            "a": "b",
            "c": io.BytesIO(b"1234567890")
        },
    )
Esempio n. 10
0
async def test_prevent_relative_redirect(
    mocked_server: helpers.MockedServer, ) -> None:
    mocked_server.mock_proto_cls = RelativeRedirectProtocol
    async with HttpClient() as client:
        with pytest.raises(FailedRedirection):
            await client.get(
                f"http://localhost:{mocked_server.port}/",
                follow_redirection=True,
            )
Esempio n. 11
0
async def test_redirect_successful(
    mocked_server: helpers.MockedServer, ) -> None:
    mocked_server.mock_proto_cls = Redirect10TimesProtocol
    async with HttpClient() as client:
        response = await client.get(f"http://localhost:{mocked_server.port}/",
                                    follow_redirection=True)

        assert response.status_code == 200
        assert response.body == b"Hello, World!"
Esempio n. 12
0
async def test_urlencoded_and_json_body(
    mocked_server: helpers.MockedServer, ) -> None:
    mocked_server.mock_proto_cls = PostEchoProtocol
    async with HttpClient() as client:
        with pytest.raises(ValueError):
            await client.post(
                f"http://localhost:{mocked_server.port}",
                body={"a": "b"},
                json={"c": "d"},
            )
Esempio n. 13
0
async def test_simple(mocked_server: helpers.MockedServer) -> None:
    mocked_server.mock_proto_cls = PatchEchoProtocol

    response = await patch(f"http://localhost:{mocked_server.port}",
                           json={
                               "a": "b",
                               "c": [1, 2]
                           })

    assert response.status_code == 200
    assert response.body == b"Hello, World!"
    assert response.version == HttpVersion.V1_1
    assert response.headers == {"content-length": "13"}

    assert response.request.method == HttpRequestMethod.PATCH
    assert response.request.version == HttpVersion.V1_1
    assert response.request.uri == "/"
    assert response.request.authority == f"localhost:{mocked_server.port}"
    assert not hasattr(response.request, "scheme")
    assert response.request.headers == {
        "user-agent": helpers.get_version_str(),
        "content-type": "application/json",
        "content-length": "23",
        "accept": "*/*",
        "host": f"localhost:{mocked_server.port}",
    }

    proto = mocked_server.select_proto()

    proto.assert_initial(
        b"PATCH / HTTP/1.1",
        b"User-Agent: %(self_ver_bytes)s",
        b"Content-Type: application/json",
        b"Content-Length: 23",
        b"Accept: */*",
        f"Host: localhost:{mocked_server.port}".encode(),
    )

    initial_bytes, body = b"".join(proto.data_chunks).split(b"\r\n\r\n", 1)

    assert body == b'{"a": "b", "c": [1, 2]}'
Esempio n. 14
0
async def test_simple(mocked_server: helpers.MockedServer) -> None:
    mocked_server.mock_proto_cls = PostEchoProtocol

    response = await post(f"http://localhost:{mocked_server.port}",
                          body=b"1234567890")

    assert response.status_code == 200
    assert response.body == b"Hello, World!"
    assert response.version == HttpVersion.V1_1
    assert response.headers == {"content-length": "13"}

    assert response.request.method == HttpRequestMethod.POST
    assert response.request.version == HttpVersion.V1_1
    assert response.request.uri == "/"
    assert response.request.authority == f"localhost:{mocked_server.port}"
    assert not hasattr(response.request, "scheme")
    assert response.request.headers == {
        "user-agent": helpers.get_version_str(),
        "content-length": "10",
        "accept": "*/*",
        "host": f"localhost:{mocked_server.port}",
    }
Esempio n. 15
0
async def test_malformed_data(mocked_server: helpers.MockedServer) -> None:
    mocked_server.mock_proto_cls = MalformedProtocol
    async with HttpClient(max_body_size=12) as client:
        with pytest.raises(BadResponse):
            await client.get(f"http://localhost:{mocked_server.port}/")
Esempio n. 16
0
async def test_too_large_2(mocked_server: helpers.MockedServer) -> None:
    mocked_server.mock_proto_cls = UrandomProtocol
    async with HttpClient(max_body_size=12) as client:
        with pytest.raises(ResponseEntityTooLarge):
            await client.get(f"http://localhost:{mocked_server.port}/")
Esempio n. 17
0
async def test_connection_closed(
    mocked_server: helpers.MockedServer, ) -> None:
    mocked_server.mock_proto_cls = ConnectionClosedProtocol
    async with HttpClient() as client:
        with pytest.raises(ConnectionClosed):
            await client.get(f"http://localhost:{mocked_server.port}/")