Ticket #10571: unicode_payload_put.patch

File unicode_payload_put.patch, 6.5 KB (added by Peter van Kampen, 13 years ago)
  • 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' here as it isn't a 'GET-like request'
     762        for method_name in ('get','head','options','delete'):
    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'})
    764766            self.assertEqual(response.context['get-foo'], 'whiz')
    765767            self.assertEqual(response.context['request-foo'], 'whiz')
     768           
    766769
    767770            # A GET-like request can pass a query string as part of the URL
    768771            response = method("/test_client_regress/request_data/?foo=whiz")
     
    814817        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
    815818                                    content_type="application/json")
    816819        self.assertEqual(response.content, json)
     820        response = self.client.put("/test_client_regress/parse_unicode_json/", json,
     821                                    content_type="application/json")
     822        self.assertEqual(response.content, json)
    817823
    818824    def test_unicode_payload_utf8(self):
    819825        "A non-ASCII unicode data encoded as UTF-8 can be POSTed"
     
    822828        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
    823829                                    content_type="application/json; charset=utf-8")
    824830        self.assertEqual(response.content, json.encode('utf-8'))
     831        response = self.client.put("/test_client_regress/parse_unicode_json/", json,
     832                                    content_type="application/json; charset=utf-8")
     833        self.assertEqual(response.content, json.encode('utf-8'))
    825834
    826835    def test_unicode_payload_utf16(self):
    827836        "A non-ASCII unicode data encoded as UTF-16 can be POSTed"
     
    830839        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
    831840                                    content_type="application/json; charset=utf-16")
    832841        self.assertEqual(response.content, json.encode('utf-16'))
     842        response = self.client.put("/test_client_regress/parse_unicode_json/", json,
     843                                    content_type="application/json; charset=utf-16")
     844        self.assertEqual(response.content, json.encode('utf-16'))
    833845
    834846    def test_unicode_payload_non_utf(self):
    835847        "A non-ASCII unicode data as a non-UTF based encoding can be POSTed"
     
    838850        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
    839851                                    content_type="application/json; charset=koi8-r")
    840852        self.assertEqual(response.content, json.encode('koi8-r'))
     853        response = self.client.put("/test_client_regress/parse_unicode_json/", json,
     854                                    content_type="application/json; charset=koi8-r")
     855        self.assertEqual(response.content, json.encode('koi8-r'))
    841856
    842857class DummyFile(object):
    843858    def __init__(self, filename):
  • 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        data = self._encode_data(data, content_type)
    245248
    246249        parsed = urlparse(path)
    247250        r = {
    248             'CONTENT_LENGTH': len(post_data),
     251            'CONTENT_LENGTH': len(data),
    249252            'CONTENT_TYPE':   content_type,
    250253            'PATH_INFO':      self._get_path(parsed),
    251254            'QUERY_STRING':   parsed[4],
    252255            'REQUEST_METHOD': 'POST',
    253             'wsgi.input':     FakePayload(post_data),
     256            'wsgi.input':     FakePayload(data),
    254257        }
    255258        r.update(extra)
    256259        return self.request(**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        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)
     295        #print repr(parsed[4])
    301296        r = {
    302             'CONTENT_LENGTH': len(post_data),
     297            'CONTENT_LENGTH': len(data),
    303298            'CONTENT_TYPE':   content_type,
    304299            'PATH_INFO':      self._get_path(parsed),
    305             'QUERY_STRING':   query_string or parsed[4],
     300            'QUERY_STRING':   parsed[4],
    306301            'REQUEST_METHOD': 'PUT',
    307             'wsgi.input':     FakePayload(post_data),
     302            'wsgi.input':     FakePayload(data),
    308303        }
    309304        r.update(extra)
    310305        return self.request(**r)
Back to Top