From: David Kerkeslager Date: Fri, 13 Dec 2019 23:00:12 +0000 (-0500) Subject: Fix a bug in default file not found handler, make creating Requests less painful... X-Git-Url: https://code.kerkeslager.com/?a=commitdiff_plain;ds=sidebyside;h=add99327a18a300ff26d64759921fc0f6847a07b;hp=751093cfdde6021ba36477977540351a307398dd;p=fwx Fix a bug in default file not found handler, make creating Requests less painful, start adding support for POST --- diff --git a/setup.py b/setup.py index 2f28f44..760ed76 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r') as fh: setuptools.setup( name='fwx', - version='0.0.6', + version='0.0.8', author='David Kerkeslager', author_email='kerkeslager+pypi@gmail.com', description="fwx isn't a framework, it's a library.", diff --git a/src/fwx/__init__.py b/src/fwx/__init__.py index c7d6442..e0abb75 100644 --- a/src/fwx/__init__.py +++ b/src/fwx/__init__.py @@ -8,6 +8,7 @@ _Request = collections.namedtuple( ( 'env', 'GET', + 'POST', 'accept', 'accept_encoding', 'accept_language', @@ -24,7 +25,10 @@ _Request = collections.namedtuple( ) class Request(_Request): - def __new__(cls, env, **kwargs): + def __new__(cls, method, path, env=None): + if env is None: + env = {} + errors = [] accept = env.get('HTTP_ACCEPT') @@ -32,8 +36,6 @@ class Request(_Request): accept_language = env.get('HTTP_ACCEPT_LANGUAGE') content = env.get('CONTENT', '') content_type = env.get('CONTENT_TYPE') - method = env.get('REQUEST_METHOD') - path = env.get('PATH_INFO') query = env.get('QUERY_STRING') user_agent = env.get('HTTP_USER_AGENT') @@ -53,17 +55,31 @@ class Request(_Request): except: cookie = http.cookies.SimpleCookie() - try: GET = urllib.parse.parse_qs(query) except: GET = {} errors.append('Unable to parse GET parameters from query string "{}"'.format(query)) + if method == 'POST': + try: + if content_type == 'application/x-www-form-urlencoded': + POST = urllib.parse.parse_qs(content) + else: + POST = {} + errors.append('Unable to parse POST parameters from content string "{}"'.format(content)) + + except: + POST = {} + errors.append('Unable to parse POST parameters from content string "{}"'.format(content)) + + else: + POST = {} + if method == 'GET': parameters = GET elif method == 'POST': - raise Exception('not yet implemented') + parameters = POST else: parameters = None @@ -71,6 +87,7 @@ class Request(_Request): cls, env=env, GET=GET, + POST=POST, accept=accept, accept_encoding=accept_encoding, accept_language=accept_language, @@ -93,7 +110,9 @@ class Request(_Request): return result def _get_request_from_env(env): - return Request(env) + method = env.get('REQUEST_METHOD') + path = env.get('PATH_INFO') + return Request(method, path, env) _Response = collections.namedtuple( 'Response', @@ -203,12 +222,15 @@ class RedirectResponse(_RedirectResponse): return (b'',) def default_file_not_found_handler(request): - return Response('', status=404) + return TextResponse( + 'Path "{}" with query "{}" not found'.format(request.path, request.query), + status=404, + ) def route_on_subpath(**kwargs): routes = kwargs.pop('routes') file_not_found_handler = kwargs.pop( - 'file_not_found_hanlder', + 'file_not_found_handler', default_file_not_found_handler, ) diff --git a/src/test_fwx.py b/src/test_fwx.py index 6f5eada..f437964 100644 --- a/src/test_fwx.py +++ b/src/test_fwx.py @@ -5,19 +5,15 @@ import fwx class RequestTests(unittest.TestCase): def test_GET(self): - request = fwx.Request({ - 'PATH_INFO': '/', + request = fwx.Request('GET', '/', { 'QUERY_STRING': 'foo=bar&baz=qux', - 'REQUEST_METHOD': 'GET', }) self.assertEqual(request.GET['foo'], ['bar']) self.assertEqual(request.GET['baz'], ['qux']) def test_parameters(self): - request = fwx.Request({ - 'PATH_INFO': '/', - 'REQUEST_METHOD': 'GET', + request = fwx.Request('GET', '/', { 'QUERY_STRING': 'foo=bar&baz=qux', }) @@ -144,10 +140,7 @@ class route_on_subpath_Tests(unittest.TestCase): ) self.assertEqual( - router(fwx.Request({ - 'PATH_INFO': '/bar/bara/anne/', - 'REQUEST_METHOD': 'GET', - })).content, + router(fwx.Request('GET', '/bar/bara/anne/')).content, 'bar', ) @@ -161,10 +154,7 @@ class route_on_subpath_Tests(unittest.TestCase): ) self.assertEqual( - router(fwx.Request({ - 'PATH_INFO': '/bar/bara/anne/', - 'REQUEST_METHOD': 'GET', - })).content, + router(fwx.Request('GET', '/bar/bara/anne/')).content, 'bara/anne/', ) @@ -178,12 +168,17 @@ class route_on_subpath_Tests(unittest.TestCase): ) self.assertEqual( - router(fwx.Request({ - 'PATH_INFO': '/bar/bara/anne/', - 'REQUEST_METHOD': 'GET', - })).content, + router(fwx.Request('GET', '/bar/bara/anne/')).content, '/bar/bara/anne/', ) +class default_file_not_found_Tests(unittest.TestCase): + def test_responds(self): + response = fwx.default_file_not_found_handler( + fwx.Request('GET', '/bar/bara/anne/'), + ) + + self.assertNotEqual(response, None) + if __name__ == '__main__': unittest.main()