forked from openlp/openlp
Fixed router so response and headers are sent correctly
Fixed tests so they run on Windows Added unit test for new method get_content_type
This commit is contained in:
parent
f0af04a9f2
commit
20a1a2ad70
@ -346,30 +346,14 @@ class HttpRouter(object):
|
||||
path = os.path.normpath(os.path.join(self.html_dir, file_name))
|
||||
if not path.startswith(self.html_dir):
|
||||
return self.do_not_found()
|
||||
ext = os.path.splitext(file_name)[1]
|
||||
html = None
|
||||
if ext == '.html':
|
||||
self.send_header('Content-type', 'text/html')
|
||||
variables = self.template_vars
|
||||
html = Template(filename=path, input_encoding='utf-8', output_encoding='utf-8').render(**variables)
|
||||
elif ext == '.css':
|
||||
self.send_header('Content-type', 'text/css')
|
||||
elif ext == '.js':
|
||||
self.send_header('Content-type', 'application/javascript')
|
||||
elif ext == '.jpg':
|
||||
self.send_header('Content-type', 'image/jpeg')
|
||||
elif ext == '.gif':
|
||||
self.send_header('Content-type', 'image/gif')
|
||||
elif ext == '.ico':
|
||||
self.send_header('Content-type', 'image/x-icon')
|
||||
elif ext == '.png':
|
||||
self.send_header('Content-type', 'image/png')
|
||||
else:
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
content = None
|
||||
ext, content_type = self.get_content_type(path)
|
||||
file_handle = None
|
||||
try:
|
||||
if html:
|
||||
content = html
|
||||
if ext == '.html':
|
||||
variables = self.template_vars
|
||||
content = Template(filename=path, input_encoding='utf-8',
|
||||
output_encoding='utf-8').render(**variables)
|
||||
else:
|
||||
file_handle = open(path, 'rb')
|
||||
log.debug('Opened %s' % path)
|
||||
@ -380,8 +364,30 @@ class HttpRouter(object):
|
||||
finally:
|
||||
if file_handle:
|
||||
file_handle.close()
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', content_type)
|
||||
self.end_headers()
|
||||
return content
|
||||
|
||||
def get_content_type(self, file_name):
|
||||
"""
|
||||
Examines the extension of the file and determines
|
||||
what header to send back
|
||||
Returns the extension found
|
||||
"""
|
||||
content_type = 'text/plain'
|
||||
file_types = {'.html': 'text/html',
|
||||
'.css': 'text/css',
|
||||
'.js': 'application/javascript',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.gif': 'image/gif',
|
||||
'.ico': 'image/x-icon',
|
||||
'.png': 'image/png'
|
||||
}
|
||||
ext = os.path.splitext(file_name)[1]
|
||||
content_type = file_types.get(ext, 'text/plain')
|
||||
return ext, content_type
|
||||
|
||||
def poll(self):
|
||||
"""
|
||||
Poll OpenLP to determine the current slide number and item name.
|
||||
|
@ -62,7 +62,7 @@ class TestRemoteTab(TestCase):
|
||||
"""
|
||||
Create the UI
|
||||
"""
|
||||
fd, self.ini_file = mkstemp('.ini')
|
||||
self.fd, self.ini_file = mkstemp('.ini')
|
||||
Settings().set_filename(self.ini_file)
|
||||
self.application = QtGui.QApplication.instance()
|
||||
Settings().extend_default_settings(__default_settings__)
|
||||
@ -76,6 +76,7 @@ class TestRemoteTab(TestCase):
|
||||
del self.application
|
||||
del self.parent
|
||||
del self.form
|
||||
os.close(self.fd)
|
||||
os.unlink(self.ini_file)
|
||||
|
||||
def get_ip_address_default_test(self):
|
||||
|
@ -59,7 +59,7 @@ class TestRouter(TestCase):
|
||||
"""
|
||||
Create the UI
|
||||
"""
|
||||
fd, self.ini_file = mkstemp('.ini')
|
||||
self.fd, self.ini_file = mkstemp('.ini')
|
||||
Settings().set_filename(self.ini_file)
|
||||
self.application = QtGui.QApplication.instance()
|
||||
Settings().extend_default_settings(__default_settings__)
|
||||
@ -70,6 +70,7 @@ class TestRouter(TestCase):
|
||||
Delete all the C++ objects at the end so that we don't have a segfault
|
||||
"""
|
||||
del self.application
|
||||
os.close(self.fd)
|
||||
os.unlink(self.ini_file)
|
||||
|
||||
def password_encrypter_test(self):
|
||||
@ -110,3 +111,18 @@ class TestRouter(TestCase):
|
||||
'The mocked function should match defined value.'
|
||||
assert function['secure'] == False, \
|
||||
'The mocked function should not require any security.'
|
||||
|
||||
def get_content_type_test(self):
|
||||
"""
|
||||
Test the get_content_type logic
|
||||
"""
|
||||
headers = [ ['test.html', 'text/html'], ['test.css', 'text/css'],
|
||||
['test.js', 'application/javascript'], ['test.jpg', 'image/jpeg'],
|
||||
['test.gif', 'image/gif'], ['test.ico', 'image/x-icon'],
|
||||
['test.png', 'image/png'], ['test.whatever', 'text/plain'],
|
||||
['test', 'text/plain'], ['', 'text/plain'],
|
||||
['/test/test.html', 'text/html'],
|
||||
['c:\\test\\test.html', 'text/html']]
|
||||
for header in headers:
|
||||
ext, content_type = self.router.get_content_type(header[0])
|
||||
self.assertEqual(content_type, header[1], 'Mismatch of content type')
|
||||
|
Loading…
Reference in New Issue
Block a user