From 190f6e5b45c68653f2deab025c50f6797e5d793f Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Fri, 21 Jan 2011 01:04:05 +0000 Subject: [PATCH] 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. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15254 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/test/client.py | 2 +- tests/regressiontests/test_client_regress/models.py | 9 +++++++++ tests/regressiontests/test_client_regress/urls.py | 1 + tests/regressiontests/test_client_regress/views.py | 3 +++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/test/client.py b/django/test/client.py index 203828a98a..24fe62764a 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -46,7 +46,7 @@ class FakePayload(object): def read(self, num_bytes=None): if num_bytes is None: - num_bytes = self.__len or 1 + num_bytes = self.__len or 0 assert self.__len >= num_bytes, "Cannot read more than the available bytes from the HTTP incoming data." content = self.__content.read(num_bytes) self.__len -= num_bytes diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index 753a4f987a..8f39961a82 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -879,3 +879,12 @@ class ResponseTemplateDeprecationTests(TestCase): def test_response_no_template(self): response = self.client.get("/test_client_regress/request_methods/") self.assertEqual(response.template, None) + +class RawPostDataTest(TestCase): + "Access to request.raw_post_data from the test client." + def test_raw_post_data(self): + # Refs #14753 + try: + response = self.client.get("/test_client_regress/raw_post_data/") + except AssertionError: + self.fail("Accessing request.raw_post_data from a view fetched with GET by the test client shouldn't fail.") diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py index 650d80b909..31edc96a86 100644 --- a/tests/regressiontests/test_client_regress/urls.py +++ b/tests/regressiontests/test_client_regress/urls.py @@ -26,4 +26,5 @@ urlpatterns = patterns('', (r'^parse_unicode_json/$', views.return_json_file), (r'^check_headers/$', views.check_headers), (r'^check_headers_redirect/$', redirect_to, {'url': '/test_client_regress/check_headers/'}), + (r'^raw_post_data/$', views.raw_post_data), ) diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py index 40aa61fca7..9fcb894fe0 100644 --- a/tests/regressiontests/test_client_regress/views.py +++ b/tests/regressiontests/test_client_regress/views.py @@ -91,3 +91,6 @@ def check_headers(request): "A view that responds with value of the X-ARG-CHECK header" return HttpResponse('HTTP_X_ARG_CHECK: %s' % request.META.get('HTTP_X_ARG_CHECK', 'Undefined')) +def raw_post_data(request): + "A view that is requested with GET and accesses request.raw_post_data. Refs #14753." + return HttpResponse(request.raw_post_data)