Exemplo n.º 1
0
    def request(self, **request):
        """
        Similar to parent class, but returns the request object as soon as it
        has created it.
        """
        environ = {
            'HTTP_COOKIE':       self.cookies.output(header='', sep='; '),
            'PATH_INFO':         '/',
            'QUERY_STRING':      '',
            'REMOTE_ADDR':       '127.0.0.1',
            'REQUEST_METHOD':    'GET',
            'SCRIPT_NAME':       '',
            'SERVER_NAME':       'testserver',
            'SERVER_PORT':       '80',
            'SERVER_PROTOCOL':   'HTTP/1.1',
            'wsgi.version':      (1,0),
            'wsgi.url_scheme':   'http',
            'wsgi.errors':       self.errors,
            'wsgi.multiprocess': True,
            'wsgi.multithread':  False,
            'wsgi.run_once':     False,
        }
        environ.update(self.defaults)
        environ.update(request)
        request = WSGIRequest(environ)

        # Add request.user.
        handler = BaseHandler()
        handler.load_middleware()
        for middleware_method in handler._request_middleware:
            if middleware_method(request):
                raise Exception("Couldn't create request mock object - "
                                "request middleware returned a response")

        return request
    def test_is_authenticated(self):
        auth = DjangoAuthentication()
        request = self.factory.get("/")
        # load the middleware since django doesnt...
        from django.core.handlers.base import BaseHandler

        handler = BaseHandler()
        handler.load_middleware()
        for middleware_method in handler._request_middleware:
            self.assertIsNone(middleware_method(request))

        # not logged in should fail
        self.assertFalse(auth.is_authenticated(request))
        # not logged in should be AnonymousUser
        self.assertEquals(auth.get_identifier(request), "AnonymousUser")

        # login test user
        from django.contrib.auth import authenticate, login

        user = authenticate(username="******", password="******")
        self.assertIsNotNone(user)
        login(request, user)

        # logged in should succeed
        self.assertTrue(auth.is_authenticated(request))
        # should be janeroe logged in
        self.assertEquals(auth.get_identifier(request), "janeroe")
Exemplo n.º 3
0
class MessageSenderWorker(QueueProcessingWorker):
    def __init__(self):
        # type: () -> None
        super().__init__()
        self.redis_client = get_redis_client()
        self.handler = BaseHandler()
        self.handler.load_middleware()

    def consume(self, event):
        # type: (Mapping[str, Any]) -> None
        server_meta = event['server_meta']

        environ = {
            'REQUEST_METHOD': 'SOCKET',
            'SCRIPT_NAME': '',
            'PATH_INFO': '/json/messages',
            'SERVER_NAME': '127.0.0.1',
            'SERVER_PORT': 9993,
            'SERVER_PROTOCOL': 'ZULIP_SOCKET/1.0',
            'wsgi.version': (1, 0),
            'wsgi.input': StringIO(),
            'wsgi.errors': sys.stderr,
            'wsgi.multithread': False,
            'wsgi.multiprocess': True,
            'wsgi.run_once': False,
            'zulip.emulated_method': 'POST'
        }

        if 'socket_user_agent' in event['request']:
            environ['HTTP_USER_AGENT'] = event['request']['socket_user_agent']
            del event['request']['socket_user_agent']

        # We're mostly using a WSGIRequest for convenience
        environ.update(server_meta['request_environ'])
        request = WSGIRequest(environ)
        # Note: If we ever support non-POST methods, we'll need to change this.
        request._post = event['request']
        request.csrf_processing_done = True

        user_profile = get_user_profile_by_id(server_meta['user_id'])
        request._cached_user = user_profile

        resp = self.handler.get_response(request)
        server_meta['time_request_finished'] = time.time()
        server_meta['worker_log_data'] = request._log_data

        resp_content = resp.content.decode('utf-8')
        response_data = ujson.loads(resp_content)
        if response_data['result'] == 'error':
            check_and_send_restart_signal()

        result = {'response': response_data, 'req_id': event['req_id'],
                  'server_meta': server_meta}

        redis_key = req_redis_key(event['req_id'])
        self.redis_client.hmset(redis_key, {'status': 'complete',
                                            'response': resp_content})

        queue_json_publish(server_meta['return_queue'], result,
                           respond_send_message)
Exemplo n.º 4
0
    def dummy_request(self):
        """
        Construct a HttpRequest object that is, as far as possible, representative of ones that would
        receive this page as a response. Used for previewing / moderation and any other place where we
        want to display a view of this page in the admin interface without going through the regular
        page routing logic.
        """
        url = self.full_url
        if url:
            url_info = urlparse(url)
            hostname = url_info.netloc
            path = url_info.path
            port = url_info.port or 80
        else:
            hostname = 'example.com'
            path = '/'
            port = 80

        request = WSGIRequest({
            'REQUEST_METHOD': 'GET',
            'PATH_INFO': path,
            'SERVER_NAME': hostname,
            'SERVER_PORT': port,
            'wsgi.input': StringIO(),
        })

        # Apply middleware to the request - see http://www.mellowmorning.com/2011/04/18/mock-django-request-for-testing/
        handler = BaseHandler()
        handler.load_middleware()
        for middleware_method in handler._request_middleware:
            if middleware_method(request):
                raise Exception("Couldn't create request mock object - "
                                "request middleware returned a response")
        return request
Exemplo n.º 5
0
    def request(self, **request_args):
        """Constructs a generic request object, INCLUDING middleware modifications."""

        from django.core import urlresolvers


        request = RequestFactory.request(self, **request_args)
        ###pprint.pprint(request)

        handler = BaseHandler()

        handler.load_middleware()

        for middleware_method in handler._request_middleware:
            #print("APPLYING REQUEST MIDDLEWARE ", middleware_method, file=sys.stderr)
            if middleware_method(request):
                raise Exception("Couldn't create request mock object - "
                                "request middleware returned a response")

        urlconf = settings.ROOT_URLCONF
        urlresolvers.set_urlconf(urlconf)
        resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)

        callback, callback_args, callback_kwargs = resolver.resolve(
                            request.path_info)

        # Apply view middleware
        for middleware_method in handler._view_middleware:
            #print("APPLYING VIEW MIDDLEWARE ", middleware_method, file=sys.stderr)
            response = middleware_method(request, callback, callback_args, callback_kwargs)
            if response:
                raise Exception("Couldn't create request mock object - "
                                "view middleware returned a response")

        return request
