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
15from synapse.rest.media.v1._base import get_filename_from_headers
16
17from tests import unittest
18
19
20class GetFileNameFromHeadersTests(unittest.TestCase):
21    # input -> expected result
22    TEST_CASES = {
23        b"inline; filename=abc.txt": "abc.txt",
24        b'inline; filename="azerty"': "azerty",
25        b'inline; filename="aze%20rty"': "aze%20rty",
26        b'inline; filename="aze"rty"': 'aze"rty',
27        b'inline; filename="azer;ty"': "azer;ty",
28        b"inline; filename*=utf-8''foo%C2%A3bar": "foo£bar",
29    }
30
31    def tests(self):
32        for hdr, expected in self.TEST_CASES.items():
33            res = get_filename_from_headers({b"Content-Disposition": [hdr]})
34            self.assertEqual(
35                res,
36                expected,
37                "expected output for %s to be %s but was %s" % (hdr, expected, res),
38            )
39