#34902 closed Uncategorized (worksforme)

django.test.client.Client.head request fails on AttributeError as_sync is missing

Reported by: Jelle van der Waa Owned by: nobody
Component: Testing framework Version: 4.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

A simple head request test now fails for me since Django 4.2. I do use pytest-django however I am not sure if this is related as the normal django test client is used.

def test_head(client):
    response = client.head('/packages/core/x86_64/linux/')
    assert response.status_code == 200
client = <django.test.client.Client object at 0x7ffb428281d0>, package = None

    def test_head(client, package):
        print(client)
>       response = client.head('/packages/core/x86_64/linux/')

packages/tests/test_views.py:114:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
env/lib/python3.11/site-packages/django/test/client.py:975: in head
    response = super().head(
env/lib/python3.11/site-packages/django/test/client.py:495: in head
    return self.generic(
env/lib/python3.11/site-packages/django/test/client.py:609: in generic
    return self.request(**r)
env/lib/python3.11/site-packages/django/test/client.py:886: in request
    response = self.handler(environ)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.test.client.ClientHandler object at 0x7ffb427cae50>
environ = {'HTTP_COOKIE': '', 'PATH_INFO': '/packages/core/x86_64/linux/', 'QUERY_STRING': '', 'REMOTE_ADDR': '127.0.0.1', ...}

    def __call__(self, environ):
        # 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()

        request_started.disconnect(close_old_connections)
        request_started.send(sender=self.__class__, environ=environ)
        request_started.connect(close_old_connections)
        request = WSGIRequest(environ)
        # 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 = self.get_response(request)

        # Simulate behaviors of most web servers.
        conditional_content_removal(request, response)

        # Attach the originating request to the response so that it could be
        # later retrieved.
        response.wsgi_request = request

        # Emulate a WSGI server by calling the close method on completion.
        if response.streaming:
>           if response.is_async:
E           AttributeError: 'HttpResponse' object has no attribute 'is_async'

env/lib/python3.11/site-packages/django/test/client.py:187: AttributeError

Change History (1)

comment:1 by Mariusz Felisiak, 11 months ago

Component: UncategorizedTesting framework
Resolution: worksforme
Status: newclosed

Thanks for the report, however the following works for me with Django 4.2.6:

  • tests/handlers/tests.py

    diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py
    index 0348b8e5d6..6bfa852db5 100644
    a b class HandlerRequestTests(SimpleTestCase):  
    253253        self.assertEqual(response.status_code, 200)
    254254        self.assertEqual(b"".join(list(response)), b"streaming content")
    255255
     256    def test_head_streaming(self):
     257        response = self.client.head("/streaming/")
     258        self.assertEqual(response.status_code, 200)
     259
    256260    def test_async_streaming(self):
    257261        response = self.client.get("/async_streaming/")
    258262        self.assertEqual(response.status_code, 200)
Note: See TracTickets for help on using tickets.
Back to Top