Ticket #4476: 4476.diff

File 4476.diff, 5.5 KB (added by Marc Fargas, 12 years ago)

Patch with docs+tests

  • django/test/client.py

    diff --git a/django/test/client.py b/django/test/client.py
    index bbd8239..4e76335 100644
    a b class Client: 
    205205
    206206        return response
    207207
    208     def get(self, path, data={}, **extra):
     208    def get(self, path, data={}, follow=False, **extra):
    209209        "Request a response from the server using GET."
    210210        r = {
    211211            'CONTENT_LENGTH':  None,
    class Client: 
    216216        }
    217217        r.update(extra)
    218218
    219         return self.request(**r)
     219        response = self.request(**r)
     220        if follow:
     221            responses = [response]
     222            if response.status_code in [301, 302]:
     223                new_url = response['Location'][17:]  # Strip the "http://testserver"
     224                responses = responses + self.get(new_url, {}, True)
     225            return responses
     226        else:
     227            return response
    220228
    221     def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
     229    def post(self, path, data={}, content_type=MULTIPART_CONTENT,
     230            follow=False, **extra):
    222231        "Request a response from the server using POST."
    223232
    224233        if content_type is MULTIPART_CONTENT:
    class Client: 
    235244        }
    236245        r.update(extra)
    237246
    238         return self.request(**r)
     247        response = self.request(**r)
     248        if follow:
     249            responses = [response]
     250            if response.status_code in [301, 302]:
     251                new_url = response['Location'][17:]  # Strip the "http://testserver"
     252                responses = responses + self.get(new_url, {}, True)
     253            return responses
     254        else:
     255            return response
    239256
    240257    def login(self, **credentials):
    241258        """Set the Client to appear as if it has sucessfully logged into a site.
  • docs/testing.txt

    diff --git a/docs/testing.txt b/docs/testing.txt
    index 7705380..0e941d9 100644
    a b arguments at time of construction:: 
    453453
    454454Once you have a ``Client`` instance, you can call any of the following methods:
    455455
    456 ``get(path, data={})``
     456``get(path, data={}, follow=False)``
    457457    Makes a GET request on the provided ``path`` and returns a ``Response``
    458458    object, which is documented below.
    459459
    Once you have a ``Client`` instance, you can call any of the following methods: 
    467467
    468468        /customers/details/?name=fred&age=7
    469469
    470 ``post(path, data={}, content_type=MULTIPART_CONTENT)``
     470    If you set ``follow`` to ``True`` the client will follow any redirects
     471    and return an array containing all the responses obtained.
     472
     473``post(path, data={}, content_type=MULTIPART_CONTENT, follow=False)``
    471474    Makes a POST request on the provided ``path`` and returns a ``Response``
    472475    object, which is documented below.
    473476
    Once you have a ``Client`` instance, you can call any of the following methods: 
    516519    Note that you should manually close the file after it has been provided to
    517520    ``post()``.
    518521
     522    If you set ``follow`` to ``True`` the client will follow any redirects
     523    and return an array containing all the responses obtained.
     524
    519525``login(**credentials)``
    520526    **New in Django development version**
    521527
  • tests/regressiontests/test_client_regress/models.py

    diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py
    index b5d9ae6..6f3f4ac 100644
    a b class AssertRedirectsTests(TestCase): 
    139139        except AssertionError, e:
    140140            self.assertEquals(str(e), "Couldn't retrieve redirection page '/test_client/permanent_redirect_view/': response code was 301 (expected 200)")
    141141
     142    def test_redirect_chain(self):
     143        # First with GET
     144        responses = self.client.get('/test_client_regress/redirects/', {}, True)
     145        self.assertEquals(len(responses), 4)
     146        self.assertRedirects(responses[0],
     147            '/test_client_regress/redirects/further/', 301, 301)
     148        self.assertRedirects(responses[1],
     149            '/test_client_regress/redirects/further/more/', 301, 301)
     150        self.assertRedirects(responses[2],
     151            '/test_client_regress/redirects/further/more/end/', 301, 404)
     152       
     153        # Now with POST
     154        responses = self.client.post('/test_client_regress/redirects/',
     155            {'nothing': 'to_send'}, follow=True)
     156        self.assertEquals(len(responses), 4)
     157        self.assertRedirects(responses[0],
     158            '/test_client_regress/redirects/further/', 301, 301)
     159
    142160class AssertFormErrorTests(TestCase):
    143161    def test_unknown_form(self):
    144162        "An assertion is raised if the form name is unknown"
  • tests/regressiontests/test_client_regress/urls.py

    diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py
    index e771707..5d93b2c 100644
    a b  
    11from django.conf.urls.defaults import *
     2from django.views.generic.simple import redirect_to
    23import views
    34
    45urlpatterns = patterns('',
    56    (r'^no_template_view/$', views.no_template_view),
    67    (r'^file_upload/$', views.file_upload_view),
    78    (r'^get_view/$', views.get_view),
    8     (r'^login_protected_redirect_view/$', views.login_protected_redirect_view)
     9    (r'^login_protected_redirect_view/$', views.login_protected_redirect_view),
     10    (r'^redirects/$', redirect_to, {'url': 'further/'}),
     11    (r'^redirects/further/$', redirect_to, {'url': 'more/'}),
     12    (r'^redirects/further/more/$', redirect_to, {'url': 'end/'}),
    913)
Back to Top