parts/django/tests/regressiontests/servers/tests.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 """
       
     2 Tests for django.core.servers.
       
     3 """
       
     4 
       
     5 import os
       
     6 
       
     7 import django
       
     8 from django.test import TestCase
       
     9 from django.core.handlers.wsgi import WSGIHandler
       
    10 from django.core.servers.basehttp import AdminMediaHandler
       
    11 
       
    12 
       
    13 class AdminMediaHandlerTests(TestCase):
       
    14 
       
    15     def setUp(self):
       
    16         self.admin_media_file_path = os.path.abspath(
       
    17             os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
       
    18         )
       
    19         self.handler = AdminMediaHandler(WSGIHandler())
       
    20 
       
    21     def test_media_urls(self):
       
    22         """
       
    23         Tests that URLs that look like absolute file paths after the
       
    24         settings.ADMIN_MEDIA_PREFIX don't turn into absolute file paths.
       
    25         """
       
    26         # Cases that should work on all platforms.
       
    27         data = (
       
    28             ('/media/css/base.css', ('css', 'base.css')),
       
    29         )
       
    30         # Cases that should raise an exception.
       
    31         bad_data = ()
       
    32 
       
    33         # Add platform-specific cases.
       
    34         if os.sep == '/':
       
    35             data += (
       
    36                 # URL, tuple of relative path parts.
       
    37                 ('/media/\\css/base.css', ('\\css', 'base.css')),
       
    38             )
       
    39             bad_data += (
       
    40                 '/media//css/base.css',
       
    41                 '/media////css/base.css',
       
    42                 '/media/../css/base.css',
       
    43             )
       
    44         elif os.sep == '\\':
       
    45             bad_data += (
       
    46                 '/media/C:\css/base.css',
       
    47                 '/media//\\css/base.css',
       
    48                 '/media/\\css/base.css',
       
    49                 '/media/\\\\css/base.css'
       
    50             )
       
    51         for url, path_tuple in data:
       
    52             try:
       
    53                 output = self.handler.file_path(url)
       
    54             except ValueError:
       
    55                 self.fail("Got a ValueError exception, but wasn't expecting"
       
    56                           " one. URL was: %s" % url)
       
    57             rel_path = os.path.join(*path_tuple)
       
    58             desired = os.path.normcase(
       
    59                 os.path.join(self.admin_media_file_path, rel_path))
       
    60             self.assertEqual(output, desired,
       
    61                 "Got: %s, Expected: %s, URL was: %s" % (output, desired, url))
       
    62         for url in bad_data:
       
    63             try:
       
    64                 output = self.handler.file_path(url)
       
    65             except ValueError:
       
    66                 continue
       
    67             self.fail('URL: %s should have caused a ValueError exception.'
       
    68                       % url)