tmp
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import unittest
|
||||
import json
|
||||
import httpx
|
||||
from pwo import async_test
|
||||
from bugis.core import BugisApp, HttpContext
|
||||
from bugis.core import BugisApp, HttpContext, HttpMethod
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
class AsgiTest(unittest.TestCase):
|
||||
@@ -18,6 +20,39 @@ class AsgiTest(unittest.TestCase):
|
||||
print(chunk)
|
||||
await ctx.send_str(200, 'Hello World!')
|
||||
|
||||
@self.app.route(('/foo/bar',), HttpMethod.PUT, recursive=True)
|
||||
async def handle_request(ctx: HttpContext) -> None:
|
||||
async for chunk in ctx.request_body:
|
||||
print(chunk)
|
||||
await ctx.send_str(200, ctx.path)
|
||||
|
||||
@self.app.route(('/foo/*',), HttpMethod.PUT, recursive=True)
|
||||
async def handle_request(ctx: HttpContext, path: Sequence[str]) -> None:
|
||||
async for chunk in ctx.request_body:
|
||||
print(chunk)
|
||||
await ctx.send_str(200, json.dumps(path))
|
||||
|
||||
@self.app.GET('/employee/${employee_id}')
|
||||
async def handle_request(ctx: HttpContext, employee_id: str) -> None:
|
||||
async for chunk in ctx.request_body:
|
||||
print(chunk)
|
||||
await ctx.send_str(200, employee_id)
|
||||
|
||||
@self.app.GET('/square/${x:int}')
|
||||
async def handle_request(ctx: HttpContext, x: int) -> None:
|
||||
async for chunk in ctx.request_body:
|
||||
print(chunk)
|
||||
await ctx.send_str(200, str(x * x))
|
||||
|
||||
@self.app.GET('/department/${department_id:int}/employee/${employee_id:int}')
|
||||
async def handle_request(ctx: HttpContext, department_id: int, employee_id: int) -> None:
|
||||
async for chunk in ctx.request_body:
|
||||
print(chunk)
|
||||
await ctx.send_str(200, json.dumps({
|
||||
'department_id': department_id,
|
||||
'employee_id': employee_id
|
||||
}))
|
||||
|
||||
@async_test
|
||||
async def test_hello(self):
|
||||
transport = httpx.ASGITransport(app=self.app)
|
||||
@@ -38,3 +73,55 @@ class AsgiTest(unittest.TestCase):
|
||||
r = await client.get("/hello4")
|
||||
self.assertEqual(r.status_code, 404)
|
||||
self.assertTrue(len(r.text) == 0)
|
||||
|
||||
@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)
|
||||
response = json.loads(r.text)
|
||||
self.assertEqual(['fizz', 'baz'], response)
|
||||
|
||||
@async_test
|
||||
async def test_foo_bar(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/bar/baz")
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual('/foo/bar/baz', r.text)
|
||||
|
||||
@async_test
|
||||
async def test_employee(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("/employee/101325")
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.text, '101325')
|
||||
|
||||
@async_test
|
||||
async def test_square(self):
|
||||
transport = httpx.ASGITransport(app=self.app)
|
||||
|
||||
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(r.text, str(x * x))
|
||||
|
||||
@async_test
|
||||
async def test_department_employee(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("department/189350/employee/101325")
|
||||
self.assertEqual(r.status_code, 200)
|
||||
response = json.loads(r.text)
|
||||
self.assertEqual({
|
||||
'department_id': 189350,
|
||||
'employee_id': 101325
|
||||
}, response)
|
||||
|
||||
|
@@ -32,13 +32,14 @@ class TreeTest(unittest.TestCase):
|
||||
|
||||
class TestHandler(PathHandler):
|
||||
|
||||
def match(self, subpath: Sequence[str], method: HttpMethod) -> bool:
|
||||
return True
|
||||
|
||||
def handle_request(self, ctx: HttpContext):
|
||||
pass
|
||||
|
||||
self.handlers = [TestHandler() for _ in range(10)]
|
||||
@property
|
||||
def recursive(self) -> bool:
|
||||
return True
|
||||
|
||||
self.handlers = [TestHandler() for _ in range(20)]
|
||||
|
||||
routes: Tuple[Tuple[Tuple[str, ...], Optional[HttpMethod], PathHandler], ...] = (
|
||||
(('home', 'something'), HttpMethod.GET, self.handlers[0]),
|
||||
@@ -49,6 +50,10 @@ class TreeTest(unittest.TestCase):
|
||||
(('home',), HttpMethod.GET, self.handlers[5]),
|
||||
(('home',), HttpMethod.POST, self.handlers[6]),
|
||||
(('home',), None, self.handlers[7]),
|
||||
(('home', '*.md'), None, self.handlers[8]),
|
||||
(('home', 'something', '*', 'blah', '*.md'), None, self.handlers[9]),
|
||||
(('home', 'bar', '*'), None, self.handlers[10]),
|
||||
|
||||
)
|
||||
|
||||
for path, method, handler in routes:
|
||||
@@ -66,9 +71,13 @@ class TreeTest(unittest.TestCase):
|
||||
('http://localhost:127.0.0.1:5432/home', HttpMethod.GET, 5),
|
||||
('http://localhost:127.0.0.1:5432/home', HttpMethod.POST, 6),
|
||||
('http://localhost:127.0.0.1:5432/home', HttpMethod.PUT, 7),
|
||||
('http://localhost:127.0.0.1:5432/home/README.md', HttpMethod.GET, 8),
|
||||
('http://localhost:127.0.0.1:5432/home/something/ciao/blah/README.md', HttpMethod.GET, 9),
|
||||
('http://localhost:127.0.0.1:5432/home/bar/ciao/blah/README.md', HttpMethod.GET, 10),
|
||||
)
|
||||
for url, method, handler_num in cases:
|
||||
with self.subTest(f"{str(method)} {url}"):
|
||||
res = self.tree.get_handler(url, method)
|
||||
self.assertIs(Maybe.of(handler_num).map(self.handlers.__getitem__).or_none(), res)
|
||||
self.assertIs(Maybe.of(handler_num).map(self.handlers.__getitem__).or_none(),
|
||||
Maybe.of_nullable(res).map(lambda it: it[0]).or_none())
|
||||
|
||||
|
Reference in New Issue
Block a user