Skip to content

Commit

Permalink
Fix tests and better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Aug 30, 2024
1 parent 2bb3c1f commit e778e1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/servestatic/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ async def __call__(self, request):
for finder in current_finders
for storage in finder.storages.values()
]
app_dirs = "\n ".join(sorted(app_dirs))
app_dirs = "\n ".join(sorted(app_dirs))
raise MissingFileError(
f"{request.path} not found. Searched these paths:{app_dirs}"
f"ServeStatic did not find the file '{request.path.lstrip(settings.STATIC_URL)}' within the following paths:\n{app_dirs}"
)

return await self.get_response(request)
Expand Down
19 changes: 12 additions & 7 deletions tests/test_django_whitenoise.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import html
import shutil
import tempfile
from contextlib import closing
Expand Down Expand Up @@ -248,12 +249,16 @@ def test_404_in_prod(server):

@override_settings(DEBUG=True)
def test_error_message(server):
response = server.get(settings.STATIC_URL + "garbage")
print(response.content.decode())
app_dirs = Path(__file__).parent / "test_files" / "static"

expected = f"""{settings.STATIC_URL + 'garbage'} not found. Searched these paths:
response = server.get(f"{settings.STATIC_URL}garbage")
response_content = str(response.content.decode())
response_content = html.unescape(response_content)

{app_dirs}"""
# Beautify for easier debugging
response_content = response_content[response_content.index("ServeStatic") :]

assert expected in str(response.content.decode())
assert (
"ServeStatic did not find the file 'garbage' within the following paths:"
in response_content
)
assert "•" in response_content
assert str(Path(__file__).parent / "test_files" / "static") in response_content

0 comments on commit e778e1c

Please sign in to comment.