diff --git a/src/microdot/microdot.py b/src/microdot/microdot.py index 49128ad..3ca28e1 100644 --- a/src/microdot/microdot.py +++ b/src/microdot/microdot.py @@ -774,7 +774,10 @@ def send_file(cls, filename, status_code=200, content_type=None, first. """ if content_type is None: - ext = filename.split('.')[-1] + if filename.endswith('.gz'): + ext = filename[:-3].split('.')[-1] + else: + ext = filename.split('.')[-1] if ext in Response.types_map: content_type = Response.types_map[ext] else: diff --git a/tests/files/test.txt.gz b/tests/files/test.txt.gz new file mode 100644 index 0000000..1910281 --- /dev/null +++ b/tests/files/test.txt.gz @@ -0,0 +1 @@ +foo \ No newline at end of file diff --git a/tests/test_response.py b/tests/test_response.py index 337f8dc..6f129dd 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -280,6 +280,15 @@ def test_send_file_compressed(self): 'application/octet-stream') self.assertEqual(res.headers['Content-Encoding'], 'gzip') + def test_send_file_with_correct_mime_type(self): + res = Response.send_file('tests/files/test.txt') + self.assertEqual(res.status_code, 200) + self.assertEqual(res.headers['Content-Type'], 'text/plain') + + res = Response.send_file('tests/files/test.txt.gz') + self.assertEqual(res.status_code, 200) + self.assertEqual(res.headers['Content-Type'], 'text/plain') + def test_default_content_type(self): original_content_type = Response.default_content_type res = Response('foo')