Exemplo n.º 6
0
 def _prepare_request(self, request, page, user, lang, use_middlewares, use_toolbar=False):
     request.current_page = SimpleLazyObject(lambda: page)
     if not user:
         if self._login_context:
             user = self._login_context.user
         else:
             user = AnonymousUser()
     request.user = user
     request.session = {}
     request.cookies = SimpleCookie()
     request.errors = StringIO()
     request.LANGUAGE_CODE = lang
     # Let's use middleware in case requested, otherwise just use CMS toolbar if needed
     if use_middlewares:
         handler = BaseHandler()
         handler.load_middleware()
         for middleware_method in handler._request_middleware:
             if middleware_method(request):
                 raise Exception(u'Couldn\'t create request mock object -'
                                 u'request middleware returned a response')
     elif use_toolbar:
         from cms.middleware.toolbar import ToolbarMiddleware
         mid = ToolbarMiddleware()
         mid.process_request(request)
     return request
Exemplo n.º 7
0
class MessageSenderWorker(QueueProcessingWorker):
    def __init__(self):
        # type: () -> None
        super().__init__()
        self.redis_client = get_redis_client()
        self.handler = BaseHandler()
        self.handler.load_middleware()

    def consume(self, event):
        # type: (Mapping[str, Any]) -> None
        server_meta = event['server_meta']

        environ = {
            'REQUEST_METHOD': 'SOCKET',
            'SCRIPT_NAME': '',
            'PATH_INFO': '/json/messages',
            'SERVER_NAME': '127.0.0.1',
            'SERVER_PORT': 9993,
            'SERVER_PROTOCOL': 'ZULIP_SOCKET/1.0',
            'wsgi.version': (1, 0),
            'wsgi.input': StringIO(),
            'wsgi.errors': sys.stderr,
            'wsgi.multithread': False,
            'wsgi.multiprocess': True,
            'wsgi.run_once': False,
            'zulip.emulated_method': 'POST'
        }

        if 'socket_user_agent' in event['request']:
            environ['HTTP_USER_AGENT'] = event['request']['socket_user_agent']
            del event['request']['socket_user_agent']

        # We're mostly using a WSGIRequest for convenience
        environ.update(server_meta['request_environ'])
        request = WSGIRequest(environ)
        # Note: If we ever support non-POST methods, we'll need to change this.
        request._post = event['request']
        request.csrf_processing_done = True

        user_profile = get_user_profile_by_id(server_meta['user_id'])
        request._cached_user = user_profile

        resp = self.handler.get_response(request)
        server_meta['time_request_finished'] = time.time()
        server_meta['worker_log_data'] = request._log_data

        resp_content = resp.content.decode('utf-8')
        response_data = ujson.loads(resp_content)
        if response_data['result'] == 'error':
            check_and_send_restart_signal()

        result = {'response': response_data, 'req_id': event['req_id'],
                  'server_meta': server_meta}

        redis_key = req_redis_key(event['req_id'])
        self.redis_client.hmset(redis_key, {'status': 'complete',
                                            'response': resp_content})

        queue_json_publish(server_meta['return_queue'], result,
                           respond_send_message)
Exemplo n.º 8
0
def test_error_handlers(rf):
    """
    Test that SHOOP_FRONT_INSTALL_ERROR_HANDLERS installs error handlers
    without overwriting possible custom ones.
    """
    with override_settings(
            DEBUG=False,
            SHOOP_FRONT_INSTALL_ERROR_HANDLERS=True,
            MIDDLEWARE_CLASSES=[]
    ):
        with replace_urls([
            url("^aaargh/", errorful_view)
        ], {"handler404": four_oh_four}):
            resolver = get_resolver(None)
            urlconf = resolver.urlconf_module
            install_error_handlers()
            assert callable(urlconf.handler500)  # We get a new 500 handler
            assert urlconf.handler404 == four_oh_four  # Our custom 404 handler didn't get overwritten
            handler = BaseHandler()
            handler.load_middleware()
            # Test 500
            response = handler.get_response(rf.get("/aaargh/"))
            assert response.status_code == 500  # Uh oh!
            assert isinstance(response, TemplateResponse)  # Looks good!
            assert response.template_name.startswith("shoop")  # Woop!
            # Test 404
            response = handler.get_response(rf.get("/another_castle/"))
            assert response.status_code == 200  # Our custom 404 handler made it a 200!
            assert b"flesh wound" in response.content
Exemplo n.º 9
0
 def request(self, **request):
     """
     Similar to parent class, but returns the request object as soon as it
     has created it.
     """
     environ = {
         'HTTP_COOKIE': self.cookies,
         'PATH_INFO': '/',
         'QUERY_STRING': '',
         'REQUEST_METHOD': 'GET',
         'SCRIPT_NAME': '',
         'SERVER_NAME': 'testserver',
         'SERVER_PORT': 80,
         'SERVER_PROTOCOL': 'HTTP/1.1',
     }
     environ.update(self.defaults)
     environ.update(request)
     request = WSGIRequest(environ)
     handler = BaseHandler()
     handler.load_middleware()
     for middleware_method in handler._request_middleware:
         if middleware_method(request):
             raise Exception("Couldn't create request mock object - "
                             "request middleware returned a response")
     return request
Exemplo n.º 10
0
    def request(self, **request):
        """
        Similar to parent class, but returns the request object as soon as it
        has created it.
        """
        environ = {
            'HTTP_COOKIE': self.cookies,
            'PATH_INFO': '/',
            'QUERY_STRING': '',
            'REQUEST_METHOD': 'GET',
            'SCRIPT_NAME': '',
            'SERVER_NAME': 'testserver',
            'SERVER_PORT': 80,
            'SERVER_PROTOCOL': 'HTTP/1.1',
        }
        environ.update(self.defaults)
        environ.update(request)
        request = WSGIRequest(environ)

        # Add request.user.
        handler = BaseHandler()
        handler.load_middleware()
        for middleware_method in handler._request_middleware:
            if middleware_method(request):
                raise Exception("Couldn't create request mock object - "
                                "request middleware returned a response")

        return request
