From 93270c7949ac2555ce8371382b6dcf7638b78487 Mon Sep 17 00:00:00 2001 From: John Turner Date: Sat, 20 Sep 2025 11:41:43 -0400 Subject: add test_server.py --- tests/meson.build | 3 ++ tests/test_server.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tests/meson.build create mode 100755 tests/test_server.py (limited to 'tests') diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000..6787668 --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,3 @@ +test_server = find_program('test_server.py') + +test('test_server', test_server) diff --git a/tests/test_server.py b/tests/test_server.py new file mode 100755 index 0000000..2761b0c --- /dev/null +++ b/tests/test_server.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 + +import sys +import asyncio +import tempfile +import string +import random +import aiohttp +from pathlib import Path + + +def truncate(file: Path) -> None: + with open(file, "w") as f: + f.truncate(0) + + +def generate_name() -> str: + characters = string.ascii_letters + + return "".join(random.choice(characters) for _ in range(10)) + + +async def main() -> int: + with tempfile.TemporaryDirectory() as tmpdir: + socket = Path(tmpdir) / (generate_name() + ".sock") + database = Path(tmpdir) / generate_name() + + truncate(database) + + proc = await asyncio.create_subprocess_exec( + "python", + "-m", + "pypaste.server", + "--socket", + socket, + "--site", + "http://dummy", + "--content-length-max-bytes", + "200000", + "--key-length", + "3", + "--dictionary", + "/usr/share/dict/web2", + "--database", + database, + "--storage-max-bytes", + "500000000", + "sqlite", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.STDOUT, + ) + + assert proc.stdout is not None + + line = await proc.stdout.readline() + + if b"starting" not in line: + print(line, file=sys.stderr) + return 1 + + connection = aiohttp.UnixConnector(path=str(socket)) + + async with aiohttp.ClientSession(connector=connection) as client: + async with client.post("http://dummy/paste", data="hello pypaste") as post: + assert post.status == 200 + data = await post.read() + + url = data.decode() + + async with client.get(url) as get: + assert get.status == 200 + assert b"hello pypaste" in await get.read() + + return 0 + + +if __name__ == "__main__": + sys.exit(asyncio.run(main())) -- cgit v1.2.3