This commit is contained in:
2026-06-30 00:23:32 +08:00
parent 77362fd05c
commit df5906fd3c
2 changed files with 60 additions and 18 deletions
+39 -8
View File
@@ -1,12 +1,13 @@
from bugis.core import BugisApp, HttpContext from bugis.core import BugisApp, HttpContext, HttpMethod
from typing import List
class Hello(BugisApp): class Hello(BugisApp):
pass
async def handle_request(self, ctx: HttpContext) -> None: # async def handle_request(self, ctx: HttpContext) -> None:
async for chunk in ctx.request_body: # async for chunk in ctx.request_body:
print(chunk) # print(chunk.decode())
await ctx.send_str(200, 'Hello World') # await ctx.send_str(200, 'Hello World')
app = BugisApp() app = BugisApp()
@@ -16,5 +17,35 @@ app = BugisApp()
@app.GET('/hello2') @app.GET('/hello2')
async def handle_request(ctx: HttpContext) -> None: async def handle_request(ctx: HttpContext) -> None:
async for chunk in ctx.request_body: async for chunk in ctx.request_body:
print(chunk) print(chunk.decode())
await ctx.send_str(200, 'Hello World') 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')
+21 -10
View File
@@ -3,7 +3,7 @@ import json
import httpx import httpx
from pwo import async_test from pwo import async_test
from bugis.core import BugisApp, HttpContext, HttpMethod from bugis.core import BugisApp, HttpContext, HttpMethod
from typing import Sequence from typing import Sequence, List
class AsgiTest(unittest.TestCase): class AsgiTest(unittest.TestCase):
@@ -15,6 +15,7 @@ class AsgiTest(unittest.TestCase):
@self.app.GET('/hello') @self.app.GET('/hello')
@self.app.GET('/hello2') @self.app.GET('/hello2')
@self.app.route('/hello3') @self.app.route('/hello3')
@self.app.GET('/hello/*')
async def handle_request(ctx: HttpContext) -> None: async def handle_request(ctx: HttpContext) -> None:
async for chunk in ctx.request_body: async for chunk in ctx.request_body:
print(chunk) print(chunk)
@@ -53,34 +54,44 @@ class AsgiTest(unittest.TestCase):
'employee_id': employee_id '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_test
async def test_hello(self): async def test_hello(self):
transport = httpx.ASGITransport(app=self.app) transport = httpx.ASGITransport(app=self.app)
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client: async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
r = await client.get("/hello") r = await client.get("/hello")
self.assertEqual(r.status_code, 200) self.assertEqual(200, r.status_code)
self.assertEqual(r.text, "Hello World!") self.assertEqual(r.text, "Hello World!")
r = await client.get("/hello2") r = await client.get("/hello2")
self.assertEqual(r.status_code, 200) self.assertEqual(200, r.status_code)
self.assertEqual(r.text, "Hello World!") self.assertEqual(r.text, "Hello World!")
r = await client.post("/hello3") r = await client.post("/hello3")
self.assertEqual(r.status_code, 200) self.assertEqual(200, r.status_code)
self.assertEqual(r.text, "Hello World!") self.assertEqual(r.text, "Hello World!")
r = await client.get("/hello4") r = await client.get("/hello4")
self.assertEqual(r.status_code, 404) self.assertEqual(404, r.status_code)
self.assertTrue(len(r.text) == 0) 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_test
async def test_foo(self): async def test_foo(self):
transport = httpx.ASGITransport(app=self.app) transport = httpx.ASGITransport(app=self.app)
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client: async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
r = await client.put("/foo/fizz/baz") r = await client.put("/foo/fizz/baz")
self.assertEqual(r.status_code, 200) self.assertEqual(200, r.status_code)
response = json.loads(r.text) response = json.loads(r.text)
self.assertEqual(['fizz', 'baz'], response) 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: async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
r = await client.put("/foo/bar/baz") 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) self.assertEqual('/foo/bar/baz', r.text)
@async_test @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: async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
r = await client.get("/employee/101325") r = await client.get("/employee/101325")
self.assertEqual(r.status_code, 200) self.assertEqual(200, r.status_code)
self.assertEqual(r.text, '101325') self.assertEqual(r.text, '101325')
@async_test @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: async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
x = 30 x = 30
r = await client.get(f"/square/{x}") 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)) self.assertEqual(r.text, str(x * x))
@async_test @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: async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
r = await client.get("department/189350/employee/101325") 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) response = json.loads(r.text)
self.assertEqual({ self.assertEqual({
'department_id': 189350, 'department_id': 189350,