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