Exemplo n.º 11
0
    def process_response(self, request, response):
        try:
            if hasattr(response, 'render') and callable(response.render):
                return response.render()
            return response
        except Exception:
            import sys
            from django.conf import settings
            from django.core.handlers.base import BaseHandler
            from django.core import signals
            from django.core import urlresolvers

            urlconf = settings.ROOT_URLCONF
            urlresolvers.set_urlconf(urlconf)
            resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)
            receivers = signals.got_request_exception.send(
                sender=self.__class__,
                request=request,
                )
            handler = BaseHandler()
            return handler.handle_uncaught_exception(
                request,
                resolver,
                sys.exc_info(),
                )
Exemplo n.º 12
0
 def request(self, **request):
     handler = BaseHandler()
     handler.load_middleware()
     r = WSGIRequest(self._base_environ(**request))
     for middleware_method in handler._request_middleware:
         if middleware_method(r):
             raise Exception('error on middleware {}'.format(middleware_method))
     return r
Exemplo n.º 13
0
 def request(self, **request):
     request = super(RequestFactory, self).request(**request)
     handler = BaseHandler()
     handler.load_middleware()
     for middleware_method in handler._request_middleware:
         if middleware_method(request):
             raise Exception('request middleware returned a response')
     return request
Exemplo n.º 14
0
 def request(self, **request):
     request = RequestFactory.request(self, **request)
     handler = BaseHandler()
     handler.load_middleware()
     for mw in handler._request_middleware:
         if mw(request):
             raise Exception('middleware returned a reponse')
     return request
Exemplo n.º 15
0
 def send(self, request, stream=False, timeout=None, verify=True,
          cert=None, proxies=None):
     handler = BaseHandler()
     handler.load_middleware()
     wsgi_request = self.prepared_request_to_wsgi_request(request)
     http_response = handler.get_response(wsgi_request)
     requests_response = self.http_response_to_response(http_response, request)
     return requests_response
Exemplo n.º 16
0
 def request(self, **request):
     request = RequestFactory.request(self, **request)
     handler = BaseHandler()
     handler.load_middleware()
     for middleware_method in handler._request_middleware:
         if middleware_method(request):
             raise Exception("Couldn't create request mock object - "
                             "request middleware returned a response")
     return request
Exemplo n.º 17
0
 def request(self, **request):
     "Construct a generic request object."
     request = RequestFactory.request(self, **request)
     handler = BaseHandler()
     handler.load_middleware()
     for middleware_method in handler._request_middleware:  # pylint: disable=protected-access
         if middleware_method(request):
             raise Exception("Couldn't create request mock object - "
                             "request middleware returned a response")
     return request
Exemplo n.º 18
0
 def request(self, **request):
     "Construct a generic request object."
     request = RequestFactory.request(self, **request)
     handler = BaseHandler()
     handler.load_middleware()
     for middleware_method in handler._request_middleware:  # pylint: disable=protected-access
         if middleware_method(request):
             raise Exception("Couldn't create request mock object - "
                             "request middleware returned a response")
     return request
Exemplo n.º 19
0
 def request(self, **request):
     # https://gist.github.com/tschellenbach/925270
     request = RequestFactory.request(self, **request)
     handler = BaseHandler()
     handler.load_middleware()
     for middleware_method in handler._request_middleware:
         if middleware_method(request):
             raise Exception("Couldn't create request mock object - "
                             "request middleware returned a response")
     return request
Exemplo n.º 20
0
 def request(self, **request):
     #https://gist.github.com/tschellenbach/925270
     request = RequestFactory.request(self, **request)  
     handler = BaseHandler()  
     handler.load_middleware()  
     for middleware_method in handler._request_middleware:  
         if middleware_method(request):  
             raise Exception("Couldn't create request mock object - "  
                             "request middleware returned a response")  
     return request 
Exemplo n.º 21
0
 def request(self, **request):
     "Construct a generic request object."
     request["REQUEST"] = dict()
     request = RequestFactory.request(self, **request)
     handler = BaseHandler()
     handler.load_middleware()
     for middleware_method in handler._request_middleware:
         if middleware_method(request):
             raise Exception("Couldn't create request mock object - " "request middleware returned a response")
     return request
Exemplo n.º 22
0
 def request(self, **request):
     "Construct a generic request object."
     request['REQUEST'] = dict()
     request = RequestFactory.request(self, **request)
     handler = BaseHandler()
     handler.load_middleware()
     for middleware_method in handler._request_middleware:
         if middleware_method(request):
             raise Exception("Couldn't create request mock object - "
                             "request middleware returned a response")
     return request
Exemplo n.º 23
0
 def request(self, **request):
     "Construct a generic request object."
     request = RequestFactory.request(self, **request)
     handler = BaseHandler()
     handler.load_middleware()
     request.csrf_processing_done = True
     for middleware_method in handler._request_middleware:
         if middleware_method(request):
             raise Exception("Couldn't create request mock object - "
                             "request middleware returned a response")
     return request
Exemplo n.º 24
0
class MessageSenderWorker(QueueProcessingWorker):
    def __init__(self):
        # type: () -> None
        super(MessageSenderWorker, self).__init__()
        self.redis_client = get_redis_client()
        self.handler = BaseHandler()
        self.handler.load_middleware()

    def consume(self, event):
        # type: (Mapping[str, Any]) -> None
        server_meta = event['server_meta']

        environ = {
            'REQUEST_METHOD': 'SOCKET',
            'SCRIPT_NAME': '',
            'PATH_INFO': '/json/messages',
            'SERVER_NAME': 'localhost',
            'SERVER_PORT': 9993,
            'SERVER_PROTOCOL': 'ZULIP_SOCKET/1.0',
            'wsgi.version': (1, 0),
            'wsgi.input': StringIO(),
            'wsgi.errors': sys.stderr,
            'wsgi.multithread': False,
            'wsgi.multiprocess': True,
            'wsgi.run_once': False,
            'zulip.emulated_method': 'POST'
        }
        # We're mostly using a WSGIRequest for convenience
        environ.update(server_meta['request_environ'])
        request = WSGIRequest(environ)
        request._request = event['request']
        request.csrf_processing_done = True

        user_profile = get_user_profile_by_id(server_meta['user_id'])
        request._cached_user = user_profile

        resp = self.handler.get_response(request)
        server_meta['time_request_finished'] = time.time()
        server_meta['worker_log_data'] = request._log_data

        resp_content = resp.content.decode('utf-8')
        result = {
            'response': ujson.loads(resp_content),
            'req_id': event['req_id'],
            'server_meta': server_meta
        }

        redis_key = req_redis_key(event['req_id'])
        self.redis_client.hmset(redis_key, {
            'status': 'complete',
            'response': resp_content
        })

        queue_json_publish(server_meta['return_queue'], result, lambda e: None)
