Esempio n. 1
0
 def test_no_request_filters_request_handler_returns_method_true(self):
     """IRequestHandler returns `method` and no IRequestFilters
     are registered. The `method` is forwarded.
     """
     args = ('template.html', {}, 'text/html', 'xhtml')
     request_dispatcher = RequestDispatcher(self.env)
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(0, len(request_dispatcher.filters))
     self.assertEqual(4, len(resp))
     self.assertEqual(args, resp)
     # TODO (1.5.1) remove old API (genshi style)
     args = ('template.html', {}, {'content_type': 'text/html'}, 'xhtml')
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(0, len(request_dispatcher.filters))
     self.assertEqual(4, len(resp))
     self.assertEqual(args, resp)
Esempio n. 2
0
 def test_5arg_post_process_request_request_handler_modifies_method(self):
     """IRequestFilter modifies `method` returned by IRequestHandler.
     """
     self.env.enable_component(self.request_filter['5ArgXml'])
     args = ('template.html', {}, 'text/html', 'xhtml')
     request_dispatcher = RequestDispatcher(self.env)
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(1, len(request_dispatcher.filters))
     self.assertEqual(4, len(resp))
     self.assertEqual(args[:3] + ('xml', ), resp)
     # TODO (1.5.1) remove old API (genshi style)
     args = ('template.html', {}, {'content_type': 'text/html'}, 'xhtml')
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(1, len(request_dispatcher.filters))
     self.assertEqual(4, len(resp))
     self.assertEqual(args[:3] + ('xml', ), resp)
Esempio n. 3
0
File: main.py Progetto: hanotch/trac
 def test_4arg_post_process_request_request_handler_returns_method_false(self):
     """IRequestHandler doesn't return `method` and IRequestFilter doesn't
     accept `method` as an argument. The `method` is set to `None`.
     """
     self.env.enable_component(self.request_filter['4Arg'])
     request_dispatcher = RequestDispatcher(self.env)
     args = ('template.html', {}, 'text/html')
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(1, len(request_dispatcher.filters))
     self.assertEqual(4, len(resp))
     self.assertEqual(args + (None,), resp)
     # TODO (1.5.1) remove old API (genshi style)
     args = ('template.html', {}, {'content_type': 'text/html'})
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(1, len(request_dispatcher.filters))
     self.assertEqual(4, len(resp))
     self.assertEqual(args + (None,), resp)
Esempio n. 4
0
 def test_post_process_request_with_2_args(self):
     request_dispatcher = RequestDispatcher(self.env)
     args = ('template.html', {})
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(2, len(request_dispatcher.filters))
     self.assertEqual(3, len(resp))
     self.assertEqual(2, len(resp[2]))
     self.assertEqual('en_US', resp[2]['domain'])
     self.assertTrue(resp[2]['text'])
Esempio n. 5
0
File: main.py Progetto: hanotch/trac
 def test_5arg_post_process_request_request_handler_returns_method_true(self):
     """IRequestHandler returns `method` and IRequestFilter accepts
     the argument. The `method` argument is passed through IRequestFilter
     implementations.
     """
     self.env.enable_component(self.request_filter['5Arg'])
     request_dispatcher = RequestDispatcher(self.env)
     args = ('template.html', {}, 'text/html', 'xhtml')
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(1, len(request_dispatcher.filters))
     self.assertEqual(4, len(resp))
     self.assertEqual(args, resp)
     # TODO (1.5.1) remove old API (genshi style)
     args = ('template.html', {}, {'content_type': 'text/html'}, 'xhtml')
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(1, len(request_dispatcher.filters))
     self.assertEqual(4, len(resp))
     self.assertEqual(args, resp)
Esempio n. 6
0
 def test_post_process_request_error_handling(self):
     """The post_process_request methods are called with a triple of
     `None` values when an exception is raised in process_request or
     post_process_request, or an empty response is returned by
     process_request.
     """
     request_dispatcher = RequestDispatcher(self.env)
     args = (None, ) * 3
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(2, len(request_dispatcher.filters))
     self.assertEqual((None, None, None), resp)
Esempio n. 7
0
File: main.py Progetto: hanotch/trac
 def test_5arg_post_process_request_request_handler_returns_method_false(self):
     """IRequestHandler doesn't return `method` and IRequestFilter accepts
     `method` as an argument. The `method` is set to `None`.
     """
     self.env.enable_component(self.request_filter['5Arg'])
     request_dispatcher = RequestDispatcher(self.env)
     args = ('template.html', {}, 'text/html')
     resp = request_dispatcher._post_process_request(self.req, *args)
     self.assertEqual(1, len(request_dispatcher.filters))
     self.assertEqual(4, len(resp))
     self.assertEqual(args[:3] + (None,), resp)
