# HG changeset patch
# User js <johan@s2hc.com>
# Date 1333297370 -7200
# Node ID 439293e4935bd15456857296d78195b86a6f024c
# Parent ec7e13dc23c15307c53a7ddbe5acb692c7222451
added delete as get_like request
removed check for 'request method: HEAD since head method won't return body
diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
a
|
b
|
|
814 | 814 | data = u'{"test": "json"}' |
815 | 815 | response = self.client.head('/test_client_regress/request_methods/', data=data, content_type='application/json') |
816 | 816 | self.assertEqual(response.status_code, 200) |
817 | | self.assertEqual(response.content, 'request method: HEAD') |
818 | 817 | |
819 | 818 | def test_options(self): |
820 | 819 | "Request a view with string data via request method options" |
… |
… |
|
836 | 835 | def test_get_like_requests(self): |
837 | 836 | # See: https://code.djangoproject.com/ticket/10571. |
838 | 837 | # Removed 'put' and 'delete' here as they are 'GET-like requests' |
839 | | for method_name in ('get','head','options'): |
| 838 | for method_name in ('get','head','options', 'delete'): |
840 | 839 | # A GET-like request can pass a query string as data |
841 | 840 | method = getattr(self.client, method_name) |
842 | 841 | response = method("/test_client_regress/request_data/", data={'foo':'whiz'}) |
# HG changeset patch
# User js <johan@s2hc.com>
# Date 1333297558 -7200
# Node ID e311d2c21ae61b695bccdc110b8128c8a14abc24
# Parent 439293e4935bd15456857296d78195b86a6f024c
refactoring test client request methods (get, put, post, delete, head, options) to use generic method, also allowing delete, head, options and get to send body in request
diff --git a/django/test/client.py b/django/test/client.py
a
|
b
|
|
229 | 229 | else: |
230 | 230 | return urllib.unquote(parsed[2]) |
231 | 231 | |
232 | | def get(self, path, data={}, **extra): |
| 232 | def _generic_method(self, method, path, data, content_type, **extra): |
| 233 | "a generic method for building the kwargs for request" |
| 234 | |
| 235 | parsed = urlparse(path) |
| 236 | |
| 237 | if content_type == 'querystring': |
| 238 | r = { |
| 239 | 'CONTENT_TYPE': 'text/html; charset=utf-8', |
| 240 | 'QUERY_STRING': urlencode(data, doseq=True) or parsed[4], |
| 241 | } |
| 242 | else: |
| 243 | encoded_data = self._encode_data(data, content_type) |
| 244 | r = { |
| 245 | 'CONTENT_TYPE': content_type, |
| 246 | 'CONTENT_LENGTH': len(encoded_data), |
| 247 | 'QUERY_STRING': parsed[4], |
| 248 | 'wsgi.input': FakePayload(encoded_data), |
| 249 | } |
| 250 | r.update({ |
| 251 | 'PATH_INFO': self._get_path(parsed), |
| 252 | 'REQUEST_METHOD': method.upper() |
| 253 | }) |
| 254 | r.update(extra) |
| 255 | return self.request(**r) |
| 256 | |
| 257 | def get(self, path, data={}, content_type='querystring', **extra): |
233 | 258 | "Construct a GET request" |
234 | 259 | |
235 | | parsed = urlparse(path) |
236 | | r = { |
237 | | 'CONTENT_TYPE': 'text/html; charset=utf-8', |
238 | | 'PATH_INFO': self._get_path(parsed), |
239 | | 'QUERY_STRING': urlencode(data, doseq=True) or parsed[4], |
240 | | 'REQUEST_METHOD': 'GET', |
241 | | } |
242 | | r.update(extra) |
243 | | return self.request(**r) |
| 260 | return self._generic_method('get', path, data, content_type, **extra) |
244 | 261 | |
245 | 262 | def post(self, path, data={}, content_type=MULTIPART_CONTENT, |
246 | 263 | **extra): |
247 | 264 | "Construct a POST request." |
248 | 265 | |
249 | | post_data = self._encode_data(data, content_type) |
| 266 | return self._generic_method('post', path, data, content_type, **extra) |
250 | 267 | |
251 | | parsed = urlparse(path) |
252 | | r = { |
253 | | 'CONTENT_LENGTH': len(post_data), |
254 | | 'CONTENT_TYPE': content_type, |
255 | | 'PATH_INFO': self._get_path(parsed), |
256 | | 'QUERY_STRING': parsed[4], |
257 | | 'REQUEST_METHOD': 'POST', |
258 | | 'wsgi.input': FakePayload(post_data), |
259 | | } |
260 | | r.update(extra) |
261 | | return self.request(**r) |
262 | | |
263 | | def head(self, path, data={}, **extra): |
| 268 | def head(self, path, data={}, content_type='querystring', **extra): |
264 | 269 | "Construct a HEAD request." |
265 | 270 | |
266 | | parsed = urlparse(path) |
267 | | r = { |
268 | | 'CONTENT_TYPE': 'text/html; charset=utf-8', |
269 | | 'PATH_INFO': self._get_path(parsed), |
270 | | 'QUERY_STRING': urlencode(data, doseq=True) or parsed[4], |
271 | | 'REQUEST_METHOD': 'HEAD', |
272 | | } |
273 | | r.update(extra) |
274 | | return self.request(**r) |
| 271 | return self._generic_method('head', path, data, content_type, **extra) |
275 | 272 | |
276 | | def options(self, path, data={}, **extra): |
| 273 | def options(self, path, data={}, content_type='querystring', **extra): |
277 | 274 | "Constrict an OPTIONS request" |
278 | 275 | |
279 | | parsed = urlparse(path) |
280 | | r = { |
281 | | 'PATH_INFO': self._get_path(parsed), |
282 | | 'QUERY_STRING': urlencode(data, doseq=True) or parsed[4], |
283 | | 'REQUEST_METHOD': 'OPTIONS', |
284 | | } |
285 | | r.update(extra) |
286 | | return self.request(**r) |
| 276 | return self._generic_method('options', path, data, content_type, **extra) |
287 | 277 | |
288 | 278 | def put(self, path, data={}, content_type=MULTIPART_CONTENT, |
289 | 279 | **extra): |
… |
… |
|
301 | 291 | 'wsgi.input': FakePayload(put_data), |
302 | 292 | } |
303 | 293 | r.update(extra) |
304 | | return self.request(**r) |
| 294 | #return self.request(**r) |
| 295 | return self._generic_method('put', path, data, content_type, **extra) |
305 | 296 | |
306 | | def delete(self, path, data={}, **extra): |
| 297 | def delete(self, path, data={}, content_type='querystring', **extra): |
307 | 298 | "Construct a DELETE request." |
308 | 299 | |
309 | | parsed = urlparse(path) |
310 | | r = { |
311 | | 'PATH_INFO': self._get_path(parsed), |
312 | | 'QUERY_STRING': urlencode(data, doseq=True) or parsed[4], |
313 | | 'REQUEST_METHOD': 'DELETE', |
314 | | } |
315 | | r.update(extra) |
316 | | return self.request(**r) |
| 300 | return self._generic_method('delete', path, data, content_type, **extra) |
317 | 301 | |
318 | 302 | |
319 | 303 | class Client(RequestFactory): |