forked from openlp/openlp
Code was not sending the headers back
This commit is contained in:
parent
6c3253c7c0
commit
768b1c77fc
@ -349,15 +349,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()
|
||||
html = None
|
||||
if self.send_appropriate_header(file_name) == '.html':
|
||||
variables = self.template_vars
|
||||
html = Template(filename=path, input_encoding='utf-8',
|
||||
output_encoding='utf-8').render(**variables)
|
||||
content = None
|
||||
ext, content_type = self.get_content_type(file_name)
|
||||
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)
|
||||
@ -368,32 +367,29 @@ 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 send_appropriate_header(self, file_name):
|
||||
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]
|
||||
if ext == '.html':
|
||||
self.send_header('Content-type', 'text/html')
|
||||
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')
|
||||
return ext
|
||||
content_type = file_types.get(ext, 'text/plain')
|
||||
return ext, content_type
|
||||
|
||||
def serve_thumbnail(self, controller_name=None, dimensions=None, file_name=None):
|
||||
"""
|
||||
@ -405,6 +401,7 @@ class HttpRouter(object):
|
||||
if not dimensions:
|
||||
dimensions = ''
|
||||
content = ''
|
||||
content_type = None
|
||||
if controller_name and file_name:
|
||||
if controller_name in supported_controllers:
|
||||
full_path = urllib.parse.unquote(file_name)
|
||||
@ -417,12 +414,15 @@ class HttpRouter(object):
|
||||
image_manager = Registry().get('image_manager')
|
||||
image_manager.add_image(full_path, just_file_name, None,
|
||||
dimensions)
|
||||
ext = self.send_appropriate_header(full_path)
|
||||
ext, content_type = self.get_content_type(full_path)
|
||||
content = image_to_byte(
|
||||
image_manager.get_image(full_path,
|
||||
just_file_name, dimensions), False)
|
||||
if len(content)==0:
|
||||
content = self.do_not_found()
|
||||
return self.do_not_found()
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type',content_type)
|
||||
self.end_headers()
|
||||
return content
|
||||
|
||||
def poll(self):
|
||||
|
@ -115,21 +115,20 @@ class TestRouter(TestCase):
|
||||
assert function['secure'] == False, \
|
||||
'The mocked function should not require any security.'
|
||||
|
||||
def send_appropriate_header_test(self):
|
||||
def get_appropriate_content_type_test(self):
|
||||
"""
|
||||
Test the header sending logic
|
||||
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']]
|
||||
send_header = MagicMock()
|
||||
self.router.send_header = send_header
|
||||
['test', 'text/plain'], ['', 'text/plain'],
|
||||
['/test/test.html', 'text/html'],
|
||||
['c:\\test\\test.html', 'text/html']]
|
||||
for header in headers:
|
||||
self.router.send_appropriate_header(header[0])
|
||||
send_header.assert_called_with('Content-type', header[1])
|
||||
send_header.reset_mock()
|
||||
ext, content_type = self.router.get_content_type(header[0])
|
||||
self.assertEqual(content_type, header[1], 'Mismatch of content type')
|
||||
|
||||
def serve_thumbnail_without_params_test(self):
|
||||
"""
|
||||
@ -141,6 +140,10 @@ class TestRouter(TestCase):
|
||||
self.router.wfile = MagicMock()
|
||||
self.router.serve_thumbnail()
|
||||
self.router.send_response.assert_called_once_with(404)
|
||||
self.assertEqual(self.router.send_response.call_count, 1,
|
||||
'Send response called once')
|
||||
self.assertEqual(self.router.end_headers.call_count, 1,
|
||||
'end_headers called once')
|
||||
|
||||
def serve_thumbnail_with_invalid_params_test(self):
|
||||
"""
|
||||
@ -207,6 +210,10 @@ class TestRouter(TestCase):
|
||||
# THEN: a file should be returned
|
||||
self.assertEqual(self.router.send_header.call_count, 1,
|
||||
'One header')
|
||||
self.assertEqual(self.router.send_response.call_count, 1,
|
||||
'Send response called once')
|
||||
self.assertEqual(self.router.end_headers.call_count, 1,
|
||||
'end_headers called once')
|
||||
mocked_exists.assert_called_with(urllib.parse.unquote(full_path))
|
||||
self.assertEqual(mocked_image_to_byte.call_count, 1, 'Called once')
|
||||
mocked_image_manager.assert_called_any(
|
||||
|
Loading…
Reference in New Issue
Block a user