From 067d5b39e8fb4e7fb9587901d55e94b7c4c1ce69 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Tue, 22 Oct 2019 22:44:36 -0400 Subject: [PATCH] Start fleshing out the request --- phial.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++---- test_phial.py | 19 ++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/phial.py b/phial.py index bc07b5f..5388c05 100644 --- a/phial.py +++ b/phial.py @@ -1,13 +1,67 @@ import collections import json +import urllib.parse -Request = collections.namedtuple( +_Request = collections.namedtuple( 'Request', ( - 'environ', + 'env', + 'GET', + 'accept', + 'accept_encoding', + 'accept_language', + 'content', + 'content_length', + 'content_type', + 'cookies', + 'method', + 'path', + 'parameters', + 'query', + 'user_agent', ) ) +class Request(_Request): + def __new__(cls, env): + accept = env.get('HTTP_ACCEPT') + accept_encoding = env.get('HTTP_ACCEPT_ENCODING') + accept_language = env.get('HTTP_ACCEPT_LANGUAGE') + content = env.get('CONTENT', '') + content_length = env.get('CONTENT_LENGTH') + content_type = env.get('CONTENT_TYPE') + cookies = env.get('HTTP_COOKIE') + method = env.get('REQUEST_METHOD') + path = env.get('PATH_INFO') + query = env.get('QUERY_STRING') + user_agent = env.get('HTTP_USER_AGENT') + + GET = urllib.parse.parse_qs(query) + + if method == 'GET': + parameters = GET + + result = super().__new__( + cls, + env=env, + GET=GET, + accept=accept, + accept_encoding=accept_encoding, + accept_language=accept_language, + content = content, + content_length = content_length, + content_type = content_type, + cookies=cookies, + method=method, + parameters=parameters, + path=path, + query=query, + user_agent=user_agent, + ) + + result.subpath = path + return result + _Response = collections.namedtuple( 'Response', ( @@ -137,8 +191,8 @@ def _get_content(response): return content def App(handler): - def app(environ, start_fn): - response = handler(Request(environ)) + def app(env, start_fn): + response = handler(Request(env)) start_fn(_get_status(response), _get_headers(response)) return _get_content(response) diff --git a/test_phial.py b/test_phial.py index 8d7498d..672be96 100644 --- a/test_phial.py +++ b/test_phial.py @@ -3,6 +3,25 @@ from unittest import mock import phial +class RequestTests(unittest.TestCase): + def test_GET(self): + request = phial.Request({ + 'REQUEST_METHOD': 'GET', + 'QUERY_STRING': 'foo=bar&baz=qux', + }) + + self.assertEqual(request.GET['foo'], ['bar']) + self.assertEqual(request.GET['baz'], ['qux']) + + def test_parameters(self): + request = phial.Request({ + 'REQUEST_METHOD': 'GET', + 'QUERY_STRING': 'foo=bar&baz=qux', + }) + + self.assertEqual(request.parameters['foo'], ['bar']) + self.assertEqual(request.parameters['baz'], ['qux']) + class ResponseTests(unittest.TestCase): def test_content_can_be_positional_argument(self): response = phial.Response('Hello, world\n', content_type='text/plain') -- 2.20.1