Ejemplo n.º 1
0
    async def test_aiohttp_hook_when_files(self, mock_interaction_server: interaction_server_impl.InteractionServer):
        mock_interaction_server.on_interaction = mock.AsyncMock(
            return_value=mock.Mock(
                payload=b"abody",
                files=[files.Bytes("x" * 329, "meow.txt"), files.Bytes("y" * 124, "nyaa.txt")],
                status_code=200,
                headers={"header1": "ok"},
                content_type="oogabooga",
            )
        )
        request = mock.Mock(
            aiohttp.web.Request,
            content_type="application/json",
            headers={"X-Signature-Ed25519": "74726f656b70657769656f6a6b736939", "X-Signature-Timestamp": "123123"},
            read=mock.AsyncMock(return_value=b"bfddasdasd"),
        )

        result = await mock_interaction_server.aiohttp_hook(request)

        mock_interaction_server.on_interaction.assert_awaited_once_with(
            body=b"bfddasdasd", signature=b"troekpewieojksi9", timestamp=b"123123"
        )
        assert isinstance(result.body, aiohttp.MultipartWriter)
        assert result.content_type == "multipart/form-data"
        assert result.headers == {
            "header1": "ok",
            "Content-Type": f"multipart/form-data; boundary={result.body.boundary}",
        }
        assert result.status == 200

        mock_writer = MockWriter()
        await result.body.write(mock_writer)

        boundary = result.body.boundary.encode()
        assert mock_writer.payload == (
            b"--" + boundary + b"""\r\nContent-Type: oogabooga\r\nContent-Disposition: form-data; name="payload_json"""
            b""""\r\nContent-Length: 5\r\n\r\nabody\r\n--""" + boundary + b"""\r\nContent-Type: text/plain\r\nConten"""
            b"""t-Disposition: form-data; name="files[0]"; filename="meow.txt"\r\n\r\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"""
            b"""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"""
            b"""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"""
            b"""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"""
            b"""\r\n--""" + boundary + b"""\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name="fil"""
            b"""es[1]"; filename="nyaa.txt"\r\n\r\nyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"""
            b"""yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\r\n--""" + boundary + b"""--\r\n"""
        )
Ejemplo n.º 2
0
    async def test_aiohttp_hook_when_no_other_headers(
        self, mock_interaction_server: interaction_server_impl.InteractionServer
    ):
        mock_interaction_server.on_interaction = mock.AsyncMock(
            return_value=mock.Mock(payload=b"abody", files=[], headers=None, status_code=200, content_type="oogabooga")
        )
        request = mock.Mock(
            aiohttp.web.Request,
            content_type="application/json",
            headers={"X-Signature-Ed25519": "74726f656b70657769656f6a6b736939", "X-Signature-Timestamp": "123123"},
            read=mock.AsyncMock(return_value=b"bfddasdasd"),
        )

        result = await mock_interaction_server.aiohttp_hook(request)

        mock_interaction_server.on_interaction.assert_awaited_once_with(
            body=b"bfddasdasd", signature=b"troekpewieojksi9", timestamp=b"123123"
        )
        assert result.body == b"abody"
        assert result.content_type == "oogabooga"
        assert result.headers == {"Content-Type": "oogabooga"}
        assert result.status == 200