2009-07-29 04:40:14 +02:00
|
|
|
"""
|
|
|
|
Tests for django.core.servers.
|
|
|
|
"""
|
|
|
|
import os
|
2011-06-30 11:06:19 +02:00
|
|
|
from urlparse import urljoin
|
2009-07-29 04:40:14 +02:00
|
|
|
|
|
|
|
import django
|
2011-06-30 11:06:19 +02:00
|
|
|
from django.conf import settings
|
2009-07-29 04:40:14 +02:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.core.handlers.wsgi import WSGIHandler
|
|
|
|
from django.core.servers.basehttp import AdminMediaHandler
|
|
|
|
|
|
|
|
|
|
|
|
class AdminMediaHandlerTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2011-06-30 11:06:19 +02:00
|
|
|
self.admin_media_url = urljoin(settings.STATIC_URL, 'admin/')
|
2010-09-26 22:55:58 +02:00
|
|
|
self.admin_media_file_path = os.path.abspath(
|
2011-06-30 11:06:19 +02:00
|
|
|
os.path.join(django.__path__[0], 'contrib', 'admin', 'static', 'admin')
|
2010-09-26 22:55:58 +02:00
|
|
|
)
|
2009-07-29 04:40:14 +02:00
|
|
|
self.handler = AdminMediaHandler(WSGIHandler())
|
|
|
|
|
|
|
|
def test_media_urls(self):
|
|
|
|
"""
|
|
|
|
Tests that URLs that look like absolute file paths after the
|
2011-06-30 11:06:19 +02:00
|
|
|
settings.STATIC_URL don't turn into absolute file paths.
|
2009-07-29 04:40:14 +02:00
|
|
|
"""
|
|
|
|
# Cases that should work on all platforms.
|
|
|
|
data = (
|
2011-06-30 11:06:19 +02:00
|
|
|
('%scss/base.css' % self.admin_media_url, ('css', 'base.css')),
|
2009-07-29 04:40:14 +02:00
|
|
|
)
|
|
|
|
# Cases that should raise an exception.
|
|
|
|
bad_data = ()
|
|
|
|
|
|
|
|
# Add platform-specific cases.
|
|
|
|
if os.sep == '/':
|
|
|
|
data += (
|
|
|
|
# URL, tuple of relative path parts.
|
2011-06-30 11:06:19 +02:00
|
|
|
('%s\\css/base.css' % self.admin_media_url, ('\\css', 'base.css')),
|
2009-07-29 04:40:14 +02:00
|
|
|
)
|
|
|
|
bad_data += (
|
2011-06-30 11:06:19 +02:00
|
|
|
'%s/css/base.css' % self.admin_media_url,
|
|
|
|
'%s///css/base.css' % self.admin_media_url,
|
|
|
|
'%s../css/base.css' % self.admin_media_url,
|
2009-07-29 04:40:14 +02:00
|
|
|
)
|
|
|
|
elif os.sep == '\\':
|
|
|
|
bad_data += (
|
2011-06-30 11:06:19 +02:00
|
|
|
'%sC:\css/base.css' % self.admin_media_url,
|
|
|
|
'%s/\\css/base.css' % self.admin_media_url,
|
|
|
|
'%s\\css/base.css' % self.admin_media_url,
|
|
|
|
'%s\\\\css/base.css' % self.admin_media_url
|
2009-07-29 04:40:14 +02:00
|
|
|
)
|
|
|
|
for url, path_tuple in data:
|
|
|
|
try:
|
|
|
|
output = self.handler.file_path(url)
|
|
|
|
except ValueError:
|
|
|
|
self.fail("Got a ValueError exception, but wasn't expecting"
|
|
|
|
" one. URL was: %s" % url)
|
|
|
|
rel_path = os.path.join(*path_tuple)
|
|
|
|
desired = os.path.normcase(
|
|
|
|
os.path.join(self.admin_media_file_path, rel_path))
|
|
|
|
self.assertEqual(output, desired,
|
|
|
|
"Got: %s, Expected: %s, URL was: %s" % (output, desired, url))
|
|
|
|
for url in bad_data:
|
|
|
|
try:
|
|
|
|
output = self.handler.file_path(url)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
self.fail('URL: %s should have caused a ValueError exception.'
|
|
|
|
% url)
|