Exemplo n.º 25
0
    def request(self, **request):
        request = RequestFactory.request(self, **request)
        handler = BaseHandler()
        handler.load_middleware()
        # call each middleware in turn and throw away any responses that they might return
        for middleware_path in settings.MIDDLEWARE:
            middleware = import_string(middleware_path)(handler)
            if hasattr(middleware, 'process_request'):
                middleware.process_request(request)

        return request
Exemplo n.º 26
0
def test_jwt_refresh_token_middleware(rf, customer_user, settings):
    refresh_token = create_refresh_token(customer_user)
    settings.MIDDLEWARE = [
        "saleor.core.middleware.jwt_refresh_token_middleware",
    ]
    request = rf.request()
    request.refresh_token = refresh_token
    handler = BaseHandler()
    handler.load_middleware()
    response = handler.get_response(request)
    cookie = response.cookies.get(JWT_REFRESH_TOKEN_COOKIE_NAME)
    assert cookie.value == refresh_token
Exemplo n.º 27
0
    def make_request(self, url, with_middleware=False):
        request = self.request_factory.get(url)

        # see comments at http://djangosnippets.org/snippets/963/
        if with_middleware:
            handler = BaseHandler()
            handler.load_middleware()
            for middleware_method in handler._request_middleware:
                if middleware_method(request):
                    raise Exception("Couldn't create request mock object - " "request middleware returned a response")

        return request
Exemplo n.º 28
0
    def __init__(self, *args, **kwargs):
        # create django request resolver
        self.handler = BaseHandler()

        # prevent recursive includes
        old = settings.MIDDLEWARE
        name = self.__module__ + '.' + self.__class__.__name__
        settings.MIDDLEWARE = [i for i in settings.MIDDLEWARE if i != name]

        self.handler.load_middleware()

        settings.MIDDLEWARE = old
        super(CommonMiddlewareAppendSlashWithoutRedirect, self).__init__(*args, **kwargs)
Exemplo n.º 29
0
def test_jwt_refresh_token_middleware_samesite_debug_mode(rf, customer_user, settings):
    refresh_token = create_refresh_token(customer_user)
    settings.MIDDLEWARE = [
        "saleor.core.middleware.jwt_refresh_token_middleware",
    ]
    settings.DEBUG = True
    request = rf.request()
    request.refresh_token = refresh_token
    handler = BaseHandler()
    handler.load_middleware()
    response = handler.get_response(request)
    cookie = response.cookies.get(JWT_REFRESH_TOKEN_COOKIE_NAME)
    assert cookie["samesite"] == "Lax"
Exemplo n.º 30
0
 def request(self, **request):
     "Construct a generic request object."
     request = RequestFactory.request(self, **request)
     handler = BaseHandler()
     handler.load_middleware()
     # BaseHandler_request_middleware is not set in Django2.0
     # and removed in Django2.1
     if sys.version_info[0] < 2:
         for middleware_method in handler._request_middleware:
             if middleware_method(request):
                 raise Exception("Couldn't create request mock object - "
                                 "request middleware returned a response")
     return request
Exemplo n.º 31
0
def initialize_context() -> HttpRequest:
    """Prepare a request object for webhook subscription.

    It creates a dummy request object and initialize middleware on it. It is required
    to process a request in the same way as API logic does.
    return: HttpRequest
    """
    handler = BaseHandler()
    context = RequestFactory().request(SERVER_NAME=SimpleLazyObject(get_host))
    handler.load_middleware()
    response = handler.get_response(context)
    if not response.status_code == 200:
        raise Exception("Unable to initialize context for webhook.")
    return context
Exemplo n.º 32
0
def get_request_mock():
    """Build a ``request`` mock that can be used for testing."""
    basehandler = BaseHandler()
    basehandler.load_middleware()
    request = WSGIRequest(
        {"REQUEST_METHOD": "GET", "SERVER_NAME": "test", "SERVER_PORT": "8000", "HTTP_HOST": "testhost"}
    )
    # Apply request middleware
    for middleware_method in basehandler._request_middleware:
        # LocaleMiddleware should never be applied a second time because
        # it would broke the current real request language
        if "LocaleMiddleware" not in str(middleware_method.im_class):
            response = middleware_method(request)
    return request
Exemplo n.º 33
0
class UrlConsumer(object):
    """
    Dispatches channel HTTP requests into django's URL system.
    """
    def __init__(self):
        self.handler = BaseHandler()
        self.handler.load_middleware()

    def __call__(self, channel, **kwargs):
        request = HttpRequest.channel_decode(kwargs)
        try:
            response = self.handler.get_response(request)
        except HttpResponse.ResponseLater:
            return
        Channel(request.response_channel).send(**response.channel_encode())
Exemplo n.º 34
0
class UrlConsumer(object):
    """
    Dispatches channel HTTP requests into django's URL system.
    """

    def __init__(self):
        self.handler = BaseHandler()
        self.handler.load_middleware()

    def __call__(self, message):
        request = HttpRequest.channel_decode(message.content)
        try:
            response = self.handler.get_response(request)
        except HttpResponse.ResponseLater:
            return
        message.reply_channel.send(response.channel_encode())
Exemplo n.º 35
0
def get_request_mock():
    """Build a request mock that can be used for testing."""
    bh = BaseHandler()
    bh.load_middleware()
    request = WSGIRequest({
        'REQUEST_METHOD': 'GET',
        'SERVER_NAME': 'test',
        'SERVER_PORT': '8000',
    })
    # Apply request middleware
    for middleware_method in bh._request_middleware:
        # LocaleMiddleware should never be applied a second time because
        # it would broke the current real request language
        if 'LocaleMiddleware' not in str(middleware_method.im_class):
            response = middleware_method(request)
    return request
Exemplo n.º 36
0
class UrlConsumer(object):
    """
    Dispatches channel HTTP requests into django's URL system.
    """

    def __init__(self):
        self.handler = BaseHandler()
        self.handler.load_middleware()

    def __call__(self, channel, **kwargs):
        request = HttpRequest.channel_decode(kwargs)
        try:
            response = self.handler.get_response(request)
        except HttpResponse.ResponseLater:
            return
        Channel(request.response_channel).send(**response.channel_encode())
