From df5906fd3ca9c322055d3e04994f7dcafc8817bd Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Tue, 30 Jun 2026 00:23:32 +0800 Subject: [PATCH] tmp --- core/example/hello.py | 47 ++++++++++++++++++++++++++++++++++------- core/tests/test_asgi.py | 31 ++++++++++++++++++--------- 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/core/example/hello.py b/core/example/hello.py index f262334..752a235 100644 --- a/core/example/hello.py +++ b/core/example/hello.py @@ -1,12 +1,13 @@ -from bugis.core import BugisApp, HttpContext - +from bugis.core import BugisApp, HttpContext, HttpMethod +from typing import List class Hello(BugisApp): + pass - async def handle_request(self, ctx: HttpContext) -> None: - async for chunk in ctx.request_body: - print(chunk) - await ctx.send_str(200, 'Hello World') + # async def handle_request(self, ctx: HttpContext) -> None: + # async for chunk in ctx.request_body: + # print(chunk.decode()) + # await ctx.send_str(200, 'Hello World') app = BugisApp() @@ -16,5 +17,35 @@ app = BugisApp() @app.GET('/hello2') async def handle_request(ctx: HttpContext) -> None: async for chunk in ctx.request_body: - print(chunk) - await ctx.send_str(200, 'Hello World') \ No newline at end of file + print(chunk.decode()) + await ctx.send_str(200, 'Hello World') + +@app.GET('/hello/alice') +async def hello_alice(ctx: HttpContext) -> None: + async for _ in ctx.request_body: + pass + await ctx.send_str(200, 'Hello Alice 2') + +@app.GET('/hello/*') +async def hello_alice(ctx: HttpContext, args: List[str]) -> None: + async for _ in ctx.request_body: + pass + await ctx.send_str(200, f'Hello {args[0]}') + +@app.GET('/hello/*/kennedy') +async def hello_alice(ctx: HttpContext, args: List[str]) -> None: + async for _ in ctx.request_body: + pass + await ctx.send_str(200, f'Hello {args[0]} kennedy') + +@app.GET('/hello/bob/marley') +async def hello_alice(ctx: HttpContext) -> None: + async for _ in ctx.request_body: + pass + await ctx.send_str(200, f'Hello bob Marley') + +@app.route('/hello/*', HttpMethod.PUT, recursive=True) +async def handle_request(ctx: HttpContext, _: List[str]) -> None: + async for chunk in ctx.request_body: + print(chunk.decode()) + await ctx.send_str(200, 'Message received') \ No newline at end of file diff --git a/core/tests/test_asgi.py b/core/tests/test_asgi.py index 72e74b4..83031a2 100644 --- a/core/tests/test_asgi.py +++ b/core/tests/test_asgi.py @@ -3,7 +3,7 @@ import json import httpx from pwo import async_test from bugis.core import BugisApp, HttpContext, HttpMethod -from typing import Sequence +from typing import Sequence, List class AsgiTest(unittest.TestCase): @@ -15,6 +15,7 @@ class AsgiTest(unittest.TestCase): @self.app.GET('/hello') @self.app.GET('/hello2') @self.app.route('/hello3') + @self.app.GET('/hello/*') async def handle_request(ctx: HttpContext) -> None: async for chunk in ctx.request_body: print(chunk) @@ -53,34 +54,44 @@ class AsgiTest(unittest.TestCase): 'employee_id': employee_id })) + @self.app.PUT('/hello/*', recursive=True) + async def handle_request(ctx: HttpContext, _: List[str]) -> None: + await ctx.stream_body(200, (chunk async for chunk in ctx.request_body)) + @async_test async def test_hello(self): transport = httpx.ASGITransport(app=self.app) async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client: r = await client.get("/hello") - self.assertEqual(r.status_code, 200) + self.assertEqual(200, r.status_code) self.assertEqual(r.text, "Hello World!") r = await client.get("/hello2") - self.assertEqual(r.status_code, 200) + self.assertEqual(200, r.status_code) self.assertEqual(r.text, "Hello World!") r = await client.post("/hello3") - self.assertEqual(r.status_code, 200) + self.assertEqual(200, r.status_code) self.assertEqual(r.text, "Hello World!") r = await client.get("/hello4") - self.assertEqual(r.status_code, 404) + self.assertEqual(404, r.status_code) self.assertTrue(len(r.text) == 0) + body = {'name': 'John', 'surname': 'Smith'} + r = await client.put("/hello/foo/bar", json=body) + self.assertEqual(200, r.status_code) + ans = json.loads(r.text) + self.assertTrue(body, ans) + @async_test async def test_foo(self): transport = httpx.ASGITransport(app=self.app) async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client: r = await client.put("/foo/fizz/baz") - self.assertEqual(r.status_code, 200) + self.assertEqual(200, r.status_code) response = json.loads(r.text) self.assertEqual(['fizz', 'baz'], response) @@ -90,7 +101,7 @@ class AsgiTest(unittest.TestCase): async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client: r = await client.put("/foo/bar/baz") - self.assertEqual(r.status_code, 200) + self.assertEqual(200, r.status_code) self.assertEqual('/foo/bar/baz', r.text) @async_test @@ -99,7 +110,7 @@ class AsgiTest(unittest.TestCase): async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client: r = await client.get("/employee/101325") - self.assertEqual(r.status_code, 200) + self.assertEqual(200, r.status_code) self.assertEqual(r.text, '101325') @async_test @@ -109,7 +120,7 @@ class AsgiTest(unittest.TestCase): async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client: x = 30 r = await client.get(f"/square/{x}") - self.assertEqual(r.status_code, 200) + self.assertEqual(200, r.status_code) self.assertEqual(r.text, str(x * x)) @async_test @@ -118,7 +129,7 @@ class AsgiTest(unittest.TestCase): async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client: r = await client.get("department/189350/employee/101325") - self.assertEqual(r.status_code, 200) + self.assertEqual(200, r.status_code) response = json.loads(r.text) self.assertEqual({ 'department_id': 189350,