Code was not sending the headers back

This commit is contained in:
Felipe Polo-Wood 2013-11-08 12:43:36 -05:00
parent 6c3253c7c0
commit 768b1c77fc
2 changed files with 42 additions and 35 deletions

View File

@ -349,15 +349,14 @@ class HttpRouter(object):
path = os.path.normpath(os.path.join(self.html_dir, file_name)) path = os.path.normpath(os.path.join(self.html_dir, file_name))
if not path.startswith(self.html_dir): if not path.startswith(self.html_dir):
return self.do_not_found() return self.do_not_found()
html = None content = None
if self.send_appropriate_header(file_name) == '.html': ext, content_type = self.get_content_type(file_name)
variables = self.template_vars
html = Template(filename=path, input_encoding='utf-8',
output_encoding='utf-8').render(**variables)
file_handle = None file_handle = None
try: try:
if html: if ext == '.html':
content = html variables = self.template_vars
content = Template(filename=path, input_encoding='utf-8',
output_encoding='utf-8').render(**variables)
else: else:
file_handle = open(path, 'rb') file_handle = open(path, 'rb')
log.debug('Opened %s' % path) log.debug('Opened %s' % path)
@ -368,32 +367,29 @@ class HttpRouter(object):
finally: finally:
if file_handle: if file_handle:
file_handle.close() file_handle.close()
self.send_response(200)
self.send_header('Content-type', content_type)
self.end_headers()
return content return content
def send_appropriate_header(self, file_name): def get_content_type(self, file_name):
""" """
Examines the extension of the file and determines Examines the extension of the file and determines
what header to send back what header to send back
Returns the extension found 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] ext = os.path.splitext(file_name)[1]
if ext == '.html': content_type = file_types.get(ext, 'text/plain')
self.send_header('Content-type', 'text/html') return ext, content_type
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
def serve_thumbnail(self, controller_name=None, dimensions=None, file_name=None): def serve_thumbnail(self, controller_name=None, dimensions=None, file_name=None):
""" """
@ -405,6 +401,7 @@ class HttpRouter(object):
if not dimensions: if not dimensions:
dimensions = '' dimensions = ''
content = '' content = ''
content_type = None
if controller_name and file_name: if controller_name and file_name:
if controller_name in supported_controllers: if controller_name in supported_controllers:
full_path = urllib.parse.unquote(file_name) full_path = urllib.parse.unquote(file_name)
@ -417,12 +414,15 @@ class HttpRouter(object):
image_manager = Registry().get('image_manager') image_manager = Registry().get('image_manager')
image_manager.add_image(full_path, just_file_name, None, image_manager.add_image(full_path, just_file_name, None,
dimensions) dimensions)
ext = self.send_appropriate_header(full_path) ext, content_type = self.get_content_type(full_path)
content = image_to_byte( content = image_to_byte(
image_manager.get_image(full_path, image_manager.get_image(full_path,
just_file_name, dimensions), False) just_file_name, dimensions), False)
if len(content)==0: 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 return content
def poll(self): def poll(self):

View File

@ -115,21 +115,20 @@ class TestRouter(TestCase):
assert function['secure'] == False, \ assert function['secure'] == False, \
'The mocked function should not require any security.' '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'], headers = [ ['test.html', 'text/html'], ['test.css', 'text/css'],
['test.js', 'application/javascript'], ['test.jpg', 'image/jpeg'], ['test.js', 'application/javascript'], ['test.jpg', 'image/jpeg'],
['test.gif', 'image/gif'], ['test.ico', 'image/x-icon'], ['test.gif', 'image/gif'], ['test.ico', 'image/x-icon'],
['test.png', 'image/png'], ['test.whatever', 'text/plain'], ['test.png', 'image/png'], ['test.whatever', 'text/plain'],
['test', 'text/plain'], ['', 'text/plain']] ['test', 'text/plain'], ['', 'text/plain'],
send_header = MagicMock() ['/test/test.html', 'text/html'],
self.router.send_header = send_header ['c:\\test\\test.html', 'text/html']]
for header in headers: for header in headers:
self.router.send_appropriate_header(header[0]) ext, content_type = self.router.get_content_type(header[0])
send_header.assert_called_with('Content-type', header[1]) self.assertEqual(content_type, header[1], 'Mismatch of content type')
send_header.reset_mock()
def serve_thumbnail_without_params_test(self): def serve_thumbnail_without_params_test(self):
""" """
@ -141,6 +140,10 @@ class TestRouter(TestCase):
self.router.wfile = MagicMock() self.router.wfile = MagicMock()
self.router.serve_thumbnail() self.router.serve_thumbnail()
self.router.send_response.assert_called_once_with(404) 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): def serve_thumbnail_with_invalid_params_test(self):
""" """
@ -207,6 +210,10 @@ class TestRouter(TestCase):
# THEN: a file should be returned # THEN: a file should be returned
self.assertEqual(self.router.send_header.call_count, 1, self.assertEqual(self.router.send_header.call_count, 1,
'One header') '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)) mocked_exists.assert_called_with(urllib.parse.unquote(full_path))
self.assertEqual(mocked_image_to_byte.call_count, 1, 'Called once') self.assertEqual(mocked_image_to_byte.call_count, 1, 'Called once')
mocked_image_manager.assert_called_any( mocked_image_manager.assert_called_any(