Exemplo n.º 37
0
class CommonMiddlewareAppendSlashWithoutRedirect(CommonMiddleware):
    """ This class converts HttpSmartRedirectResponse to the common response
        of Django view, without redirect. This is necessary to match status_codes
        for urls like /url?q=1 and /url/?q=1. If you don't use it, you will have 302
        code always on pages without slash.
    """
    response_redirect_class = HttpSmartRedirectResponse

    def __init__(self, *args, **kwargs):
        # create django request resolver
        self.handler = BaseHandler()

        # prevent recursive includes
        old = settings.MIDDLEWARE
        name = self.__module__ + '.' + self.__class__.__name__
        settings.MIDDLEWARE = [i for i in settings.MIDDLEWARE if i != name]

        self.handler.load_middleware()

        settings.MIDDLEWARE = old
        super(CommonMiddlewareAppendSlashWithoutRedirect, self).__init__(*args, **kwargs)

    def get_full_path_with_slash(self, request):
        """ Return the full path of the request with a trailing slash appended
            without Exception in Debug mode
        """
        new_path = request.get_full_path(force_append_slash=True)
        # Prevent construction of scheme relative urls.
        new_path = escape_leading_slashes(new_path)
        return new_path

    def process_response(self, request, response):
        response = super(CommonMiddlewareAppendSlashWithoutRedirect, self).process_response(request, response)

        request.editor_keymap = settings.EDITOR_KEYMAP

        if isinstance(response, HttpSmartRedirectResponse):
            if not request.path.endswith('/'):
                # remove prefix SCRIPT_NAME
                path = request.path[len(settings.FORCE_SCRIPT_NAME):] if settings.FORCE_SCRIPT_NAME \
                    else request.path
                request.path = path + '/'
            # we don't need query string in path_info because it's in request.GET already
            request.path_info = request.path
            response = self.handler.get_response(request)

        return response
Exemplo n.º 38
0
class MessageSenderWorker(QueueProcessingWorker):
    def __init__(self):
        # type: () -> None
        super(MessageSenderWorker, self).__init__()
        self.redis_client = get_redis_client()
        self.handler = BaseHandler()
        self.handler.load_middleware()

    def consume(self, event):
        # type: (Mapping[str, Any]) -> None
        server_meta = event['server_meta']

        environ = {'REQUEST_METHOD': 'SOCKET',
                   'SCRIPT_NAME': '',
                   'PATH_INFO': '/json/messages',
                   'SERVER_NAME': 'localhost',
                   'SERVER_PORT': 9993,
                   'SERVER_PROTOCOL': 'ZULIP_SOCKET/1.0',
                   'wsgi.version': (1, 0),
                   'wsgi.input': StringIO(),
                   'wsgi.errors': sys.stderr,
                   'wsgi.multithread': False,
                   'wsgi.multiprocess': True,
                   'wsgi.run_once': False,
                   'zulip.emulated_method': 'POST'}
        # We're mostly using a WSGIRequest for convenience
        environ.update(server_meta['request_environ'])
        request = WSGIRequest(environ)
        request._request = event['request']
        request.csrf_processing_done = True

        user_profile = get_user_profile_by_id(server_meta['user_id'])
        request._cached_user = user_profile

        resp = self.handler.get_response(request)
        server_meta['time_request_finished'] = time.time()
        server_meta['worker_log_data'] = request._log_data

        resp_content = resp.content.decode('utf-8')
        result = {'response': ujson.loads(resp_content), 'req_id': event['req_id'],
                  'server_meta': server_meta}

        redis_key = req_redis_key(event['req_id'])
        self.redis_client.hmset(redis_key, {'status': 'complete',
                                            'response': resp_content});

        queue_json_publish(server_meta['return_queue'], result, lambda e: None)
Exemplo n.º 39
0
class Middleware404View(Buildable404View):
    def __init__(self, **kwargs):
        self.handler = BaseHandler()
        self.handler.load_middleware()
        super().__init__(**kwargs)

    def get(self, request):
        return self.handler.get_response(request)

    def get_content(self):
        response = self.get(self.request)
        if hasattr(response, 'render'):
            return response.render().content
        if hasattr(response, 'content'):
            return response.content
        raise AttributeError(
            "'%s' object has no attribute 'render' or 'content'" % response)
Exemplo n.º 40
0
 def _apply_middlewares(self, request):
     handler = BaseHandler()
     from django.utils.module_loading import import_string
     for middleware_path in reversed(settings.MIDDLEWARE):
         middleware = import_string(middleware_path)
         mw_instance = middleware(handler)
         if hasattr(mw_instance, 'process_request'):
             mw_instance.process_request(request)
Exemplo n.º 41
0
def get_events_backend(
    request: HttpRequest,
    user_profile: UserProfile,
    handler: BaseHandler,
    user_client: Optional[Client] = REQ(converter=get_client, default=None),
    last_event_id: Optional[int] = REQ(converter=int, default=None),
    queue_id: Optional[List[str]] = REQ(default=None),
    apply_markdown: bool = REQ(default=False, validator=check_bool),
    client_gravatar: bool = REQ(default=False, validator=check_bool),
    all_public_streams: bool = REQ(default=False, validator=check_bool),
    event_types: Optional[str] = REQ(default=None,
                                     validator=check_list(check_string)),
    dont_block: bool = REQ(default=False, validator=check_bool),
    narrow: Iterable[Sequence[str]] = REQ(default=[],
                                          validator=check_list(None)),
    lifespan_secs: int = REQ(default=0, converter=int)
) -> Union[HttpResponse, _RespondAsynchronously]:
    if user_client is None:
        valid_user_client = request.client
    else:
        valid_user_client = user_client

    events_query = dict(user_profile_id=user_profile.id,
                        user_profile_email=user_profile.email,
                        queue_id=queue_id,
                        last_event_id=last_event_id,
                        event_types=event_types,
                        client_type_name=valid_user_client.name,
                        all_public_streams=all_public_streams,
                        lifespan_secs=lifespan_secs,
                        narrow=narrow,
                        dont_block=dont_block,
                        handler_id=handler.handler_id)

    if queue_id is None:
        events_query['new_queue_data'] = dict(
            user_profile_id=user_profile.id,
            realm_id=user_profile.realm_id,
            user_profile_email=user_profile.email,
            event_types=event_types,
            client_type_name=valid_user_client.name,
            apply_markdown=apply_markdown,
            client_gravatar=client_gravatar,
            all_public_streams=all_public_streams,
            queue_timeout=lifespan_secs,
            last_connection_time=time.time(),
            narrow=narrow)

    result = fetch_events(events_query)
    if "extra_log_data" in result:
        request._log_data['extra'] = result["extra_log_data"]

    if result["type"] == "async":
        handler._request = request
        return RespondAsynchronously
    if result["type"] == "error":
        raise result["exception"]
    return json_success(result["response"])
