Ticket #5888: 5888-r9050.diff

File 5888-r9050.diff, 6.9 KB (added by Leah Culver, 16 years ago)

Added tests and docs to previous patch

  • django/test/client.py

     
    284284
    285285        return self.request(**r)
    286286
     287    def head(self, path, data={}, **extra):
     288        """
     289        Request a response from the server using HEAD.
     290        """
     291        r = {
     292            'CONTENT_LENGTH':  None,
     293            'CONTENT_TYPE':    'text/html; charset=utf-8',
     294            'PATH_INFO':       urllib.unquote(path),
     295            'QUERY_STRING':    urlencode(data, doseq=True),
     296            'REQUEST_METHOD': 'HEAD',
     297        }
     298        r.update(extra)
     299
     300        return self.request(**r)
     301       
     302    def options(self, path, data={}, **extra):
     303        """
     304        Request a response from the server using OPTIONS.
     305        """
     306        r = {
     307            'CONTENT_LENGTH':  None,
     308            'CONTENT_TYPE':    None,
     309            'PATH_INFO':       urllib.unquote(path),
     310            'QUERY_STRING':    urlencode(data, doseq=True),
     311            'REQUEST_METHOD': 'OPTIONS',
     312        }
     313        r.update(extra)
     314
     315        return self.request(**r)
     316
     317    def put(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
     318        """
     319        Send a resource to the server using PUT.
     320        """
     321        if content_type is MULTIPART_CONTENT:
     322            post_data = encode_multipart(BOUNDARY, data)
     323        else:
     324            post_data = data
     325        r = {
     326            'CONTENT_LENGTH': len(post_data),
     327            'CONTENT_TYPE':   content_type,
     328            'PATH_INFO':      urllib.unquote(path),
     329            'REQUEST_METHOD': 'PUT',
     330            'wsgi.input':     FakePayload(post_data),
     331        }
     332        r.update(extra)
     333
     334        return self.request(**r)
     335
     336    def delete(self, path, data={}, **extra):
     337        """
     338        Send a DELETE request to the server.
     339        """
     340        r = {
     341            'CONTENT_LENGTH':  None,
     342            'CONTENT_TYPE':    None,
     343            'PATH_INFO':       urllib.unquote(path),
     344            'REQUEST_METHOD': 'DELETE',
     345            }
     346        r.update(extra)
     347
     348        return self.request(**r)
     349
    287350    def login(self, **credentials):
    288351        """
    289352        Sets the Client to appear as if it has successfully logged into a site.
  • tests/regressiontests/test_client_regress/views.py

     
    4343def check_session_view(request):
    4444    "A view that reads a session variable"
    4545    return HttpResponse(request.session.get('session_var', 'NO'))
     46
     47def request_methods_view(request):
     48    "A view that responds with the request method"
     49    return HttpResponse('request method: %s' % request.method)
     50 No newline at end of file
  • tests/regressiontests/test_client_regress/models.py

     
    382382        response = self.client.get('/test_client_regress/check_session/')
    383383        self.assertEqual(response.status_code, 200)
    384384        self.assertEqual(response.content, 'YES')
    385        
    386  No newline at end of file
     385
     386class RequestMethodTests(TestCase):
     387    def test_get(self):
     388        "Request a view via request method GET"
     389        response = self.client.get('/test_client_regress/request_methods/')
     390        self.assertEqual(response.status_code, 200)
     391        self.assertEqual(response.content, 'request method: GET')
     392
     393    def test_post(self):
     394        "Request a view via request method POST"
     395        response = self.client.post('/test_client_regress/request_methods/')
     396        self.assertEqual(response.status_code, 200)
     397        self.assertEqual(response.content, 'request method: POST')
     398
     399    def test_head(self):
     400        "Request a view via request method HEAD"
     401        response = self.client.head('/test_client_regress/request_methods/')
     402        self.assertEqual(response.status_code, 200)
     403        self.assertNotEqual(response.content, 'request method: HEAD')
     404        self.assertEqual(response.content, '')
     405
     406    def test_options(self):
     407        "Request a view via request method OPTIONS"
     408        response = self.client.options('/test_client_regress/request_methods/')
     409        self.assertEqual(response.status_code, 200)
     410        self.assertEqual(response.content, 'request method: OPTIONS')
     411
     412    def test_put(self):
     413        "Request a view via request method PUT"
     414        response = self.client.put('/test_client_regress/request_methods/')
     415        self.assertEqual(response.status_code, 200)
     416        self.assertEqual(response.content, 'request method: PUT')
     417
     418    def test_delete(self):
     419        "Request a view via request method DELETE"
     420        response = self.client.delete('/test_client_regress/request_methods/')
     421        self.assertEqual(response.status_code, 200)
     422        self.assertEqual(response.content, 'request method: DELETE')
     423 No newline at end of file
  • tests/regressiontests/test_client_regress/urls.py

     
    99    (r'^login_protected_redirect_view/$', views.login_protected_redirect_view),
    1010    (r'^set_session/$', views.set_session_view),
    1111    (r'^check_session/$', views.check_session_view),
     12    (r'^request_methods/$', views.request_methods_view),
    1213)
  • docs/topics/testing.txt

     
    544544        Note that you should manually close the file after it has been provided
    545545        to ``post()``.
    546546
     547    .. method:: Client.head(path, data={})
     548
     549        Makes a HEAD request on the provided ``path`` and returns a ``Response``
     550        object. Useful for testing RESTful interfaces. Acts just like
     551        :meth:`Client.get` except it does not return a message body.
     552
     553    .. method:: Client.options(path, data={})
     554
     555        Makes an OPTIONS request on the provided ``path`` and returns a
     556        ``Response`` object. Useful for testing RESTful interfaces.
     557
     558    .. method:: Client.put(path, data={}, content_type=MULTIPART_CONTENT)
     559
     560        Makes an PUT request on the provided ``path`` and returns a
     561        ``Response`` object. Useful for testing RESTful interfaces. Acts just
     562        like :meth:`Client.put` except with the PUT request method.
     563
     564    .. method:: Client.delete(path)
     565
     566        Makes an DELETE request on the provided ``path`` and returns a
     567        ``Response`` object. Useful for testing RESTful interfaces.
     568
    547569    .. method:: Client.login(**credentials)
    548570
    549571        .. versionadded:: 1.0
Back to Top