Esempio n. 1
0
 def __init__(self, request: ASGIRequest):
     self.request = request
     request.scope["__django_request__"] = request
     request.scope["__django_session__"] = request.session
     request.scope["__django_user__"] = request.user
     self.raw_response = dict(status=200, content=b"")
     self.headers = {}
Esempio n. 2
0
 def request(self, **request):
     """Construct a generic request object."""
     # This is synchronous, which means all methods on this class are.
     # AsyncClient, however, has an async request function, which makes all
     # its methods async.
     if '_body_file' in request:
         body_file = request.pop('_body_file')
     else:
         body_file = FakePayload('')
     return ASGIRequest(self._base_scope(**request), body_file)
Esempio n. 3
0
    async def __call__(self, scope):
        # Set up middleware if needed. We couldn't do this earlier, because
        # settings weren't available.
        if self._middleware_chain is None:
            self.load_middleware(is_async=True)
        # Extract body file from the scope, if provided.
        if "_body_file" in scope:
            body_file = scope.pop("_body_file")
        else:
            body_file = FakePayload("")

        request_started.disconnect(close_old_connections)
        await sync_to_async(request_started.send,
                            thread_sensitive=False)(sender=self.__class__,
                                                    scope=scope)
        request_started.connect(close_old_connections)
        request = ASGIRequest(scope, body_file)
        # Sneaky little hack so that we can easily get round
        # CsrfViewMiddleware. This makes life easier, and is probably required
        # for backwards compatibility with external tests against admin views.
        request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
        # Request goes through middleware.
        response = await self.get_response_async(request)
        # Simulate behaviors of most Web servers.
        conditional_content_removal(request, response)
        # Attach the originating ASGI request to the response so that it could
        # be later retrieved.
        response.asgi_request = request
        # Emulate a server by calling the close method on completion.
        if response.streaming:
            response.streaming_content = await sync_to_async(
                closing_iterator_wrapper, thread_sensitive=False)(
                    response.streaming_content,
                    response.close,
                )
        else:
            request_finished.disconnect(close_old_connections)
            # Will fire request_finished.
            await sync_to_async(response.close, thread_sensitive=False)()
            request_finished.connect(close_old_connections)
        return response