Author: russellm
Date: 2007-05-10 06:27:59 -0500 (Thu, 10 May 2007)
New Revision: 5179
Modified:
django/trunk/django/test/testcases.py
django/trunk/docs/testing.txt
django/trunk/tests/modeltests/test_client/models.py
django/trunk/tests/modeltests/test_client/urls.py
django/trunk/tests/modeltests/test_client/views.py
Log:
Added configurable arguments to assertRedirects and assertContains to allow for
other response status codes. Thanks for the suggestion, Jiri Barton.
Modified: django/trunk/django/test/testcases.py
===================================================================
--- django/trunk/django/test/testcases.py 2007-05-09 15:28:16 UTC (rev
5178)
+++ django/trunk/django/test/testcases.py 2007-05-10 11:27:59 UTC (rev
5179)
@@ -56,32 +56,35 @@
self._pre_setup()
super(TestCase, self).run(result)
- def assertRedirects(self, response, expected_path):
- """Assert that a response redirected to a specific URL, and that the
+ def assertRedirects(self, response, expected_path, status_code=302,
target_status_code=200):
+ """Assert that a response redirected to a specific URL, and that the
redirect URL can be loaded.
"""
- self.assertEqual(response.status_code, 302,
- "Response didn't redirect: Reponse code was %d" %
response.status_code)
+ self.assertEqual(response.status_code, status_code,
+ "Response didn't redirect: Reponse code was %d (expected %d)" %
+ (response.status_code, status_code))
scheme, netloc, path, params, query, fragment =
urlparse(response['Location'])
self.assertEqual(path, expected_path,
"Response redirected to '%s', expected '%s'" % (path,
expected_path))
redirect_response = self.client.get(path)
- self.assertEqual(redirect_response.status_code, 200,
- "Couldn't retrieve redirection page '%s'" % path)
+ self.assertEqual(redirect_response.status_code, target_status_code,
+ "Couldn't retrieve redirection page '%s': response code was %d
(expected %d)" %
+ (path, response.status_code, status_code))
- def assertContains(self, response, text, count=1):
+ def assertContains(self, response, text, count=1, status_code=200):
"""Assert that a response indicates that a page was retreived
successfully,
- (i.e., the HTTP status code was 200), and that ``text`` occurs
``count``
+ (i.e., the HTTP status code was as expected), and that ``text`` occurs
``count``
times in the content of the response.
"""
- self.assertEqual(response.status_code, 200,
- "Couldn't retrieve page'")
+ self.assertEqual(response.status_code, status_code,
+ "Couldn't retrieve page: Response code was %d (expected %d)'" %
+ (response.status_code, status_code))
real_count = response.content.count(text)
self.assertEqual(real_count, count,
- "Could only find %d of %d instances of '%s' in response" %
(real_count, count, text))
-
+ "Found %d instances of '%s' in response (expected %d)" %
(real_count, text, count))
+
def assertFormError(self, response, form, field, errors):
"Assert that a form used to render the response has a specific field
error"
if not response.context:
Modified: django/trunk/docs/testing.txt
===================================================================
--- django/trunk/docs/testing.txt 2007-05-09 15:28:16 UTC (rev 5178)
+++ django/trunk/docs/testing.txt 2007-05-10 11:27:59 UTC (rev 5179)
@@ -472,9 +472,9 @@
``django.TestCase`` adds to these, providing some assertions
that can be useful in testing the behavior of web sites.
-``assertContains(response, text, count=1)``
- Assert that a response indicates that a page was retrieved successfully,
- (i.e., the HTTP status code was 200), and that ``text`` occurs ``count``
+``assertContains(response, text, count=1, status_code=200)``
+ Assert that a response indicates that a page could be retrieved and
+ produced the nominated status code, and that ``text`` occurs ``count``
times in the content of the response.
``assertFormError(response, form, field, errors)``
@@ -493,9 +493,10 @@
Assert that the template with the given name was *not* used in rendering
the response.
-``assertRedirects(response, expected_path)``
- Assert that the response received redirects the browser to the provided
- path, and that the expected_path can be retrieved.
+``assertRedirects(response, expected_path, status_code=302,
target_status_code=200)``
+ Assert that the response received produced the nominated status code,
+ redirects the browser to the provided path, and that retrieving the
provided
+ path yields a response with the target status code.
``assertTemplateUsed(response, template_name)``
Assert that the template with the given name was used in rendering the
Modified: django/trunk/tests/modeltests/test_client/models.py
===================================================================
--- django/trunk/tests/modeltests/test_client/models.py 2007-05-09 15:28:16 UTC
(rev 5178)
+++ django/trunk/tests/modeltests/test_client/models.py 2007-05-10 11:27:59 UTC
(rev 5179)
@@ -35,7 +35,7 @@
self.assertEqual(response.template.name, 'GET Template')
def test_no_template_view(self):
- "Check that template usage assersions work then templates aren't in
use"
+ "Template usage assertions work then templates aren't in use"
response = self.client.get('/test_client/no_template_view/')
# Check that the no template case doesn't mess with the template
assertions
@@ -75,6 +75,7 @@
self.failUnless('Data received' in response.content)
def test_raw_post(self):
+ "POST raw data (with a content type) to a view"
test_doc = """<?xml version="1.0"
encoding="utf-8"?><library><book><title>Blink</title><author>Malcolm
Gladwell</author></book></library>"""
response = self.client.post("/test_client/raw_post_view/", test_doc,
content_type="text/xml")
@@ -89,6 +90,28 @@
# Check that the response was a 302 (redirect)
self.assertRedirects(response, '/test_client/get_view/')
+ def test_permanent_redirect(self):
+ "GET a URL that redirects permanently elsewhere"
+ response = self.client.get('/test_client/permanent_redirect_view/')
+
+ # Check that the response was a 301 (permanent redirect)
+ self.assertRedirects(response, '/test_client/get_view/',
status_code=301)
+
+ def test_redirect_to_strange_location(self):
+ "GET a URL that redirects to a non-200 page"
+ response = self.client.get('/test_client/double_redirect_view/')
+
+ # Check that the response was a 302, and that
+ # the attempt to get the redirection location returned 301 when
retrieved
+ self.assertRedirects(response,
'/test_client/permanent_redirect_view/', target_status_code=301)
+
+ def test_notfound_response(self):
+ "GET a URL that responds as '404:Not Found'"
+ response = self.client.get('/test_client/bad_view/')
+
+ # Check that the response was a 404, and that the content contains
MAGIC
+ self.assertContains(response, 'MAGIC', status_code=404)
+
def test_valid_form(self):
"POST valid data to a form"
post_data = {
Modified: django/trunk/tests/modeltests/test_client/urls.py
===================================================================
--- django/trunk/tests/modeltests/test_client/urls.py 2007-05-09 15:28:16 UTC
(rev 5178)
+++ django/trunk/tests/modeltests/test_client/urls.py 2007-05-10 11:27:59 UTC
(rev 5179)
@@ -1,4 +1,5 @@
from django.conf.urls.defaults import *
+from django.views.generic.simple import redirect_to
import views
urlpatterns = patterns('',
@@ -7,6 +8,9 @@
(r'^post_view/$', views.post_view),
(r'^raw_post_view/$', views.raw_post_view),
(r'^redirect_view/$', views.redirect_view),
+ (r'^permanent_redirect_view/$', redirect_to, { 'url':
'/test_client/get_view/' }),
+ (r'^double_redirect_view/$', views.double_redirect_view),
+ (r'^bad_view/$', views.bad_view),
(r'^form_view/$', views.form_view),
(r'^form_view_with_template/$', views.form_view_with_template),
(r'^login_protected_view/$', views.login_protected_view),
Modified: django/trunk/tests/modeltests/test_client/views.py
===================================================================
--- django/trunk/tests/modeltests/test_client/views.py 2007-05-09 15:28:16 UTC
(rev 5178)
+++ django/trunk/tests/modeltests/test_client/views.py 2007-05-10 11:27:59 UTC
(rev 5179)
@@ -1,7 +1,7 @@
from xml.dom.minidom import parseString
from django.core.mail import EmailMessage, SMTPConnection
from django.template import Context, Template
-from django.http import HttpResponse, HttpResponseRedirect
+from django.http import HttpResponse, HttpResponseRedirect,
HttpResponseNotFound
from django.contrib.auth.decorators import login_required
from django.newforms.forms import Form
from django.newforms import fields
@@ -54,6 +54,14 @@
"A view that redirects all requests to the GET view"
return HttpResponseRedirect('/test_client/get_view/')
+def double_redirect_view(request):
+ "A view that redirects all requests to a redirection view"
+ return HttpResponseRedirect('/test_client/permanent_redirect_view/')
+
+def bad_view(request):
+ "A view that returns a 404 with some error content"
+ return HttpResponseNotFound('Not found!. This page contains some MAGIC
content')
+
TestChoices = (
('a', 'First Choice'),
('b', 'Second Choice'),
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---