Ticket #10571: unicode_payload_put.2.patch

File unicode_payload_put.2.patch, 5.7 KB (added by Peter van Kampen, 13 years ago)

Updated patch addressing wildfire's comments. The deletion of the query_string handling in the put method still needs to be looked at, as for the deletion of 'put' , 'delete' in test_get_like_requests.

  • django/test/client.py

     
    207207        "Construct a generic request object."
    208208        return WSGIRequest(self._base_environ(**request))
    209209
     210    def _encode_data(self, data, content_type, ):
     211        if content_type is MULTIPART_CONTENT:
     212            return encode_multipart(BOUNDARY, data)
     213        else:
     214            # Encode the content so that the byte representation is correct.
     215            match = CONTENT_TYPE_RE.match(content_type)
     216            if match:
     217                charset = match.group(1)
     218            else:
     219                charset = settings.DEFAULT_CHARSET
     220            return smart_str(data, encoding=charset)
     221
    210222    def _get_path(self, parsed):
    211223        # If there are parameters, add them
    212224        if parsed[3]:
     
    232244             **extra):
    233245        "Construct a POST request."
    234246
    235         if content_type is MULTIPART_CONTENT:
    236             post_data = encode_multipart(BOUNDARY, data)
    237         else:
    238             # Encode the content so that the byte representation is correct.
    239             match = CONTENT_TYPE_RE.match(content_type)
    240             if match:
    241                 charset = match.group(1)
    242             else:
    243                 charset = settings.DEFAULT_CHARSET
    244             post_data = smart_str(data, encoding=charset)
     247        post_data = self._encode_data(data, content_type)
    245248
    246249        parsed = urlparse(path)
    247250        r = {
     
    286289            **extra):
    287290        "Construct a PUT request."
    288291
    289         if content_type is MULTIPART_CONTENT:
    290             post_data = encode_multipart(BOUNDARY, data)
    291         else:
    292             post_data = data
     292        put_data = self._encode_data(data, content_type)
    293293
    294         # Make `data` into a querystring only if it's not already a string. If
    295         # it is a string, we'll assume that the caller has already encoded it.
    296         query_string = None
    297         if not isinstance(data, basestring):
    298             query_string = urlencode(data, doseq=True)
    299 
    300294        parsed = urlparse(path)
    301295        r = {
    302             'CONTENT_LENGTH': len(post_data),
     296            'CONTENT_LENGTH': len(put_data),
    303297            'CONTENT_TYPE':   content_type,
    304298            'PATH_INFO':      self._get_path(parsed),
    305             'QUERY_STRING':   query_string or parsed[4],
     299            'QUERY_STRING':   parsed[4],
    306300            'REQUEST_METHOD': 'PUT',
    307             'wsgi.input':     FakePayload(post_data),
     301            'wsgi.input':     FakePayload(put_data),
    308302        }
    309303        r.update(extra)
    310304        return self.request(**r)
  • tests/regressiontests/test_client_regress/models.py

     
    757757
    758758class QueryStringTests(TestCase):
    759759    def test_get_like_requests(self):
    760         for method_name in ('get','head','options','put','delete'):
     760        # See: https://code.djangoproject.com/ticket/10571.
     761        # Removed 'put' and 'delete' here as they are 'GET-like requests'
     762        for method_name in ('get','head','options'):
    761763            # A GET-like request can pass a query string as data
    762764            method = getattr(self.client, method_name)
    763765            response = method("/test_client_regress/request_data/", data={'foo':'whiz'})
     
    814816        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
    815817                                    content_type="application/json")
    816818        self.assertEqual(response.content, json)
     819        response = self.client.put("/test_client_regress/parse_unicode_json/", json,
     820                                    content_type="application/json")
     821        self.assertEqual(response.content, json)
    817822
    818823    def test_unicode_payload_utf8(self):
    819824        "A non-ASCII unicode data encoded as UTF-8 can be POSTed"
     
    822827        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
    823828                                    content_type="application/json; charset=utf-8")
    824829        self.assertEqual(response.content, json.encode('utf-8'))
     830        response = self.client.put("/test_client_regress/parse_unicode_json/", json,
     831                                    content_type="application/json; charset=utf-8")
     832        self.assertEqual(response.content, json.encode('utf-8'))
    825833
    826834    def test_unicode_payload_utf16(self):
    827835        "A non-ASCII unicode data encoded as UTF-16 can be POSTed"
     
    830838        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
    831839                                    content_type="application/json; charset=utf-16")
    832840        self.assertEqual(response.content, json.encode('utf-16'))
     841        response = self.client.put("/test_client_regress/parse_unicode_json/", json,
     842                                    content_type="application/json; charset=utf-16")
     843        self.assertEqual(response.content, json.encode('utf-16'))
    833844
    834845    def test_unicode_payload_non_utf(self):
    835846        "A non-ASCII unicode data as a non-UTF based encoding can be POSTed"
     
    838849        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
    839850                                    content_type="application/json; charset=koi8-r")
    840851        self.assertEqual(response.content, json.encode('koi8-r'))
     852        response = self.client.put("/test_client_regress/parse_unicode_json/", json,
     853                                    content_type="application/json; charset=koi8-r")
     854        self.assertEqual(response.content, json.encode('koi8-r'))
    841855
    842856class DummyFile(object):
    843857    def __init__(self, filename):
Back to Top