From ada08cd6d824e4cee976d3c3b68b0d153b403eee Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Wed, 16 Feb 2011 12:50:57 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#15314=20--=20Added=20tests=20for=20the?= =?UTF-8?q?=20static=20URL=20pattern=20function=20added=20in=20r15530=20an?= =?UTF-8?q?d=20made=20sure=20the=20**kwargs=20are=20passed=20to=20the=20vi?= =?UTF-8?q?ew=20correctly.=20Thanks=20for=20the=20report=20and=20initial?= =?UTF-8?q?=20patch,=20Bruno=20Reni=C3=A9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@15552 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/conf/urls/static.py | 2 +- tests/regressiontests/views/tests/static.py | 34 ++++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/django/conf/urls/static.py b/django/conf/urls/static.py index f90b8b5cab..ff1942f990 100644 --- a/django/conf/urls/static.py +++ b/django/conf/urls/static.py @@ -22,5 +22,5 @@ def static(prefix, view='django.views.static.serve', **kwargs): elif '://' in prefix: raise ImproperlyConfigured("URL '%s' not allowed as static prefix" % prefix) return patterns('', - url(r'^%s(?P.*)$' % re.escape(prefix.lstrip('/')), view, **kwargs), + url(r'^%s(?P.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs), ) diff --git a/tests/regressiontests/views/tests/static.py b/tests/regressiontests/views/tests/static.py index cd29687967..8ae04c2176 100644 --- a/tests/regressiontests/views/tests/static.py +++ b/tests/regressiontests/views/tests/static.py @@ -2,14 +2,18 @@ import mimetypes from os import path from django.conf import settings +from django.conf.urls.static import static from django.test import TestCase from django.http import HttpResponseNotModified + +from regressiontests.views import urls from regressiontests.views.urls import media_dir class StaticTests(TestCase): """Tests django views in django/views/static.py""" def setUp(self): + self.prefix = 'site_media' self.old_debug = settings.DEBUG settings.DEBUG = True @@ -20,26 +24,25 @@ class StaticTests(TestCase): "The static view can serve static media" media_files = ['file.txt', 'file.txt.gz'] for filename in media_files: - response = self.client.get('/views/site_media/%s' % filename) + response = self.client.get('/views/%s/%s' % (self.prefix, filename)) file_path = path.join(media_dir, filename) self.assertEquals(open(file_path).read(), response.content) self.assertEquals(len(response.content), int(response['Content-Length'])) self.assertEquals(mimetypes.guess_type(file_path)[1], response.get('Content-Encoding', None)) def test_unknown_mime_type(self): - response = self.client.get('/views/site_media/file.unknown') + response = self.client.get('/views/%s/file.unknown' % self.prefix) self.assertEquals('application/octet-stream', response['Content-Type']) def test_copes_with_empty_path_component(self): file_name = 'file.txt' - response = self.client.get('/views/site_media//%s' % file_name) + response = self.client.get('/views/%s//%s' % (self.prefix, file_name)) file = open(path.join(media_dir, file_name)) self.assertEquals(file.read(), response.content) def test_is_modified_since(self): file_name = 'file.txt' - response = self.client.get( - '/views/site_media/%s' % file_name, + response = self.client.get('/views/%s/%s' % (self.prefix, file_name), HTTP_IF_MODIFIED_SINCE='Thu, 1 Jan 1970 00:00:00 GMT') file = open(path.join(media_dir, file_name)) self.assertEquals(file.read(), response.content) @@ -47,7 +50,7 @@ class StaticTests(TestCase): def test_not_modified_since(self): file_name = 'file.txt' response = self.client.get( - '/views/site_media/%s' % file_name, + '/views/%s/%s' % (self.prefix, file_name), HTTP_IF_MODIFIED_SINCE='Mon, 18 Jan 2038 05:14:07 UTC' # This is 24h before max Unix time. Remember to fix Django and # update this test well before 2038 :) @@ -62,7 +65,7 @@ class StaticTests(TestCase): """ file_name = 'file.txt' invalid_date = 'Mon, 28 May 999999999999 28:25:26 GMT' - response = self.client.get('/views/site_media/%s' % file_name, + response = self.client.get('/views/%s/%s' % (self.prefix, file_name), HTTP_IF_MODIFIED_SINCE=invalid_date) file = open(path.join(media_dir, file_name)) self.assertEquals(file.read(), response.content) @@ -77,9 +80,24 @@ class StaticTests(TestCase): """ file_name = 'file.txt' invalid_date = ': 1291108438, Wed, 20 Oct 2010 14:05:00 GMT' - response = self.client.get('/views/site_media/%s' % file_name, + response = self.client.get('/views/%s/%s' % (self.prefix, file_name), HTTP_IF_MODIFIED_SINCE=invalid_date) file = open(path.join(media_dir, file_name)) self.assertEquals(file.read(), response.content) self.assertEquals(len(response.content), int(response['Content-Length'])) + + +class StaticHelperTest(StaticTests): + """ + Test case to make sure the static URL pattern helper works as expected + """ + def setUp(self): + super(StaticHelperTest, self).setUp() + self.prefix = 'media' + self._old_views_urlpatterns = urls.urlpatterns + urls.urlpatterns += static('/media/', document_root=media_dir) + + def tearDown(self): + super(StaticHelperTest, self).tearDown() + urls.urlpatterns = self._old_views_urlpatterns