test_base.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright 2019 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.media._base import get_filename_from_headers
  15. from tests import unittest
  16. class GetFileNameFromHeadersTests(unittest.TestCase):
  17. # input -> expected result
  18. TEST_CASES = {
  19. b"attachment; filename=abc.txt": "abc.txt",
  20. b'attachment; filename="azerty"': "azerty",
  21. b'attachment; filename="aze%20rty"': "aze%20rty",
  22. b'attachment; filename="aze"rty"': 'aze"rty',
  23. b'attachment; filename="azer;ty"': "azer;ty",
  24. b"attachment; filename*=utf-8''foo%C2%A3bar": "foo£bar",
  25. }
  26. def tests(self) -> None:
  27. for hdr, expected in self.TEST_CASES.items():
  28. res = get_filename_from_headers({b"Content-Disposition": [hdr]})
  29. self.assertEqual(
  30. res,
  31. expected,
  32. f"expected output for {hdr!r} to be {expected} but was {res}",
  33. )