Esempio n. 1
0
 def test_append_request(self):
     data = {'url': '/api', 'method': 'GET'}
     request = Request(**data)
     response = Response()
     response.append_request(request)
     self.assertEqual(
         response.content['request'], request.to_representation()
     )
Esempio n. 2
0
    def process_request(self, request):
        """
        Handle received request from user.

        :param request: request from user.
        """
        logger.info("\"{method} {url}\" args={args}".format(
            method=request.method, url=request.url, args=request.args))
        response = Response()

        try:
            url = self.extract_url(request)
            handler, args, kwargs = self.search_handler(request, url)

            # Invoke handler for request
            if handler:

                for middleware in self.middlewares:
                    middleware.process_request(request, handler)

                # Search serializer for response
                format = request.get_argument('format')
                serializer = handler.get_renderer(format, *args, **kwargs)

                response.content = handler.dispatch(request, *args, **kwargs)
            else:
                raise NotSpecifiedHandler()
        except BaseAPIException as exc:
            logger.exception(exc)
            response.wrap_exception(exc)
            serializer = JSONRenderer()

        response.append_request(request)
        return serializer.render(response.content)
Esempio n. 3
0
    def process_request(self, request):
        """
        Handle received request from user.

        :param request: request from user.
        """
        logger.info("\"{method} {url}\" args={args}".format(
            method=request.method,
            url=request.url,
            args=request.args)
        )
        response = Response()

        try:
            url = self.extract_url(request)
            handler, args, kwargs = self.search_handler(request, url)

            # Invoke handler for request
            if handler:

                for middleware in self.middlewares:
                    middleware.process_request(request, handler)

                # Search serializer for response
                format = request.get_argument('format')
                serializer = handler.get_renderer(format, *args, **kwargs)

                response.content = handler.dispatch(request, *args, **kwargs)
            else:
                raise NotSpecifiedHandler()
        except BaseAPIException as exc:
            logger.exception(exc)
            response.wrap_exception(exc)
            serializer = JSONRenderer()

        response.append_request(request)
        return serializer.render(response.content)
Esempio n. 4
0
 def test_wrap_exception(self):
     exception = BaseAPIException()
     response = Response()
     response.wrap_exception(exception)
     self.assertIn('detail', response._content)
     self.assertEqual(response._content['detail'], exception.detail)
Esempio n. 5
0
 def test_wrap_exception(self):
     exception = BaseAPIException()
     response = Response()
     response.wrap_exception(exception)
     self.assertIn('detail', response._content)
     self.assertEqual(response._content['detail'], exception.detail)
Esempio n. 6
0
 def test_append_request_with_undefined_event_name(self):
     options = {'url': '/api', 'method': 'GET'}
     request = Request(**options)
     response = Response()
     response.append_request(request)
     self.assertEqual(response.content['event_name'], request.event_name)
Esempio n. 7
0
 def test_content_setter_with_specified_list(self):
     response = Response()
     response.content = options = [1, 2, 3, 4, 5]
     self.assertEqual(response._content['data'], options)
Esempio n. 8
0
 def test_content_setter_with_specified_dictionary_2(self):
     response = Response()
     response.content = options = {'key': 'value'}
     self.assertEqual(response._content['data'], options)
Esempio n. 9
0
 def test_content_setter_with_specified_dictionary(self):
     response = Response()
     response.content = options = {'detail': 'my description'}
     self.assertEqual(response._content['data'], options)
Esempio n. 10
0
 def test_content_setter_with_empty_dictionary(self):
     response = Response()
     self.assertEqual(response.content, {})
Esempio n. 11
0
 def test_init(self):
     response = Response()
     self.assertEqual(response._content, {})