PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/views/tests/static.py

https://code.google.com/p/mango-py/
Python | 103 lines | 72 code | 15 blank | 16 comment | 2 complexity | 43cd3b412bc13f24e65ed917cba5ec3b MD5 | raw file
  1import mimetypes
  2from os import path
  3
  4from django.conf import settings
  5from django.conf.urls.static import static
  6from django.test import TestCase
  7from django.http import HttpResponseNotModified
  8
  9from regressiontests.views import urls
 10from regressiontests.views.urls import media_dir
 11
 12class StaticTests(TestCase):
 13    """Tests django views in django/views/static.py"""
 14
 15    def setUp(self):
 16        self.prefix = 'site_media'
 17        self.old_debug = settings.DEBUG
 18        settings.DEBUG = True
 19
 20    def tearDown(self):
 21        settings.DEBUG = self.old_debug
 22
 23    def test_serve(self):
 24        "The static view can serve static media"
 25        media_files = ['file.txt', 'file.txt.gz']
 26        for filename in media_files:
 27            response = self.client.get('/views/%s/%s' % (self.prefix, filename))
 28            file_path = path.join(media_dir, filename)
 29            self.assertEqual(open(file_path).read(), response.content)
 30            self.assertEqual(len(response.content), int(response['Content-Length']))
 31            self.assertEqual(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None))
 32
 33    def test_unknown_mime_type(self):
 34        response = self.client.get('/views/%s/file.unknown' % self.prefix)
 35        self.assertEqual('application/octet-stream', response['Content-Type'])
 36
 37    def test_copes_with_empty_path_component(self):
 38        file_name = 'file.txt'
 39        response = self.client.get('/views/%s//%s' % (self.prefix, file_name))
 40        file = open(path.join(media_dir, file_name))
 41        self.assertEqual(file.read(), response.content)
 42
 43    def test_is_modified_since(self):
 44        file_name = 'file.txt'
 45        response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
 46            HTTP_IF_MODIFIED_SINCE='Thu, 1 Jan 1970 00:00:00 GMT')
 47        file = open(path.join(media_dir, file_name))
 48        self.assertEqual(file.read(), response.content)
 49
 50    def test_not_modified_since(self):
 51        file_name = 'file.txt'
 52        response = self.client.get(
 53            '/views/%s/%s' % (self.prefix, file_name),
 54            HTTP_IF_MODIFIED_SINCE='Mon, 18 Jan 2038 05:14:07 GMT'
 55            # This is 24h before max Unix time. Remember to fix Django and
 56            # update this test well before 2038 :)
 57            )
 58        self.assertTrue(isinstance(response, HttpResponseNotModified))
 59
 60    def test_invalid_if_modified_since(self):
 61        """Handle bogus If-Modified-Since values gracefully
 62
 63        Assume that a file is modified since an invalid timestamp as per RFC
 64        2616, section 14.25.
 65        """
 66        file_name = 'file.txt'
 67        invalid_date = 'Mon, 28 May 999999999999 28:25:26 GMT'
 68        response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
 69                                   HTTP_IF_MODIFIED_SINCE=invalid_date)
 70        file = open(path.join(media_dir, file_name))
 71        self.assertEqual(file.read(), response.content)
 72        self.assertEqual(len(response.content),
 73                          int(response['Content-Length']))
 74
 75    def test_invalid_if_modified_since2(self):
 76        """Handle even more bogus If-Modified-Since values gracefully
 77
 78        Assume that a file is modified since an invalid timestamp as per RFC
 79        2616, section 14.25.
 80        """
 81        file_name = 'file.txt'
 82        invalid_date = ': 1291108438, Wed, 20 Oct 2010 14:05:00 GMT'
 83        response = self.client.get('/views/%s/%s' % (self.prefix, file_name),
 84                                   HTTP_IF_MODIFIED_SINCE=invalid_date)
 85        file = open(path.join(media_dir, file_name))
 86        self.assertEqual(file.read(), response.content)
 87        self.assertEqual(len(response.content),
 88                          int(response['Content-Length']))
 89
 90
 91class StaticHelperTest(StaticTests):
 92    """
 93    Test case to make sure the static URL pattern helper works as expected
 94    """
 95    def setUp(self):
 96        super(StaticHelperTest, self).setUp()
 97        self.prefix = 'media'
 98        self._old_views_urlpatterns = urls.urlpatterns[:]
 99        urls.urlpatterns += static('/media/', document_root=media_dir)
100
101    def tearDown(self):
102        super(StaticHelperTest, self).tearDown()
103        urls.urlpatterns = self._old_views_urlpatterns