from django.http import HttpResponsePermanentRedirect
from django.test.client import Client
class MyTestClass(unittest.TestCase):
def test_my_method(self):
client = Client()
response = client.post('/some_url/')
self.assertEqual(response.status_code, 301)
self.assertTrue(isinstance(response, HttpResponsePermanentRedirect))
self.assertEqual(response.META['HTTP_LOCATION'], '/url_we_expect_to_be_redirected_to/')
There are other response attributes that might be interesting for testing. If you do not know what is on the object, you can always do
print dir(response)
EDIT CURRENT VERSIONS OF DJANGO
Now this is a little easier, just do:
self.assertEqual(response.get('location'), '/url/we/expect')
I also suggest using the opposite to search for the URL that you expect on behalf of if that is the URL in your application.
source
share