create core framework

This commit is contained in:
2024-11-02 23:04:36 +08:00
parent 6acf6d1d6e
commit 544229b7a6
30 changed files with 1612 additions and 16 deletions

40
core/tests/test_asgi.py Normal file
View File

@@ -0,0 +1,40 @@
import unittest
import httpx
from pwo import async_test
from bugis.core import BugisApp, HttpContext
class AsgiTest(unittest.TestCase):
app: BugisApp
def setUp(self):
self.app = BugisApp()
@self.app.GET('/hello')
@self.app.GET('/hello2')
@self.app.route('/hello3')
async def handle_request(ctx: HttpContext) -> None:
async for chunk in ctx.request_body:
print(chunk)
await ctx.send_str(200, 'Hello World!')
@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(r.text, "Hello World!")
r = await client.get("/hello2")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, "Hello World!")
r = await client.post("/hello3")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, "Hello World!")
r = await client.get("/hello4")
self.assertEqual(r.status_code, 404)
self.assertTrue(len(r.text) == 0)