41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
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)
|