def handle_request(self, environ, response): method = environ.get('REQUEST_METHOD', 'GET') path = environ.get('PATH_INFO', '/') if path == '/': path = '/index.html' agent = environ.get('HTTP_USER_AGENT', '') group = 'mobile' if match_mobile.match(agent) else 'default' response = Response(response) log.debug('Handling request {} {} for agent {} ({})'.format(method, path, agent, group)) try: if method == 'GET': content = app.get_file(path, group) if content: return response.reply_with_file(path, content) content = app.route(path) if method == 'POST': post = self.parse_post_parameters(environ) content = app.route(path, **post) if content is None: return response.reply_with_not_found() return response.reply_with_json(content) except ValueError as e: return response.reply_with_client_error(e) except TypeError: return response.reply_with_method_not_allowed(method) except ServerError as e: return response.reply_with_server_error(e)
def test_parser_with_type_fails_with_invalid_float(self): with self.assertRaises(ValueError): app.route('/typed/route/1/2.1.3')
def test_parser_finds_one_param(self): result = app.route('/test/value') self.assertEquals('value', result)
def test_route_without_mandatory_argument_returns_none(self): self.assertIsNone(app.route('/another/one'))
def test_parser_with_type_returns_int_and_float_value(self): result1, result2 = app.route('/typed/route/1/2.1') self.assertEquals(1, result1) self.assertEquals(2.1, result2)
def test_route_with_two_optional_arguments_works_as_expected(self): one, two, three, fourth = app.route('/another/one/two') self.assertEquals('one', one) self.assertEquals('two', two) self.assertIsNone(three) self.assertIsNone(fourth)
def test_route_with_optional_arguments(self): one, two, three, fourth = app.route('/another/one/two/three') self.assertEquals('one', one) self.assertEquals('two', two) self.assertEquals('three', three) self.assertIsNone(fourth)
def test_route_with_kwargs(self): result = app.route('/route/with/kwargs', argument='value') self.assertEquals('value', result)
def test_parser_finds_two_params(self): result1, result2 = app.route('/other/route/value1/value2') self.assertEquals('value1', result1) self.assertEquals('value2', result2)
def test_parser_finds_two_params_with_one_supported_ignores_second(self): result = app.route('/test/value1/value2') self.assertEquals('value1', result)