From b4fc01bd3eb4be2fbd6b0f5ca3c6ddbae6111b21 Mon Sep 17 00:00:00 2001 From: Lukas Kremla <155779787+lukaskremla@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:34:11 +0200 Subject: [PATCH 1/3] Add a condition for properly setting gzip file content-type headers --- src/microdot/microdot.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/microdot/microdot.py b/src/microdot/microdot.py index 49128ad..62a404f 100644 --- a/src/microdot/microdot.py +++ b/src/microdot/microdot.py @@ -774,7 +774,11 @@ 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: From 9bba7ee509b099b094f3b612db42b7b22435410d Mon Sep 17 00:00:00 2001 From: Lukas Kremla <155779787+lukaskremla@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:41:29 +0200 Subject: [PATCH 2/3] Changed the quotes from "" to '' for consistency --- src/microdot/microdot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/microdot/microdot.py b/src/microdot/microdot.py index 62a404f..12f8caa 100644 --- a/src/microdot/microdot.py +++ b/src/microdot/microdot.py @@ -774,7 +774,7 @@ def send_file(cls, filename, status_code=200, content_type=None, first. """ if content_type is None: - if filename.endswith(".gz"): + if filename.endswith('.gz'): ext = filename[:-3].split('.')[-1] else: ext = filename.split('.')[-1] From 5cfad2311ea22df0d259fff0acc0550f964f8b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kremla?= Date: Tue, 13 Aug 2024 19:41:50 +0200 Subject: [PATCH 3/3] Fixed linting issue and added a test for the gzipped file content-type handling --- src/microdot/microdot.py | 1 - tests/files/test.txt.gz | 1 + tests/test_response.py | 9 +++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/files/test.txt.gz diff --git a/src/microdot/microdot.py b/src/microdot/microdot.py index 12f8caa..3ca28e1 100644 --- a/src/microdot/microdot.py +++ b/src/microdot/microdot.py @@ -778,7 +778,6 @@ def send_file(cls, filename, status_code=200, content_type=None, 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')