Ticket #10571: unicode_payload_put.patch
File unicode_payload_put.patch, 6.5 KB (added by , 13 years ago) |
---|
-
tests/regressiontests/test_client_regress/models.py
757 757 758 758 class QueryStringTests(TestCase): 759 759 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'): 761 763 # A GET-like request can pass a query string as data 762 764 method = getattr(self.client, method_name) 763 765 response = method("/test_client_regress/request_data/", data={'foo':'whiz'}) 764 766 self.assertEqual(response.context['get-foo'], 'whiz') 765 767 self.assertEqual(response.context['request-foo'], 'whiz') 768 766 769 767 770 # A GET-like request can pass a query string as part of the URL 768 771 response = method("/test_client_regress/request_data/?foo=whiz") … … 814 817 response = self.client.post("/test_client_regress/parse_unicode_json/", json, 815 818 content_type="application/json") 816 819 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) 817 823 818 824 def test_unicode_payload_utf8(self): 819 825 "A non-ASCII unicode data encoded as UTF-8 can be POSTed" … … 822 828 response = self.client.post("/test_client_regress/parse_unicode_json/", json, 823 829 content_type="application/json; charset=utf-8") 824 830 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')) 825 834 826 835 def test_unicode_payload_utf16(self): 827 836 "A non-ASCII unicode data encoded as UTF-16 can be POSTed" … … 830 839 response = self.client.post("/test_client_regress/parse_unicode_json/", json, 831 840 content_type="application/json; charset=utf-16") 832 841 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')) 833 845 834 846 def test_unicode_payload_non_utf(self): 835 847 "A non-ASCII unicode data as a non-UTF based encoding can be POSTed" … … 838 850 response = self.client.post("/test_client_regress/parse_unicode_json/", json, 839 851 content_type="application/json; charset=koi8-r") 840 852 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')) 841 856 842 857 class DummyFile(object): 843 858 def __init__(self, filename): -
django/test/client.py
207 207 "Construct a generic request object." 208 208 return WSGIRequest(self._base_environ(**request)) 209 209 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 210 222 def _get_path(self, parsed): 211 223 # If there are parameters, add them 212 224 if parsed[3]: … … 232 244 **extra): 233 245 "Construct a POST request." 234 246 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) 245 248 246 249 parsed = urlparse(path) 247 250 r = { 248 'CONTENT_LENGTH': len( post_data),251 'CONTENT_LENGTH': len(data), 249 252 'CONTENT_TYPE': content_type, 250 253 'PATH_INFO': self._get_path(parsed), 251 254 'QUERY_STRING': parsed[4], 252 255 'REQUEST_METHOD': 'POST', 253 'wsgi.input': FakePayload( post_data),256 'wsgi.input': FakePayload(data), 254 257 } 255 258 r.update(extra) 256 259 return self.request(**r) … … 286 289 **extra): 287 290 "Construct a PUT request." 288 291 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) 293 293 294 # Make `data` into a querystring only if it's not already a string. If295 # it is a string, we'll assume that the caller has already encoded it.296 query_string = None297 if not isinstance(data, basestring):298 query_string = urlencode(data, doseq=True)299 300 294 parsed = urlparse(path) 295 #print repr(parsed[4]) 301 296 r = { 302 'CONTENT_LENGTH': len( post_data),297 'CONTENT_LENGTH': len(data), 303 298 'CONTENT_TYPE': content_type, 304 299 'PATH_INFO': self._get_path(parsed), 305 'QUERY_STRING': query_string orparsed[4],300 'QUERY_STRING': parsed[4], 306 301 'REQUEST_METHOD': 'PUT', 307 'wsgi.input': FakePayload( post_data),302 'wsgi.input': FakePayload(data), 308 303 } 309 304 r.update(extra) 310 305 return self.request(**r)