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(), )
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]}'
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"
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"
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"] == "/"
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, )
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, )
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!"
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"}, )
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(), )
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(), )
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}/")
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}/")
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}/")