Skip to content

Comments

swev-id: django__django-16642 Fix FileResponse content encoding#517

Open
casey-brooks wants to merge 1 commit intodjango__django-16642from
fix-fileresponse-encoding-br-Z
Open

swev-id: django__django-16642 Fix FileResponse content encoding#517
casey-brooks wants to merge 1 commit intodjango__django-16642from
fix-fileresponse-encoding-br-Z

Conversation

@casey-brooks
Copy link

Summary

  • set FileResponse Content-Type from the underlying resource while inferring Content-Encoding for known compression suffixes (.Z, .gz, .bz2, .xz, .br)
  • keep explicit content_type untouched and expand regression coverage for the new encoding rules
  • align the response headers with HTTP semantics by separating representation metadata from transfer codings

Reproduction

from django.http import FileResponse

FileResponse(open("test.html.br", "rb"))
FileResponse(open("test.html.Z", "rb"))
PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages \
DJANGO_SETTINGS_MODULE=tests.test_sqlite \
python3.11 tests/runtests.py responses --parallel=1

Observed failure

Testing against Django installed in '/workspace/django/django'
Found 56 test(s).
System check identified no issues (0 silenced).
.................FFF.....EE...............................
======================================================================
ERROR: test_content_encoding_Z_suffix (responses.test_fileresponse.FileResponseTests.test_content_encoding_Z_suffix)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/workspace/django/tests/responses/test_fileresponse.py", line 269, in test_content_encoding_Z_suffix
    self.assertEqual(response.headers["Content-Encoding"], "compress")
                     ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/workspace/django/django/utils/datastructures.py", line 308, in __getitem__
    return self._store[key.lower()][1]
           ~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'content-encoding'

======================================================================
ERROR: test_content_encoding_br_suffix (responses.test_fileresponse.FileResponseTests.test_content_encoding_br_suffix)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/workspace/django/tests/responses/test_fileresponse.py", line 275, in test_content_encoding_br_suffix
    self.assertEqual(response.headers["Content-Encoding"], "br")
                     ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/workspace/django/django/utils/datastructures.py", line 308, in __getitem__
    return self._store[key.lower()][1]
           ~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'content-encoding'

======================================================================
FAIL: test_compressed_response (responses.test_fileresponse.FileResponseTests.test_compressed_response) (ext='.tar.gz')
FileResponse infers the underlying Content-Type and sets
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/workspace/django/tests/responses/test_fileresponse.py", line 262, in test_compressed_response
    self.assertEqual(response.headers["Content-Type"], mimetype)
AssertionError: 'application/gzip' != 'application/x-tar'
- application/gzip
+ application/x-tar


======================================================================
FAIL: test_compressed_response (responses.test_fileresponse.FileResponseTests.test_compressed_response) (ext='.tar.bz2')
FileResponse infers the underlying Content-Type and sets
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/workspace/django/tests/responses/test_fileresponse.py", line 262, in test_compressed_response
    self.assertEqual(response.headers["Content-Type"], mimetype)
AssertionError: 'application/x-bzip' != 'application/x-tar'
- application/x-bzip
?               ^^^^
+ application/x-tar
?               ^^^


======================================================================
FAIL: test_compressed_response (responses.test_fileresponse.FileResponseTests.test_compressed_response) (ext='.tar.xz')
FileResponse infers the underlying Content-Type and sets
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/workspace/django/tests/responses/test_fileresponse.py", line 262, in test_compressed_response
    self.assertEqual(response.headers["Content-Type"], mimetype)
AssertionError: 'application/x-xz' != 'application/x-tar'
- application/x-xz
?               ^^
+ application/x-tar
?               ^^^


----------------------------------------------------------------------
Ran 56 tests in 0.011s

FAILED (failures=3, errors=2)

Testing

  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3.11 tests/runtests.py responses --parallel=1

Fixes #512

@casey-brooks casey-brooks requested a review from a team December 24, 2025 17:14
@casey-brooks
Copy link
Author

Test & Lint Summary

  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3.11 tests/runtests.py responses --parallel=1 → 56 passed, 0 failed, 0 skipped
  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages python3.11 -m flake8 django/http/response.py tests/responses/test_fileresponse.py → no lint errors

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specs met: FileResponse now preserves underlying Content-Type and emits Content-Encoding for known compressed suffixes, and regression coverage exercises the new paths.

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rowan-stein rowan-stein changed the base branch from django__django-16642 to main January 11, 2026 20:13
@rowan-stein rowan-stein changed the base branch from main to django__django-16642 January 12, 2026 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix FileResponse Content-Type and Content-Encoding for .Z and .br (swev-id: django__django-16642)

2 participants