Esempio n. 8
0
    def expand_macro(self, formatter, name, content):
        args, kwargs = parse_args(content, strict=False)

        milestones = filter(lambda m: not args or any(map(lambda a: m.name.startswith(a), args)), Milestone.select(self.env))

        req = formatter.req
        template = "listmilestones.html"
        data = {'milestones' : milestones}
        content_type = 'text/html'

        dispatcher = RequestDispatcher(self.env)
        response = dispatcher._post_process_request(req, template, data, content_type)

        # API < 1.1.2 does not return a method.
        if (len(response) == 3):
            template, data, content_type = response
            return Markup(Chrome(self.env).render_template(formatter.req, template, data, content_type=content_type, fragment=True))

        template, data, content_type, method = response
        return Markup(Chrome(self.env).render_template(formatter.req, template, data, content_type=content_type, fragment=True, method=method))
Esempio n. 9
0
    def expand_macro(self, formatter, name, content):
        args, kwargs = parse_args(content, strict=False)

        milestones = filter(lambda m: not args or any(map(lambda a: m.name.startswith(a), args)), Milestone.select(self.env))

        req = formatter.req
        template = "listmilestones.html"
        data = {'milestones' : milestones}
        content_type = 'text/html'

        dispatcher = RequestDispatcher(self.env)
        response = dispatcher._post_process_request(req, template, data, content_type)

        # API < 1.1.2 does not return a method.
        if (len(response) == 3):
            template, data, content_type = response
            return Markup(Chrome(self.env).render_template(formatter.req, template, data, content_type=content_type, fragment=True))

        template, data, content_type, method = response
        return Markup(Chrome(self.env).render_template(formatter.req, template, data, content_type=content_type, fragment=True, method=method))
Esempio n. 10
0
class PostProcessRequestTestCase(unittest.TestCase):
    """Test cases for handling of the optional `method` argument in
    RequestDispatcher._post_process_request."""
    def setUp(self):
        self.env = EnvironmentStub()
        self.req = MockRequest(self.env)
        self.request_dispatcher = RequestDispatcher(self.env)
        self.compmgr = ComponentManager()
        self.env.clear_component_registry()

    def tearDown(self):
        self.env.restore_component_registry()

    def test_no_request_filters_request_handler_returns_method_false(self):
        """IRequestHandler doesn't return `method` and no IRequestFilters
        are registered. The `method` is set to `None`.
        """
        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(0, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args + (None, ), resp)

    def test_no_request_filters_request_handler_returns_method_true(self):
        """IRequestHandler returns `method` and no IRequestFilters
        are registered. The `method` is forwarded.
        """
        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(0, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args, resp)

    def test_4arg_post_process_request_request_handler_returns_method_false(
            self):
        """IRequestHandler doesn't return `method` and IRequestFilter doesn't
        accept `method` as an argument. The `method` is set to `None`.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, handler):
                return handler

            def post_process_request(self, req, template, data, content_type):
                return template, data, content_type

        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args + (None, ), resp)

    def test_4arg_post_process_request_request_handler_returns_method_true(
            self):
        """IRequestHandler returns `method` and IRequestFilter doesn't accept
        the argument. The `method` argument is forwarded over IRequestFilter
        implementations that don't accept the argument.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, handler):
                return handler

            def post_process_request(self, req, template, data, content_type):
                return template, data, content_type

        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args, resp)

    def test_5arg_post_process_request_request_handler_returns_method_false(
            self):
        """IRequestHandler doesn't return `method` and IRequestFilter accepts
        `method` as an argument. The `method` is set to `None`.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, handler):
                return handler

            def post_process_request(self,
                                     req,
                                     template,
                                     data,
                                     content_type,
                                     method=None):
                return template, data, content_type, method

        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args[:3] + (None, ), resp)

    def test_5arg_post_process_request_request_handler_returns_method_true(
            self):
        """IRequestHandler returns `method` and IRequestFilter accepts
        the argument. The `method` argument is passed through IRequestFilter
        implementations.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, handler):
                return handler

            def post_process_request(self,
                                     req,
                                     template,
                                     data,
                                     content_type,
                                     method=None):
                return template, data, content_type, method

        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args, resp)

    def test_5arg_post_process_request_request_handler_adds_method(self):
        """IRequestFilter adds `method` not returned by IRequestHandler.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, handler):
                return handler

            def post_process_request(self,
                                     req,
                                     template,
                                     data,
                                     content_type,
                                     method=None):
                return template, data, content_type, 'xml'

        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args[:3] + ('xml', ), resp)

    def test_5arg_post_process_request_request_handler_modifies_method(self):
        """IRequestFilter modifies `method` returned by IRequestHandler.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, handler):
                return handler

            def post_process_request(self,
                                     req,
                                     template,
                                     data,
                                     content_type,
                                     method=None):
                return template, data, content_type, 'xml'

        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args[:3] + ('xml', ), resp)
