From 20138ee85f12633aac64f78139b1c764913fca39 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Sat, 17 Jun 2017 09:47:58 -0700 Subject: [PATCH] add match_info to request --- sanic/request.py | 5 +++++ tests/test_requests.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/sanic/request.py b/sanic/request.py index a5d204a8..3cc9c10b 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -178,6 +178,11 @@ class Request(dict): def content_type(self): return self.headers.get('Content-Type', DEFAULT_HTTP_CONTENT_TYPE) + @property + def match_info(self): + """return matched info after resolving route""" + return self.app.router.get(self)[2] + @property def path(self): return self._parsed_url.path.decode('utf-8') diff --git a/tests/test_requests.py b/tests/test_requests.py index d61d4d55..2351a3b0 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -211,6 +211,19 @@ def test_content_type(): assert response.text == 'application/json' +def test_match_info(): + app = Sanic('test_match_info') + + @app.route('/api/v1/user//') + async def handler(request, user_id): + return json(request.match_info) + + request, response = app.test_client.get('/api/v1/user/sanic_user/') + + assert request.match_info == {"user_id": "sanic_user"} + assert json_loads(response.text) == {"user_id": "sanic_user"} + + # ------------------------------------------------------------ # # POST # ------------------------------------------------------------ #