Django

Code

Changeset 5179

Show
Ignore:
Timestamp:
05/10/07 06:27:59 (2 years ago)
Author:
russellm
Message:

Added configurable arguments to assertRedirects and assertContains to allow for other response status codes. Thanks for the suggestion, Jiri Barton.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/testcases.py

    r5173 r5179  
    5757        super(TestCase, self).run(result) 
    5858 
    59     def assertRedirects(self, response, expected_path): 
    60         """Assert that a response redirected to a specific URL, and that the  
     59    def assertRedirects(self, response, expected_path, status_code=302, target_status_code=200): 
     60        """Assert that a response redirected to a specific URL, and that the 
    6161        redirect URL can be loaded. 
    6262         
    6363        """ 
    64         self.assertEqual(response.status_code, 302,  
    65             "Response didn't redirect: Reponse code was %d" % response.status_code) 
     64        self.assertEqual(response.status_code, status_code,  
     65            "Response didn't redirect: Reponse code was %d (expected %d)" %  
     66                (response.status_code, status_code)) 
    6667        scheme, netloc, path, params, query, fragment = urlparse(response['Location']) 
    6768        self.assertEqual(path, expected_path,  
    6869            "Response redirected to '%s', expected '%s'" % (path, expected_path)) 
    6970        redirect_response = self.client.get(path) 
    70         self.assertEqual(redirect_response.status_code, 200,  
    71             "Couldn't retrieve redirection page '%s'" % path) 
     71        self.assertEqual(redirect_response.status_code, target_status_code,  
     72            "Couldn't retrieve redirection page '%s': response code was %d (expected %d)" %  
     73                (path, response.status_code, status_code)) 
    7274     
    73     def assertContains(self, response, text, count=1): 
     75    def assertContains(self, response, text, count=1, status_code=200): 
    7476        """Assert that a response indicates that a page was retreived successfully, 
    75         (i.e., the HTTP status code was 200), and that ``text`` occurs ``count``  
     77        (i.e., the HTTP status code was as expected), and that ``text`` occurs ``count`` 
    7678        times in the content of the response. 
    7779         
    7880        """ 
    79         self.assertEqual(response.status_code, 200, 
    80             "Couldn't retrieve page'") 
     81        self.assertEqual(response.status_code, status_code, 
     82            "Couldn't retrieve page: Response code was %d (expected %d)'" %  
     83                (response.status_code, status_code)) 
    8184        real_count = response.content.count(text) 
    8285        self.assertEqual(real_count, count, 
    83             "Could only find %d of %d instances of '%s' in response" % (real_count, count, text)) 
    84  
     86            "Found %d instances of '%s' in response (expected %d)" % (real_count, text, count)) 
     87     
    8588    def assertFormError(self, response, form, field, errors): 
    8689        "Assert that a form used to render the response has a specific field error" 
  • django/trunk/docs/testing.txt

    r5173 r5179  
    473473that can be useful in testing the behavior of web sites. 
    474474 
    475 ``assertContains(response, text, count=1)`` 
    476     Assert that a response indicates that a page was retrieved successfully, 
    477     (i.e., the HTTP status code was 200), and that ``text`` occurs ``count`` 
     475``assertContains(response, text, count=1, status_code=200)`` 
     476    Assert that a response indicates that a page could be retrieved and 
     477    produced the nominated status code, and that ``text`` occurs ``count`` 
    478478    times in the content of the response. 
    479479 
     
    494494    the response. 
    495495 
    496 ``assertRedirects(response, expected_path)`` 
    497     Assert that the response received redirects the browser to the provided 
    498     path, and that the expected_path can be retrieved. 
     496``assertRedirects(response, expected_path, status_code=302, target_status_code=200)`` 
     497    Assert that the response received produced the nominated status code, 
     498    redirects the browser to the provided path, and that retrieving the provided 
     499    path yields a response with the target status code. 
    499500 
    500501``assertTemplateUsed(response, template_name)`` 
  • django/trunk/tests/modeltests/test_client/models.py

    r5173 r5179  
    3636 
    3737    def test_no_template_view(self): 
    38         "Check that template usage assersions work then templates aren't in use" 
     38        "Template usage assertions work then templates aren't in use" 
    3939        response = self.client.get('/test_client/no_template_view/') 
    4040 
     
    7676         
    7777    def test_raw_post(self): 
     78        "POST raw data (with a content type) to a view" 
    7879        test_doc = """<?xml version="1.0" encoding="utf-8"?><library><book><title>Blink</title><author>Malcolm Gladwell</author></book></library>""" 
    7980        response = self.client.post("/test_client/raw_post_view/", test_doc, 
     
    8990        # Check that the response was a 302 (redirect) 
    9091        self.assertRedirects(response, '/test_client/get_view/') 
     92 
     93    def test_permanent_redirect(self): 
     94        "GET a URL that redirects permanently elsewhere" 
     95        response = self.client.get('/test_client/permanent_redirect_view/') 
     96         
     97        # Check that the response was a 301 (permanent redirect) 
     98        self.assertRedirects(response, '/test_client/get_view/', status_code=301) 
     99 
     100    def test_redirect_to_strange_location(self): 
     101        "GET a URL that redirects to a non-200 page" 
     102        response = self.client.get('/test_client/double_redirect_view/') 
     103         
     104        # Check that the response was a 302, and that 
     105        # the attempt to get the redirection location returned 301 when retrieved 
     106        self.assertRedirects(response, '/test_client/permanent_redirect_view/', target_status_code=301) 
     107 
     108    def test_notfound_response(self): 
     109        "GET a URL that responds as '404:Not Found'" 
     110        response = self.client.get('/test_client/bad_view/') 
     111         
     112        # Check that the response was a 404, and that the content contains MAGIC 
     113        self.assertContains(response, 'MAGIC', status_code=404) 
    91114 
    92115    def test_valid_form(self): 
  • django/trunk/tests/modeltests/test_client/urls.py

    r5173 r5179  
    11from django.conf.urls.defaults import * 
     2from django.views.generic.simple import redirect_to 
    23import views 
    34 
     
    89    (r'^raw_post_view/$', views.raw_post_view), 
    910    (r'^redirect_view/$', views.redirect_view), 
     11    (r'^permanent_redirect_view/$', redirect_to, { 'url': '/test_client/get_view/' }), 
     12    (r'^double_redirect_view/$', views.double_redirect_view), 
     13    (r'^bad_view/$', views.bad_view), 
    1014    (r'^form_view/$', views.form_view), 
    1115    (r'^form_view_with_template/$', views.form_view_with_template), 
  • django/trunk/tests/modeltests/test_client/views.py

    r5173 r5179  
    22from django.core.mail import EmailMessage, SMTPConnection 
    33from django.template import Context, Template 
    4 from django.http import HttpResponse, HttpResponseRedirect 
     4from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound 
    55from django.contrib.auth.decorators import login_required 
    66from django.newforms.forms import Form 
     
    5454    "A view that redirects all requests to the GET view" 
    5555    return HttpResponseRedirect('/test_client/get_view/') 
     56 
     57def double_redirect_view(request): 
     58    "A view that redirects all requests to a redirection view" 
     59    return HttpResponseRedirect('/test_client/permanent_redirect_view/') 
     60 
     61def bad_view(request): 
     62    "A view that returns a 404 with some error content" 
     63    return HttpResponseNotFound('Not found!. This page contains some MAGIC content') 
    5664 
    5765TestChoices = (