Exemplo n.º 42
0
class MessageSenderWorker(QueueProcessingWorker):
    def __init__(self):
        super(MessageSenderWorker, self).__init__()
        self.redis_client = get_redis_client()
        self.handler = BaseHandler()
        self.handler.load_middleware()

    def consume(self, event):
        server_meta = event["server_meta"]

        environ = {
            "REQUEST_METHOD": "SOCKET",
            "SCRIPT_NAME": "",
            "PATH_INFO": "/json/messages",
            "SERVER_NAME": "localhost",
            "SERVER_PORT": 9993,
            "SERVER_PROTOCOL": "ZULIP_SOCKET/1.0",
            "wsgi.version": (1, 0),
            "wsgi.input": StringIO(),
            "wsgi.errors": sys.stderr,
            "wsgi.multithread": False,
            "wsgi.multiprocess": True,
            "wsgi.run_once": False,
            "zulip.emulated_method": "POST",
        }
        # We're mostly using a WSGIRequest for convenience
        environ.update(server_meta["request_environ"])
        request = WSGIRequest(environ)
        request._request = event["request"]
        request.csrf_processing_done = True

        user_profile = get_user_profile_by_id(server_meta["user_id"])
        request._cached_user = user_profile

        resp = self.handler.get_response(request)
        server_meta["time_request_finished"] = time.time()
        server_meta["worker_log_data"] = request._log_data

        resp_content = resp.content
        result = {"response": ujson.loads(resp_content), "req_id": event["req_id"], "server_meta": server_meta}

        redis_key = req_redis_key(event["req_id"])
        self.redis_client.hmset(redis_key, {"status": "complete", "response": resp_content})

        queue_json_publish(server_meta["return_queue"], result, lambda e: None)
Exemplo n.º 43
0
    def _prepare_request(self, request, page, user, lang, use_middlewares, use_toolbar=False,
                         secure=False):
        from django.contrib.auth.models import AnonymousUser
        try:
            from importlib import import_module
        except ImportError:
            from django.utils.importlib import import_module

        engine = import_module(settings.SESSION_ENGINE)

        request.current_page = SimpleLazyObject(lambda: page)
        if not user:
            if self._login_context:
                user = self._login_context.user
            else:
                user = AnonymousUser()
        if user.is_authenticated():
            session_key = user._meta.pk.value_to_string(user)
        else:
            session_key = 'session_key'

        request.user = user
        request._cached_user = user
        request.session = engine.SessionStore(session_key)
        if secure:
            request.environ['SERVER_PORT'] = str('443')
            request.environ['wsgi.url_scheme'] = str('https')
        request.cookies = SimpleCookie()
        request.errors = StringIO()
        request.LANGUAGE_CODE = lang
        if request.method == 'POST':
            request._dont_enforce_csrf_checks = True
        # Let's use middleware in case requested, otherwise just use CMS toolbar if needed
        if use_middlewares:
            handler = BaseHandler()
            handler.load_middleware()
            for middleware_method in handler._request_middleware:
                if middleware_method(request):
                    raise Exception('Couldn\'t create request mock object -'
                                    'request middleware returned a response')
        elif use_toolbar:
            from cms.middleware.toolbar import ToolbarMiddleware
            mid = ToolbarMiddleware()
            mid.process_request(request)
        return request
Exemplo n.º 44
0
    def get_request_from_message(self, message, widget, user):
        """returns inicialized request
        """

        request = AsgiHandler.request_class(message)

        request.user = user
        request.path = widget.parent.get_absolute_url()
        request.frontend_editing = True

        if not hasattr(request, '_feincms_extra_context'):
            request._feincms_extra_context = {}

        # call processors
        for fn in reversed(list(widget.parent.request_processors.values())):
            fn(widget.parent, request)

        request.LEONARDO_CONFIG = ContextConfig(request)

        # this breaks all get_absolute_uri calls
        request.META['SERVER_NAME'] = 'localhost'
        request.META['SERVER_PORT'] = 80

        handler = BaseHandler()
        handler.load_middleware()

        # Apply request middleware
        for middleware_method in handler._request_middleware:
            try:
                middleware_method(request)
            except:
                pass

        # we know that we are in editing mode and user is logged in
        request.frontend_editing = True
        request.user = user

        # call processors
        for fn in reversed(list(widget.parent.request_processors.values())):
            fn(widget.parent, request)

        if hasattr(widget, 'process') and callable(widget.process):
            widget.process(request, view=self)

        return request
Exemplo n.º 45
0
 def test_kwargs_redirect(self):
     """if kwargs['next'] is supplied to the view then this
     is where the redirect goes"""
     # set up the data
     post_data = {
         'next': 'http://duckduckgo.com/',
         'contenttype_pk' : self.ctype.pk,
         'object_pk': self.item.pk,
     }
     # make a request and hook up the basket
     request = self.factory.post('/customer/details', post_data)
     # all the jiggery pokery
     handler = BaseHandler()
     handler.load_middleware()
     for middleware_method in handler._request_middleware:
         middleware_method(request)
     r = basket(request, next='http://jamiecurle.com/')
     self.assertEqual(r['location'], 'http://jamiecurle.com/')
Exemplo n.º 46
0
def get_fake_request(meta):
  '''Retrieves a fake request using the given request.META.  This allows celery tasks to have a "request" to use in code.'''
  # if the body was cached in the meta, put it back as the wsgi.input
  if BODY_KEY in meta:
    meta['wsgi.input'] = FakePayload(meta[BODY_KEY])
  
  # create a basic request using the Django testing framework
  request = RequestFactory().request(**meta)
  
  # run middleware on it
  handler = BaseHandler()  
  handler.load_middleware()  
  for middleware_method in handler._request_middleware:  
    response = middleware_method(request)
    if response:
      raise Exception("Middleware cannot return a response with a FakeRequest.")  
      
  # return the request
  return request   
