Commit 53bc81dc authored by Claude Paroz's avatar Claude Paroz
Browse files

[1.7.x] Fixed #21740 -- Allowed test client data to be an empty string

This fixes a regression introduced by 2a31d009.
Thanks tony-zhu for the report.
Backport of f0bb3c98 from master.
parent 117e9706
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -278,8 +278,9 @@ class RequestFactory(object):
    def get(self, path, data=None, secure=False, **extra):
        "Construct a GET request."

        data = {} if data is None else data
        r = {
            'QUERY_STRING': urlencode(data or {}, doseq=True),
            'QUERY_STRING': urlencode(data, doseq=True),
        }
        r.update(extra)
        return self.generic('GET', path, secure=secure, **r)
@@ -288,7 +289,8 @@ class RequestFactory(object):
             secure=False, **extra):
        "Construct a POST request."

        post_data = self._encode_data(data or {}, content_type)
        data = {} if data is None else data
        post_data = self._encode_data(data, content_type)

        return self.generic('POST', path, post_data, content_type,
                            secure=secure, **extra)
@@ -296,8 +298,9 @@ class RequestFactory(object):
    def head(self, path, data=None, secure=False, **extra):
        "Construct a HEAD request."

        data = {} if data is None else data
        r = {
            'QUERY_STRING': urlencode(data or {}, doseq=True),
            'QUERY_STRING': urlencode(data, doseq=True),
        }
        r.update(extra)
        return self.generic('HEAD', path, secure=secure, **r)
+3 −0
Original line number Diff line number Diff line
@@ -128,3 +128,6 @@ Bugfixes

* Fixed :djadmin:`makemigrations` to detect changes to
  :attr:`Meta.db_table <django.db.models.Options.db_table>` (:ticket:`23629`).

* Fixed a regression when feeding the Django test client with an empty data
  string (:ticket:`21740`).
+10 −0
Original line number Diff line number Diff line
@@ -1186,6 +1186,16 @@ class RequestMethodStringDataTests(TestCase):
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, b'request method: PATCH')

    def test_empty_string_data(self):
        "Request a view with empty string data via request method GET/POST/HEAD"
        # Regression test for #21740
        response = self.client.get('/body/', data='', content_type='application/json')
        self.assertEqual(response.content, b'')
        response = self.client.post('/body/', data='', content_type='application/json')
        self.assertEqual(response.content, b'')
        response = self.client.head('/body/', data='', content_type='application/json')
        self.assertEqual(response.content, b'')


class QueryStringTests(TestCase):
    urls = 'test_client_regress.urls'