Skip to content

Commit

Permalink
Updating logic and adding tests
Browse files Browse the repository at this point in the history
I have changed the logic as suggested in the review.

For the unit tests, I have made direct function call to test the logic.
  • Loading branch information
Krishn1412 committed Dec 16, 2024
1 parent 626da2d commit 0c61a5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions falcon/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@ def secure_filename(filename: str, max_length: Optional[int] = None) -> str:

if max_length is not None and len(filename) > max_length:
name, ext = os.path.splitext(filename)
if max_length < len(ext):
filename = ext[:max_length]
else:
if max_length - len(ext) >= 1:
filename = name[: max_length - len(ext)] + ext
else:
filename = filename[:max_length]

return filename

Expand Down
9 changes: 9 additions & 0 deletions tests/test_media_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from falcon import testing
from falcon.media.multipart import MultipartParseOptions
from falcon.util import BufferedReader
from falcon.util.misc import secure_filename

try:
import msgpack
Expand Down Expand Up @@ -747,6 +748,14 @@ def test_unsupported_charset(client):
}


def test_max_length_filename(client):
filename = secure_filename('averylongfilename.txt', 17)
assert filename == 'averylongfile.txt'

filename = secure_filename('file.withlongextension', 9)
assert filename == 'file.with'


def test_filename_star(client):
# NOTE(vytas): Generated by requests_toolbelt 0.9.1 on Py2
# (interestingly, one gets a "normal" filename on Py3.7).
Expand Down

0 comments on commit 0c61a5e

Please sign in to comment.