Esempio n. 11
0
class PostProcessRequestTestCase(unittest.TestCase):
    """Test cases for handling of the optional `method` argument in
    RequestDispatcher._post_process_request."""
    def setUp(self):
        self.env = EnvironmentStub()
        self.req = MockRequest(self.env)
        self.request_dispatcher = RequestDispatcher(self.env)
        self.compmgr = ComponentManager()
        self.env.clear_component_registry()

    def tearDown(self):
        self.env.restore_component_registry()

    def test_no_request_filters_request_handler_returns_method_false(self):
        """IRequestHandler doesn't return `method` and no IRequestFilters
        are registered. The `method` is set to `None`.
        """
        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(0, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args + (None, ), resp)

    def test_no_request_filters_request_handler_returns_method_true(self):
        """IRequestHandler returns `method` and no IRequestFilters
        are registered. The `method` is forwarded.
        """
        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(0, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args, resp)

    def test_4arg_post_process_request_request_handler_returns_method_false(
            self):
        """IRequestHandler doesn't return `method` and IRequestFilter doesn't
        accept `method` as an argument. The `method` is set to `None`.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, req, handler):
                return handler

            def post_process_request(self, req, template, data, content_type):
                return template, data, content_type

        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args + (None, ), resp)

    def test_4arg_post_process_request_request_handler_returns_method_true(
            self):
        """IRequestHandler returns `method` and IRequestFilter doesn't accept
        the argument. The `method` argument is forwarded over IRequestFilter
        implementations that don't accept the argument.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, req, handler):
                return handler

            def post_process_request(self, req, template, data, content_type):
                return template, data, content_type

        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args, resp)

    def test_5arg_post_process_request_request_handler_returns_method_false(
            self):
        """IRequestHandler doesn't return `method` and IRequestFilter accepts
        `method` as an argument. The `method` is set to `None`.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, req, handler):
                return handler

            def post_process_request(self,
                                     req,
                                     template,
                                     data,
                                     content_type,
                                     method=None):
                return template, data, content_type, method

        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args[:3] + (None, ), resp)

    def test_5arg_post_process_request_request_handler_returns_method_true(
            self):
        """IRequestHandler returns `method` and IRequestFilter accepts
        the argument. The `method` argument is passed through IRequestFilter
        implementations.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, req, handler):
                return handler

            def post_process_request(self,
                                     req,
                                     template,
                                     data,
                                     content_type,
                                     method=None):
                return template, data, content_type, method

        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args, resp)

    def test_5arg_post_process_request_request_handler_adds_method(self):
        """IRequestFilter adds `method` not returned by IRequestHandler.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, req, handler):
                return handler

            def post_process_request(self,
                                     req,
                                     template,
                                     data,
                                     content_type,
                                     method=None):
                return template, data, content_type, 'xml'

        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args[:3] + ('xml', ), resp)

    def test_5arg_post_process_request_request_handler_modifies_method(self):
        """IRequestFilter modifies `method` returned by IRequestHandler.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)

            def pre_process_request(self, req, handler):
                return handler

            def post_process_request(self,
                                     req,
                                     template,
                                     data,
                                     content_type,
                                     method=None):
                return template, data, content_type, 'xml'

        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args[:3] + ('xml', ), resp)

    def test_redirect_on_permission_error(self):
        """The post_process_request method can redirect during exception
        handling from an exception raised in process_request.
        """
        class RedirectOnPermissionErrorStub(Component):
            implements(IRequestHandler, IRequestFilter)

            def match_request(self, req):
                return re.match(r'/perm-error', req.path_info)

            def process_request(self, req):
                req.entered_process_request = True
                raise PermissionError("No permission to view")

            def pre_process_request(self, req, handler):
                return handler

            def post_process_request(self, req, template, data, content_type):
                if (template, data, content_type) == (None, None, None):
                    req.entered_post_process_request = True
                    req.redirect(req.href('/redirect-target'))
                return template, data, content_type

        dispatcher = RequestDispatcher(self.env)
        req = MockRequest(self.env, method='GET', path_info='/perm-error')
        req.entered_process_request = False
        req.entered_post_process_request = False

        try:
            dispatcher.dispatch(req)
        except RequestDone:
            pass
        else:
            self.fail("RequestDone not raised")

        self.assertTrue(req.entered_process_request)
        self.assertTrue(req.entered_post_process_request)
