8000 [1.7.x] Fixed #21740 -- Allowed test client data to be an empty string · alex-python/django@53bc81d · GitHub
[go: up one dir, main page]

Skip to content

Commit 53bc81d

Browse files
committed
[1.7.x] Fixed #21740 -- Allowed test client data to be an empty string
This fixes a regression introduced by 2a31d00. Thanks tony-zhu for the report. Backport of f0bb3c9 from master.
1 parent 117e970 commit 53bc81d

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

django/test/client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,9 @@ def _get_path(self, parsed):
278278
def get(self, path, data=None, secure=False, **extra):
279279
"Construct a GET request."
280280

281+
data = {} if data is None else data
281282
r = {
282-
'QUERY_STRING': urlencode(data or {}, doseq=True),
283+
'QUERY_STRING': urlencode(data, doseq=True),
283284
}
284285
r.update(extra)
285286
return self.generic('GET', path, secure=secure, **r)
@@ -288,16 +289,18 @@ def post(self, path, data=None, content_type=MULTIPART_CONTENT,
288289
secure=False, **extra):
289290
"Construct a POST request."
290291

291-
post_data = self._encode_data(data or {}, content_type)
292+
data = {} if data is None else data
293+
post_data = self._encode_data(data, content_type)
292294

293295
return self.generic('POST', path, post_data, content_type,
294296
secure=secure, **extra)
295297

296298
def head(self, path, data=None, secure=False, **extra):
297299
"Construct a HEAD request."
298300

301+
data = {} if data is None else data
299302
r = {
300-
'QUERY_STRING': urlencode(data or {}, doseq=True),
303+
'QUERY_STRING': urlencode(data, doseq=True),
301304
}
302305
r.update(extra)
303306
return self.generic('HEAD', path, secure=secure, **r)
8000

docs/releases/1.7.1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,6 @@ Bugfixes
128128

129129
* Fixed :djadmin:`makemigrations` to detect changes to
130130
:attr:`Meta.db_table <django.db.models.Options.db_table>` (:ticket:`23629`).
131+
132+
* Fixed a regression when feeding the Django test client with an empty data
133+
string (:ticket:`21740`).

tests/test_client_regress/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,16 @@ def test_patch(self):
11861186
self.assertEqual(response.status_code, 200)
11871187
self.assertEqual(response.content, b'request method: PATCH')
11881188

1189+
def test_empty_string_data(self):
1190+
"Request a view with empty string data via request method GET/POST/HEAD"
1191+
# Regression test for #21740
1192+
response = self.client.get('/body/', data='', content_type='application/json')
1193+
self.assertEqual(response.content, b'')
1194+
response = self.client.post('/body/', data='', content_type='application/json')
1195+
self.assertEqual(response.content, b'')
1196+
response = self.client.head('/body/', data='', content_type='application/json')
1197+
self.assertEqual(response.content, b'')
1198+
11891199

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

0 commit comments

Comments
 (0)
0