Loading django/test/client.py +13 −0 Original line number Diff line number Diff line Loading @@ -306,6 +306,10 @@ class RequestFactory(object): r.update(extra) return self.generic('HEAD', path, secure=secure, **r) def trace(self, path, secure=False, **extra): "Construct a TRACE request." return self.generic('TRACE', path, secure=secure, **extra) def options(self, path, data='', content_type='application/octet-stream', secure=False, **extra): "Construct an OPTIONS request." Loading Loading @@ -552,6 +556,15 @@ class Client(RequestFactory): response = self._handle_redirects(response, **extra) return response def trace(self, path, data='', follow=False, secure=False, **extra): """ Send a TRACE request to the server. """ response = super(Client, self).trace(path, data=data, secure=secure, **extra) if follow: response = self._handle_redirects(response, **extra) return response def login(self, **credentials): """ Sets the Factory to appear as if it has successfully logged into a site. Loading docs/releases/1.8.txt +4 −0 Original line number Diff line number Diff line Loading @@ -372,6 +372,10 @@ Requests and Responses Tests ^^^^^ * The :class:`RequestFactory.trace() <django.test.RequestFactory>` and :class:`Client.trace() <django.test.Client.trace>` methods were implemented, allowing you to create ``TRACE`` requests in your tests. * The ``count`` argument was added to :meth:`~django.test.SimpleTestCase.assertTemplateUsed`. This allows you to assert that a template was rendered a specific number of times. Loading docs/topics/testing/advanced.txt +2 −2 Original line number Diff line number Diff line Loading @@ -21,8 +21,8 @@ restricted subset of the test client API: * It only has access to the HTTP methods :meth:`~Client.get()`, :meth:`~Client.post()`, :meth:`~Client.put()`, :meth:`~Client.delete()`, :meth:`~Client.head()` and :meth:`~Client.options()`. :meth:`~Client.delete()`, :meth:`~Client.head()`, :meth:`~Client.options()`, and :meth:`~Client.trace()`. * These methods accept all the same arguments *except* for ``follows``. Since this is just a factory for producing Loading docs/topics/testing/tools.txt +14 −0 Original line number Diff line number Diff line Loading @@ -316,6 +316,20 @@ Use the ``django.test.Client`` class to make requests. The ``follow``, ``secure`` and ``extra`` arguments act the same as for :meth:`Client.get`. .. method:: Client.trace(path, follow=False, secure=False, **extra) .. versionadded:: 1.8 Makes a TRACE request on the provided ``path`` and returns a ``Response`` object. Useful for simulating diagnostic probes. Unlike the other request methods, ``data`` is not provided as a keyword parameter in order to comply with :rfc:`2616`, which mandates that TRACE requests should not have an entity-body. The ``follow``, ``secure``, and ``extra`` arguments act the same as for :meth:`Client.get`. .. method:: Client.login(**credentials) If your site uses Django's :doc:`authentication system</topics/auth/index>` Loading tests/test_client/tests.py +52 −3 Original line number Diff line number Diff line Loading @@ -23,10 +23,11 @@ rather than the HTML rendered to the end-user. from __future__ import unicode_literals from django.core import mail from django.http import HttpResponse from django.test import Client, TestCase, RequestFactory from django.test import override_settings from .views import get_view from .views import get_view, post_view, trace_view @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',), Loading Loading @@ -79,6 +80,13 @@ class ClientTest(TestCase): self.assertEqual(response.templates[0].name, 'POST Template') self.assertContains(response, 'Data received') def test_trace(self): """TRACE a view""" response = self.client.trace('/trace_view/') self.assertEqual(response.status_code, 200) self.assertEqual(response.context['method'], 'TRACE') self.assertEqual(response.templates[0].name, 'TRACE Template') def test_response_headers(self): "Check the value of HTTP headers returned in a response" response = self.client.get("/header_view/") Loading Loading @@ -552,13 +560,54 @@ class CustomTestClientTest(TestCase): self.assertEqual(hasattr(self.client, "i_am_customized"), True) _generic_view = lambda request: HttpResponse(status=200) @override_settings(ROOT_URLCONF='test_client.urls') class RequestFactoryTest(TestCase): """Tests for the request factory.""" # A mapping between names of HTTP/1.1 methods and their test views. http_methods_and_views = ( ('get', get_view), ('post', post_view), ('put', _generic_view), ('patch', _generic_view), ('delete', _generic_view), ('head', _generic_view), ('options', _generic_view), ('trace', trace_view), ) def setUp(self): self.request_factory = RequestFactory() def test_request_factory(self): factory = RequestFactory() request = factory.get('/somewhere/') """The request factory implements all the HTTP/1.1 methods.""" for method_name, view in self.http_methods_and_views: method = getattr(self.request_factory, method_name) request = method('/somewhere/') response = view(request) self.assertEqual(response.status_code, 200) def test_get_request_from_factory(self): """ The request factory returns a templated response for a GET request. """ request = self.request_factory.get('/somewhere/') response = get_view(request) self.assertEqual(response.status_code, 200) self.assertContains(response, 'This is a test') def test_trace_request_from_factory(self): """The request factory returns an echo response for a TRACE request.""" url_path = '/somewhere/' request = self.request_factory.trace(url_path) response = trace_view(request) protocol = request.META["SERVER_PROTOCOL"] echoed_request_line = "TRACE {} {}".format(url_path, protocol) self.assertEqual(response.status_code, 200) self.assertContains(response, echoed_request_line) Loading
django/test/client.py +13 −0 Original line number Diff line number Diff line Loading @@ -306,6 +306,10 @@ class RequestFactory(object): r.update(extra) return self.generic('HEAD', path, secure=secure, **r) def trace(self, path, secure=False, **extra): "Construct a TRACE request." return self.generic('TRACE', path, secure=secure, **extra) def options(self, path, data='', content_type='application/octet-stream', secure=False, **extra): "Construct an OPTIONS request." Loading Loading @@ -552,6 +556,15 @@ class Client(RequestFactory): response = self._handle_redirects(response, **extra) return response def trace(self, path, data='', follow=False, secure=False, **extra): """ Send a TRACE request to the server. """ response = super(Client, self).trace(path, data=data, secure=secure, **extra) if follow: response = self._handle_redirects(response, **extra) return response def login(self, **credentials): """ Sets the Factory to appear as if it has successfully logged into a site. Loading
docs/releases/1.8.txt +4 −0 Original line number Diff line number Diff line Loading @@ -372,6 +372,10 @@ Requests and Responses Tests ^^^^^ * The :class:`RequestFactory.trace() <django.test.RequestFactory>` and :class:`Client.trace() <django.test.Client.trace>` methods were implemented, allowing you to create ``TRACE`` requests in your tests. * The ``count`` argument was added to :meth:`~django.test.SimpleTestCase.assertTemplateUsed`. This allows you to assert that a template was rendered a specific number of times. Loading
docs/topics/testing/advanced.txt +2 −2 Original line number Diff line number Diff line Loading @@ -21,8 +21,8 @@ restricted subset of the test client API: * It only has access to the HTTP methods :meth:`~Client.get()`, :meth:`~Client.post()`, :meth:`~Client.put()`, :meth:`~Client.delete()`, :meth:`~Client.head()` and :meth:`~Client.options()`. :meth:`~Client.delete()`, :meth:`~Client.head()`, :meth:`~Client.options()`, and :meth:`~Client.trace()`. * These methods accept all the same arguments *except* for ``follows``. Since this is just a factory for producing Loading
docs/topics/testing/tools.txt +14 −0 Original line number Diff line number Diff line Loading @@ -316,6 +316,20 @@ Use the ``django.test.Client`` class to make requests. The ``follow``, ``secure`` and ``extra`` arguments act the same as for :meth:`Client.get`. .. method:: Client.trace(path, follow=False, secure=False, **extra) .. versionadded:: 1.8 Makes a TRACE request on the provided ``path`` and returns a ``Response`` object. Useful for simulating diagnostic probes. Unlike the other request methods, ``data`` is not provided as a keyword parameter in order to comply with :rfc:`2616`, which mandates that TRACE requests should not have an entity-body. The ``follow``, ``secure``, and ``extra`` arguments act the same as for :meth:`Client.get`. .. method:: Client.login(**credentials) If your site uses Django's :doc:`authentication system</topics/auth/index>` Loading
tests/test_client/tests.py +52 −3 Original line number Diff line number Diff line Loading @@ -23,10 +23,11 @@ rather than the HTML rendered to the end-user. from __future__ import unicode_literals from django.core import mail from django.http import HttpResponse from django.test import Client, TestCase, RequestFactory from django.test import override_settings from .views import get_view from .views import get_view, post_view, trace_view @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',), Loading Loading @@ -79,6 +80,13 @@ class ClientTest(TestCase): self.assertEqual(response.templates[0].name, 'POST Template') self.assertContains(response, 'Data received') def test_trace(self): """TRACE a view""" response = self.client.trace('/trace_view/') self.assertEqual(response.status_code, 200) self.assertEqual(response.context['method'], 'TRACE') self.assertEqual(response.templates[0].name, 'TRACE Template') def test_response_headers(self): "Check the value of HTTP headers returned in a response" response = self.client.get("/header_view/") Loading Loading @@ -552,13 +560,54 @@ class CustomTestClientTest(TestCase): self.assertEqual(hasattr(self.client, "i_am_customized"), True) _generic_view = lambda request: HttpResponse(status=200) @override_settings(ROOT_URLCONF='test_client.urls') class RequestFactoryTest(TestCase): """Tests for the request factory.""" # A mapping between names of HTTP/1.1 methods and their test views. http_methods_and_views = ( ('get', get_view), ('post', post_view), ('put', _generic_view), ('patch', _generic_view), ('delete', _generic_view), ('head', _generic_view), ('options', _generic_view), ('trace', trace_view), ) def setUp(self): self.request_factory = RequestFactory() def test_request_factory(self): factory = RequestFactory() request = factory.get('/somewhere/') """The request factory implements all the HTTP/1.1 methods.""" for method_name, view in self.http_methods_and_views: method = getattr(self.request_factory, method_name) request = method('/somewhere/') response = view(request) self.assertEqual(response.status_code, 200) def test_get_request_from_factory(self): """ The request factory returns a templated response for a GET request. """ request = self.request_factory.get('/somewhere/') response = get_view(request) self.assertEqual(response.status_code, 200) self.assertContains(response, 'This is a test') def test_trace_request_from_factory(self): """The request factory returns an echo response for a TRACE request.""" url_path = '/somewhere/' request = self.request_factory.trace(url_path) response = trace_view(request) protocol = request.META["SERVER_PROTOCOL"] echoed_request_line = "TRACE {} {}".format(url_path, protocol) self.assertEqual(response.status_code, 200) self.assertContains(response, echoed_request_line)