Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#14753 closed (fixed)

Accessing (Get)HttpRequest.raw_post_data in view results in exception during testing

Reported by: zimnyx Owned by: nobody
Component: Testing framework Version: master
Severity: Keywords: blocker
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

(Confirmed with Django trunk r14680)

This happens only when running tests and test client makes GET request.
When views is accessed under runserver, it serves empty page as desired - no exception is raised.

# view
def zoo(response):
    return HttpResponse(response.raw_post_data)
#view test
class SimpleTest(TestCase):    
    def test_lala(self):       
        c = Client()
        res = c.get('/')
/tmp/zoo$ time python manage.py test --failfast
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_lala (zoo.lew.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/zoo/../zoo/lew/tests.py", line 15, in test_lala
    res = c.get('/')
  File "/tmp/zoo/django/test/client.py", line 434, in get
    response = super(Client, self).get(path, data=data, **extra)
  File "/tmp/zoo/django/test/client.py", line 218, in get
    return self.request(**r)
  File "/tmp/zoo/django/core/handlers/base.py", line 109, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/tmp/zoo/../zoo/lew/views.py", line 6, in zoo
    return HttpResponse(re.raw_post_data)
  File "/tmp/zoo/django/http/__init__.py", line 153, in _get_raw_post_data
    self._raw_post_data = self.read()
  File "/tmp/zoo/django/http/__init__.py", line 199, in read
    return self._stream.read(*args, **kwargs)
  File "/tmp/zoo/django/test/client.py", line 50, in read
    assert self.__len >= num_bytes, "Cannot read more than the available bytes from the HTTP incoming data."
AssertionError: Cannot read more than the available bytes from the HTTP incoming data.

I can agree that accessing raw_post_data in case of GET request is weird, but nevertheless such issue should be handled in consistent way no matter if it's request via Django's test client or real http request handled by runserver.

Attachments (2)

14753.tests.diff (1.8 KB) - added by ojii 5 years ago.
test case for the issue described
14753.diff (539 bytes) - added by ojii 5 years ago.
fix for this issue

Download all attachments as: .zip

Change History (8)

comment:1 Changed 5 years ago by russellm

  • milestone set to 1.3
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

This has been introduced as a result of the streaming HTTPRequest changes, so this is a regression.

Changed 5 years ago by ojii

test case for the issue described

comment:2 Changed 5 years ago by ojii

  • Has patch set
  • Patch needs improvement set

Changed 5 years ago by ojii

fix for this issue

comment:3 Changed 5 years ago by ojii

  • Patch needs improvement unset

comment:4 Changed 4 years ago by russellm

  • Keywords blocker added

comment:5 Changed 4 years ago by ramiro

  • Resolution set to fixed
  • Status changed from new to closed

(In [15254]) Fixed #14753 -- Fixed the test client to not raise an AssertionError when request.raw_post_data is accessed from a view it has fetched with GET. Thanks zimnyx for the report and ojii for the patch.

comment:6 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top