Exemplo n.º 47
0
    def _prepare_request(self,
                         request,
                         page,
                         user,
                         lang,
                         use_middlewares,
                         use_toolbar=False,
                         secure=False):
        from django.contrib.auth.models import AnonymousUser

        request.current_page = SimpleLazyObject(lambda: page)
        if not user:
            if self._login_context:
                user = self._login_context.user
            else:
                user = AnonymousUser()
        request.user = user
        request._cached_user = user
        request.session = {}
        if secure:
            request.environ['SERVER_PORT'] = str('443')
            request.environ['wsgi.url_scheme'] = str('https')
        if user.is_authenticated():
            request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
        request.cookies = SimpleCookie()
        request.errors = StringIO()
        request.LANGUAGE_CODE = lang
        if request.method == 'POST':
            request._dont_enforce_csrf_checks = True
        # Let's use middleware in case requested, otherwise just use CMS toolbar if needed
        if use_middlewares:
            handler = BaseHandler()
            handler.load_middleware()
            for middleware_method in handler._request_middleware:
                if middleware_method(request):
                    raise Exception('Couldn\'t create request mock object -'
                                    'request middleware returned a response')
        elif use_toolbar:
            from cms.middleware.toolbar import ToolbarMiddleware
            mid = ToolbarMiddleware()
            mid.process_request(request)
        return request
Exemplo n.º 48
0
def get_request_mock():
    """Build a ``request`` mock up that is used in to render
    the templates in the most fidel environement as possible.

    This fonction is used in the get_placeholders method to
    render the input template and search for the placeholder
    within.
    """
    basehandler = BaseHandler()
    basehandler.load_middleware()
    
    request = FACTORY.get('/')
    # Apply request middleware
    for middleware_method in basehandler._request_middleware:
        # LocaleMiddleware should never be applied a second time because
        # it would broke the current real request language
        if 'LocaleMiddleware' not in str(middleware_method.im_class):
            response = middleware_method(request)
    
    return request
Exemplo n.º 49
0
    def consume_request(self,
                        request,
                        start_callback=None,
                        success_callback=None,
                        fail_callback=None):
        start_callback() if start_callback else None

        handler = BaseHandler()
        handler.load_middleware()

        response = handler.get_response(request)

        if is_success(response.status_code):
            success_callback() if success_callback else None
        else:
            fail_callback() if fail_callback else None

        self.responses[request] = response

        return True
Exemplo n.º 50
0
def get_events_backend(request: HttpRequest, user_profile: UserProfile, handler: BaseHandler,
                       user_client: Optional[Client]=REQ(converter=get_client, default=None),
                       last_event_id: Optional[int]=REQ(converter=int, default=None),
                       queue_id: Optional[List[str]]=REQ(default=None),
                       apply_markdown: bool=REQ(default=False, validator=check_bool),
                       client_gravatar: bool=REQ(default=False, validator=check_bool),
                       all_public_streams: bool=REQ(default=False, validator=check_bool),
                       event_types: Optional[str]=REQ(default=None, validator=check_list(check_string)),
                       dont_block: bool=REQ(default=False, validator=check_bool),
                       narrow: Iterable[Sequence[str]]=REQ(default=[], validator=check_list(None)),
                       lifespan_secs: int=REQ(default=0, converter=int)
                       ) -> Union[HttpResponse, _RespondAsynchronously]:
    if user_client is None:
        valid_user_client = request.client
    else:
        valid_user_client = user_client

    events_query = dict(
        user_profile_id = user_profile.id,
        user_profile_email = user_profile.email,
        queue_id = queue_id,
        last_event_id = last_event_id,
        event_types = event_types,
        client_type_name = valid_user_client.name,
        all_public_streams = all_public_streams,
        lifespan_secs = lifespan_secs,
        narrow = narrow,
        dont_block = dont_block,
        handler_id = handler.handler_id)

    if queue_id is None:
        events_query['new_queue_data'] = dict(
            user_profile_id = user_profile.id,
            realm_id = user_profile.realm_id,
            user_profile_email = user_profile.email,
            event_types = event_types,
            client_type_name = valid_user_client.name,
            apply_markdown = apply_markdown,
            client_gravatar = client_gravatar,
            all_public_streams = all_public_streams,
            queue_timeout = lifespan_secs,
            last_connection_time = time.time(),
            narrow = narrow)

    result = fetch_events(events_query)
    if "extra_log_data" in result:
        request._log_data['extra'] = result["extra_log_data"]

    if result["type"] == "async":
        handler._request = request
        return RespondAsynchronously
    if result["type"] == "error":
        raise result["exception"]
    return json_success(result["response"])
Exemplo n.º 51
0
def get_fake_request(meta):
    '''Retrieves a fake request using the given request.META.  This allows celery tasks to have a "request" to use in code.'''
    # if the body was cached in the meta, put it back as the wsgi.input
    if BODY_KEY in meta:
        meta['wsgi.input'] = FakePayload(meta[BODY_KEY])

    # create a basic request using the Django testing framework
    request = RequestFactory().request(**meta)

    # run middleware on it
    handler = BaseHandler()
    handler.load_middleware()
    for middleware_method in handler._request_middleware:
        response = middleware_method(request)
        if response:
            raise Exception(
                "Middleware cannot return a response with a FakeRequest.")

    # return the request
    return request
Exemplo n.º 52
0
def test_jwt_refresh_token_middleware_token_without_expire(rf, customer_user, settings):
    settings.JWT_EXPIRE = True
    payload = jwt_user_payload(
        customer_user,
        JWT_REFRESH_TYPE,
        settings.JWT_TTL_REFRESH,
    )
    del payload["exp"]

    refresh_token = jwt_encode(payload)
    settings.MIDDLEWARE = [
        "saleor.core.middleware.jwt_refresh_token_middleware",
    ]
    request = rf.request()
    request.refresh_token = refresh_token
    handler = BaseHandler()
    handler.load_middleware()
    response = handler.get_response(request)
    cookie = response.cookies.get(JWT_REFRESH_TOKEN_COOKIE_NAME)
    assert cookie.value == refresh_token