Esempio n. 12
0
class PostProcessRequestTestCase(unittest.TestCase):
    """Test cases for handling of the optional `method` argument in
    RequestDispatcher._post_process_request."""

    def setUp(self):
        self.env = EnvironmentStub()
        self.req = Mock()
        self.request_dispatcher = RequestDispatcher(self.env)
        self.compmgr = ComponentManager()
        # Make sure we have no external components hanging around in the
        # component registry
        self.old_registry = ComponentMeta._registry
        ComponentMeta._registry = {}

    def tearDown(self):
        # Restore the original component registry
        ComponentMeta._registry = self.old_registry

    def test_no_request_filters_request_handler_returns_method_false(self):
        """IRequestHandler doesn't return `method` and no IRequestFilters
        are registered. The `method` is set to `None`.
        """
        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(0, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args + (None,), resp)

    def test_no_request_filters_request_handler_returns_method_true(self):
        """IRequestHandler returns `method` and no IRequestFilters
        are registered. The `method` is forwarded.
        """
        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(0, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args, resp)

    def test_4arg_post_process_request_request_handler_returns_method_false(self):
        """IRequestHandler doesn't return `method` and IRequestFilter doesn't
        accept `method` as an argument. The `method` is set to `None`.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)
            def pre_process_request(self, handler):
                return handler
            def post_process_request(self, req, template, data, content_type):
                return template, data, content_type
        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args + (None,), resp)

    def test_4arg_post_process_request_request_handler_returns_method_true(self):
        """IRequestHandler returns `method` and IRequestFilter doesn't accept
        the argument. The `method` argument is forwarded over IRequestFilter
        implementations that don't accept the argument.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)
            def pre_process_request(self, handler):
                return handler
            def post_process_request(self, req, template, data, content_type):
                return template, data, content_type
        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args, resp)

    def test_5arg_post_process_request_request_handler_returns_method_false(self):
        """IRequestHandler doesn't return `method` and IRequestFilter accepts
        `method` as an argument. The `method` is set to `None`.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)
            def pre_process_request(self, handler):
                return handler
            def post_process_request(self, req, template, data,
                                     content_type, method=None):
                return template, data, content_type, method
        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args[:3] + (None,), resp)

    def test_5arg_post_process_request_request_handler_returns_method_true(self):
        """IRequestHandler returns `method` and IRequestFilter accepts
        the argument. The `method` argument is passed through IRequestFilter
        implementations.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)
            def pre_process_request(self, handler):
                return handler
            def post_process_request(self, req, template, data,
                                     content_type, method=None):
                return template, data, content_type, method
        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args, resp)

    def test_5arg_post_process_request_request_handler_adds_method(self):
        """IRequestFilter adds `method` not returned by IRequestHandler.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)
            def pre_process_request(self, handler):
                return handler
            def post_process_request(self, req, template, data,
                                     content_type, method=None):
                return template, data, content_type, 'xml'
        args = ('template.html', {}, 'text/html')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args[:3] + ('xml',), resp)

    def test_5arg_post_process_request_request_handler_modifies_method(self):
        """IRequestFilter modifies `method` returned by IRequestHandler.
        """
        class RequestFilter(Component):
            implements(IRequestFilter)
            def pre_process_request(self, handler):
                return handler
            def post_process_request(self, req, template, data,
                                     content_type, method=None):
                return template, data, content_type, 'xml'
        args = ('template.html', {}, 'text/html', 'xhtml')
        resp = self.request_dispatcher._post_process_request(self.req, *args)
        self.assertEqual(1, len(self.request_dispatcher.filters))
        self.assertEqual(4, len(resp))
        self.assertEqual(args[:3] + ('xml',), resp)