Ejemplo 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()
     )
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo 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)
Ejemplo n.º 10
0
 def test_content_setter_with_empty_dictionary(self):
     response = Response()
     self.assertEqual(response.content, {})
Ejemplo n.º 11
0
 def test_init(self):
     response = Response()
     self.assertEqual(response._content, {})