diff --git a/src/microdot/microdot.py b/src/microdot/microdot.py index 4bfa005..3d84920 100644 --- a/src/microdot/microdot.py +++ b/src/microdot/microdot.py @@ -13,6 +13,7 @@ import time try: from inspect import iscoroutinefunction, iscoroutine + from functools import partial async def invoke_handler(handler, *args, **kwargs): """Invoke a handler and return the result. @@ -23,7 +24,7 @@ try: ret = await handler(*args, **kwargs) else: ret = await asyncio.get_running_loop().run_in_executor( - None, handler, *args, **kwargs) + None, partial(handler, *args, **kwargs)) return ret except ImportError: # pragma: no cover def iscoroutine(coro): diff --git a/tests/test_microdot.py b/tests/test_microdot.py index f99d571..e0bf255 100644 --- a/tests/test_microdot.py +++ b/tests/test_microdot.py @@ -25,6 +25,14 @@ class TestMicrodot(unittest.TestCase): async def index2(req): return 'foo-async' + @app.route('/arg/') + def index3(req, id): + return id + + @app.route('/arg/async/') + async def index4(req, id): + return f'async-{id}' + client = TestClient(app) res = self._run(client.get('/')) @@ -45,6 +53,24 @@ class TestMicrodot(unittest.TestCase): self.assertEqual(res.body, b'foo-async') self.assertEqual(res.json, None) + res = self._run(client.get('/arg/123')) + self.assertEqual(res.status_code, 200) + self.assertEqual(res.headers['Content-Type'], + 'text/plain; charset=UTF-8') + self.assertEqual(res.headers['Content-Length'], '3') + self.assertEqual(res.text, '123') + self.assertEqual(res.body, b'123') + self.assertEqual(res.json, None) + + res = self._run(client.get('/arg/async/123')) + self.assertEqual(res.status_code, 200) + self.assertEqual(res.headers['Content-Type'], + 'text/plain; charset=UTF-8') + self.assertEqual(res.headers['Content-Length'], '9') + self.assertEqual(res.text, 'async-123') + self.assertEqual(res.body, b'async-123') + self.assertEqual(res.json, None) + def test_post_request(self): app = Microdot()