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.rest.media.v1._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"inline; filename=abc.txt": "abc.txt",
  20. b'inline; filename="azerty"': "azerty",
  21. b'inline; filename="aze%20rty"': "aze%20rty",
  22. b'inline; filename="aze"rty"': 'aze"rty',
  23. b'inline; filename="azer;ty"': "azer;ty",
  24. b"inline; filename*=utf-8''foo%C2%A3bar": "foo£bar",
  25. }
  26. def tests(self):
  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. "expected output for %s to be %s but was %s" % (hdr, expected, res),
  33. )