Ticket #4476: 4476_rev7412.diff

File 4476_rev7412.diff, 5.6 KB (added by Marc Fargas, 11 years ago)

Updated patch, used my version for being shorter ;))

  • django/test/client.py

    diff --git a/django/test/client.py b/django/test/client.py
    index bac28f7..f48560a 100644
    a b class Client: 
    204204
    205205        return response
    206206
    207     def get(self, path, data={}, **extra):
     207    def get(self, path, data={}, follow=False, **extra):
    208208        "Request a response from the server using GET."
    209209        r = {
    210210            'CONTENT_LENGTH':  None,
    class Client: 
    215215        }
    216216        r.update(extra)
    217217
    218         return self.request(**r)
     218        response = self.request(**r)
     219        if follow:
     220            responses = [response]
     221            if response.status_code in [301, 302, 303, 307]:
     222                new_url = response['Location'][17:]  # Strip the "http://testserver"
     223                responses = responses + self.get(new_url, {}, True)
     224            return responses
     225        else:
     226            return response
    219227
    220     def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
     228    def post(self, path, data={}, content_type=MULTIPART_CONTENT,
     229            follow=False, **extra):
    221230        "Request a response from the server using POST."
    222231
    223232        if content_type is MULTIPART_CONTENT:
    class Client: 
    234243        }
    235244        r.update(extra)
    236245
    237         return self.request(**r)
     246        response = self.request(**r)
     247        if follow:
     248            responses = [response]
     249            if response.status_code in [301, 302, 303, 307]:
     250                new_url = response['Location'][17:]  # Strip the "http://testserver"
     251                responses = responses + self.get(new_url, {}, True)
     252            return responses
     253        else:
     254            return response
    238255
    239256    def login(self, **credentials):
    240257        """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 0ff3cce..e2cef77 100644
    a b arguments at time of construction:: 
    460460
    461461Once you have a ``Client`` instance, you can call any of the following methods:
    462462
    463 ``get(path, data={})``
     463``get(path, data={}, follow=False)``
    464464    Makes a GET request on the provided ``path`` and returns a ``Response``
    465465    object, which is documented below.
    466466
    Once you have a ``Client`` instance, you can call any of the following methods: 
    474474
    475475        /customers/details/?name=fred&age=7
    476476
    477 ``post(path, data={}, content_type=MULTIPART_CONTENT)``
     477    If you set ``follow`` to ``True`` the client will follow any redirects
     478    and return an array containing all the responses obtained.
     479
     480``post(path, data={}, content_type=MULTIPART_CONTENT, follow=False)``
    478481    Makes a POST request on the provided ``path`` and returns a ``Response``
    479482    object, which is documented below.
    480483
    Once you have a ``Client`` instance, you can call any of the following methods: 
    523526    Note that you should manually close the file after it has been provided to
    524527    ``post()``.
    525528
     529    If you set ``follow`` to ``True`` the client will follow any redirects
     530    and return an array containing all the responses obtained.
     531
    526532``login(**credentials)``
    527533    **New in Django development version**
    528534
  • 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 305ccc9..45330c9 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 d3304ca..f70ca6a 100644
    a b  
    11from django.conf.urls.defaults import *
     2from django.views.generic.simple import redirect_to
    23import views
    34
    45urlpatterns = patterns('',
    urlpatterns = patterns('', 
    67    (r'^file_upload/$', views.file_upload_view),
    78    (r'^get_view/$', views.get_view),
    89    url(r'^arg_view/(?P<name>.+)/$', views.view_with_argument, name='arg_view'),
    9     (r'^login_protected_redirect_view/$', views.login_protected_redirect_view)
     10    (r'^login_protected_redirect_view/$', views.login_protected_redirect_view),
     11    (r'^redirects/$', redirect_to, {'url': 'further/'}),
     12    (r'^redirects/further/$', redirect_to, {'url': 'more/'}),
     13    (r'^redirects/further/more/$', redirect_to, {'url': 'end/'}),
    1014)
Back to Top