Ticket #18048: patch_and_tests.diff

File patch_and_tests.diff, 6.1 KB (added by Johan Sommerfeld <johan@…>, 13 years ago)

patch of client, more and modified tests

  • tests/regressiontests/test_client_regress/models.py

    # 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  
    814814        data = u'{"test": "json"}'
    815815        response = self.client.head('/test_client_regress/request_methods/', data=data, content_type='application/json')
    816816        self.assertEqual(response.status_code, 200)
    817         self.assertEqual(response.content, 'request method: HEAD')
    818817
    819818    def test_options(self):
    820819        "Request a view with string data via request method options"
     
    836835    def test_get_like_requests(self):
    837836        # See: https://code.djangoproject.com/ticket/10571.
    838837        # 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'):
    840839            # A GET-like request can pass a query string as data
    841840            method = getattr(self.client, method_name)
    842841            response = method("/test_client_regress/request_data/", data={'foo':'whiz'})
  • django/test/client.py

    # 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  
    229229        else:
    230230            return urllib.unquote(parsed[2])
    231231
    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):
    233258        "Construct a GET request"
    234259
    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)
    244261
    245262    def post(self, path, data={}, content_type=MULTIPART_CONTENT,
    246263             **extra):
    247264        "Construct a POST request."
    248265
    249         post_data = self._encode_data(data, content_type)
     266        return self._generic_method('post', path, data, content_type, **extra)
    250267
    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):
    264269        "Construct a HEAD request."
    265270
    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)
    275272
    276     def options(self, path, data={}, **extra):
     273    def options(self, path, data={}, content_type='querystring', **extra):
    277274        "Constrict an OPTIONS request"
    278275
    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)
    287277
    288278    def put(self, path, data={}, content_type=MULTIPART_CONTENT,
    289279            **extra):
     
    301291            'wsgi.input':     FakePayload(put_data),
    302292        }
    303293        r.update(extra)
    304         return self.request(**r)
     294        #return self.request(**r)
     295        return self._generic_method('put', path, data, content_type, **extra)
    305296
    306     def delete(self, path, data={}, **extra):
     297    def delete(self, path, data={}, content_type='querystring', **extra):
    307298        "Construct a DELETE request."
    308299
    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)
    317301
    318302
    319303class Client(RequestFactory):
Back to Top