Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed gzip automatic content-type assignment and added automatic compression header configuration #251

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/microdot/microdot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 compressed and 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:
Expand Down
1 change: 1 addition & 0 deletions tests/files/test.txt.gz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
11 changes: 11 additions & 0 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ def test_send_file_compressed(self):
'application/octet-stream')
self.assertEqual(res.headers['Content-Encoding'], 'gzip')

def test_send_file_gzip_handling(self):
res = Response.send_file('tests/files/test.txt.gz')
self.assertEqual(res.status_code, 200)
self.assertEqual(res.headers['Content-Type'],
'application/octet-stream')

res = Response.send_file('tests/files/test.txt.gz', compressed=True)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.headers['Content-Type'], 'text/plain')
self.assertEqual(res.headers['Content-Encoding'], 'gzip')

def test_default_content_type(self):
original_content_type = Response.default_content_type
res = Response('foo')
Expand Down
Loading