Exemplo n.º 53
0
def test_error_handlers(rf):
    """
    Test that SHUUP_FRONT_INSTALL_ERROR_HANDLERS installs error handlers
    without overwriting possible custom ones.
    """
    with override_settings(
        DEBUG=False,
        SHUUP_FRONT_INSTALL_ERROR_HANDLERS=True,
        MIDDLEWARE_CLASSES=[],
        TEMPLATES=[  # Overriden to be sure about the contents of our 500.jinja
            {
                "BACKEND": "django_jinja.backend.Jinja2",
                "DIRS": [
                    os.path.realpath(os.path.join(os.path.dirname(__file__), "templates"))
                ],
                "OPTIONS": {
                    "match_extension": ".jinja",
                    "newstyle_gettext": True,
                },
                "NAME": "jinja2",
            }
        ]
    ):
        with replace_urls([
            url("^aaargh/", errorful_view)
        ], {"handler404": four_oh_four}):
            resolver = get_resolver(None)
            urlconf = resolver.urlconf_module
            install_error_handlers()
            assert callable(urlconf.handler500)  # We get a new 500 handler
            assert urlconf.handler404 == four_oh_four  # Our custom 404 handler didn't get overwritten
            handler = BaseHandler()
            handler.load_middleware()
            # Test 500
            response = handler.get_response(rf.get("/aaargh/"))
            assert response.status_code == 500  # Uh oh!
            assert "intergalactic testing 500" in force_text(response.content)
            # Test 404
            response = handler.get_response(rf.get("/another_castle/"))
            assert response.status_code == 200  # Our custom 404 handler made it a 200!
            assert b"flesh wound" in response.content
Exemplo n.º 54
0
def build_dummy_request(newsitem):
    """
    Construct a HttpRequest object that is, as far as possible,
    representative of ones that would receive this page as a response. Used
    for previewing / moderation and any other place where we want to
    display a view of this page in the admin interface without going
    through the regular page routing logic.
    """
    url = newsitem.full_url
    if url:
        url_info = urlparse(url)
        hostname = url_info.hostname
        path = url_info.path
        port = url_info.port or 80
    else:
        # Cannot determine a URL to this page - cobble one together based on
        # whatever we find in ALLOWED_HOSTS
        try:
            hostname = settings.ALLOWED_HOSTS[0]
        except IndexError:
            hostname = 'localhost'
        path = '/'
        port = 80

    request = WSGIRequest({
        'REQUEST_METHOD': 'GET',
        'PATH_INFO': path,
        'SERVER_NAME': hostname,
        'SERVER_PORT': port,
        'HTTP_HOST': hostname,
        'wsgi.input': StringIO(),
    })

    # Apply middleware to the request - see http://www.mellowmorning.com/2011/04/18/mock-django-request-for-testing/
    handler = BaseHandler()
    handler.load_middleware()
    # call each middleware in turn and throw away any responses that they might return
    for middleware_method in handler._request_middleware:
        middleware_method(request)

    return request
Exemplo n.º 55
0
def get_request_mock():
    """Build a ``request`` mock up that is used in to render
    the templates in the most fidel environement as possible.

    This fonction is used in the get_placeholders method to
    render the input template and search for the placeholder
    within.
    """
    basehandler = BaseHandler()
    basehandler.load_middleware()
    # http://www.python.org/dev/peps/pep-0333/
    request = WSGIRequest({
        'HTTP_COOKIE': '',
        'PATH_INFO': '/',
        'QUERY_STRING': '',
        'REMOTE_ADDR': '127.0.0.1',
        'REQUEST_METHOD': 'GET',
        'SERVER_NAME': 'page-request-mock',
        'SCRIPT_NAME': '',
        'SERVER_PORT': '80',
        'SERVER_PROTOCOL': 'HTTP/1.1',
        'HTTP_HOST': 'page-request-host',
        'CONTENT_TYPE': 'text/html; charset=utf-8',
        'wsgi.version': (1, 0),
        'wsgi.url_scheme': 'http',
        'wsgi.multiprocess': True,
        'wsgi.multithread':  False,
        'wsgi.run_once':     False,
        'wsgi.input': StringIO("")
    })
    # Apply request middleware
    for middleware_method in basehandler._request_middleware:
        # LocaleMiddleware should never be applied a second time because
        # it would broke the current real request language
        if 'LocaleMiddleware' not in str(middleware_method.im_class):
            response = middleware_method(request)

    return request
Exemplo n.º 56
0
def get_request_mock():
    """Build a ``request`` mock up that is used in to render
    the templates in the most fidel environement as possible.

    This fonction is used in the get_placeholders method to
    render the input template and search for the placeholder
    within.
    """
    from django.test.client import RequestFactory
    from django.core.handlers.base import BaseHandler
    factory = RequestFactory()
    basehandler = BaseHandler()
    basehandler.load_middleware()

    request = factory.get('/')
    # Apply request middleware
    for middleware_method in basehandler._request_middleware:
        # LocaleMiddleware should never be applied a second time because
        # it would broke the current real request language
        if 'LocaleMiddleware' not in str(middleware_method.__self__.__class__):
            middleware_method(request)

    return request
Exemplo n.º 57
0
    def process_request(self, request):
        """Replicates a lot of code from BaseHandler#get_response."""
        callback, callback_args, callback_kwargs = resolve(request.path_info)

        if getattr(callback, 'bypass_middleware', False):
            # bypass_middleware decorator was used; zero out all
            # middleware and return the response.
            handler = BaseHandler()

            handler._request_middleware = []
            handler._view_middleware = []
            handler._template_response_middleware = []
            handler._response_middleware = []
            handler._exception_middleware = []

            response = handler._get_response(request)

            return response
Exemplo n.º 58
0
    def get_response(self, request):
        # Set up middleware if needed. We couldn't do this earlier, because
        # settings weren't available.
        if self._request_middleware is None:
            self.initLock.acquire()
            try:
                try:
                    # Check that middleware is still uninitialised.
                    if self._request_middleware is None:
                        self.load_middleware()
                except:
                    # Unload whatever middleware we got
                    self._request_middleware = None
                    raise
            finally:
                self.initLock.release()

        return BaseHandler.get_response(self, request)
Exemplo n.º 59
0
def test_plugins_middleware_requestor_in_plugin_when_no_app_and_user_in_req_is_none(
        rf, settings):
    settings.MIDDLEWARE = [
        "saleor.core.middleware.plugins",
    ]
    settings.PLUGINS = ["saleor.plugins.tests.sample_plugins.ActivePlugin"]
    request = rf.request()
    request.user = None
    request.app = None

    handler = BaseHandler()
    handler.load_middleware()
    handler.get_response(request)
    plugin = request.plugins.all_plugins.pop()

    assert not plugin.requestor