コード例 #1
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(),
    )
コード例 #2
0
async def test_simple_unix(
    mocked_unix_server: helpers.MockedUnixServer, ) -> None:
    mocked_unix_server.mock_proto_cls = GetEchoProtocol

    client = HttpClient()
    client.resolver.override(
        "localhost",
        mocked_unix_server.port,
        resolve_to=mocked_unix_server.path,
    )

    response = await client.get(f"http://localhost:{mocked_unix_server.port}")

    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.GET
    assert response.request.version == HttpVersion.V1_1
    assert response.request.uri == "/"
    assert response.request.authority == f"localhost:{mocked_unix_server.port}"
    assert not hasattr(response.request, "scheme")
    assert response.request.headers == {
        "user-agent": helpers.get_version_str(),
        "accept": "*/*",
        "host": f"localhost:{mocked_unix_server.port}",
    }

    mocked_unix_server.select_proto().assert_initial(
        b"GET / HTTP/1.1",
        b"User-Agent: %(self_ver_bytes)s",
        b"Accept: */*",
        f"Host: localhost:{mocked_unix_server.port}".encode(),
    )
コード例 #3
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]}'
コード例 #4
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]}'
コード例 #5
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}",
    }