Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #11371: Made `django.test.Client.put()` work for non-fo…

…rm-data PUT (i.e. JSON, etc.). Thanks, phyfus. Backport of [11656] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@11657 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bf33627c63a475d1a1e0ee1400f4dc0e6cb2f076 1 parent 0ed09c8
Jacob Kaplan-Moss authored October 26, 2009
8  django/test/client.py
@@ -362,12 +362,18 @@ def put(self, path, data={}, content_type=MULTIPART_CONTENT,
362 362
         else:
363 363
             post_data = data
364 364
 
  365
+        # Make `data` into a querystring only if it's not already a string. If
  366
+        # it is a string, we'll assume that the caller has already encoded it.
  367
+        query_string = None
  368
+        if not isinstance(data, basestring):
  369
+            query_string = urlencode(data, doseq=True)
  370
+
365 371
         parsed = urlparse(path)
366 372
         r = {
367 373
             'CONTENT_LENGTH': len(post_data),
368 374
             'CONTENT_TYPE':   content_type,
369 375
             'PATH_INFO':      urllib.unquote(parsed[2]),
370  
-            'QUERY_STRING':   urlencode(data, doseq=True) or parsed[4],
  376
+            'QUERY_STRING':   query_string or parsed[4],
371 377
             'REQUEST_METHOD': 'PUT',
372 378
             'wsgi.input':     FakePayload(post_data),
373 379
         }
17  tests/regressiontests/test_client_regress/models.py
@@ -574,6 +574,23 @@ def test_delete(self):
574 574
         self.assertEqual(response.status_code, 200)
575 575
         self.assertEqual(response.content, 'request method: DELETE')
576 576
 
  577
+class RequestMethodStringDataTests(TestCase):
  578
+    def test_post(self):
  579
+        "Request a view with string data via request method POST"
  580
+        # Regression test for #11371
  581
+        data = u'{"test": "json"}'
  582
+        response = self.client.post('/test_client_regress/request_methods/', data=data, content_type='application/json')
  583
+        self.assertEqual(response.status_code, 200)
  584
+        self.assertEqual(response.content, 'request method: POST')
  585
+
  586
+    def test_put(self):
  587
+        "Request a view with string data via request method PUT"
  588
+        # Regression test for #11371
  589
+        data = u'{"test": "json"}'
  590
+        response = self.client.put('/test_client_regress/request_methods/', data=data, content_type='application/json')
  591
+        self.assertEqual(response.status_code, 200)
  592
+        self.assertEqual(response.content, 'request method: PUT')
  593
+
577 594
 class QueryStringTests(TestCase):
578 595
     def test_get_like_requests(self):
579 596
         for method_name in ('get','head','options','put','delete'):

0 notes on commit bf33627

Please sign in to comment.
Something went wrong with that request. Please try again.