| 1 |
"""Unit tests around responses.""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
from django_downloadview.response import DownloadResponse |
| 6 |
|
| 7 |
|
| 8 |
class DownloadResponseTestCase(unittest.TestCase): |
| 9 |
"""Tests around :class:`django_downloadviews.response.DownloadResponse`.""" |
| 10 |
|
| 11 |
def test_content_disposition_encoding(self): |
| 12 |
"""Content-Disposition header is encoded.""" |
| 13 |
response = DownloadResponse( |
| 14 |
"fake file", |
| 15 |
attachment=True, |
| 16 |
basename="espacé .txt", |
| 17 |
) |
| 18 |
headers = response.default_headers |
| 19 |
self.assertIn('filename="espace_.txt"', headers["Content-Disposition"]) |
| 20 |
self.assertIn( |
| 21 |
"filename*=UTF-8''espac%C3%A9%20.txt", headers["Content-Disposition"] |
| 22 |
) |
| 23 |
|
| 24 |
def test_content_disposition_escaping(self): |
| 25 |
"""Content-Disposition headers escape special characters.""" |
| 26 |
response = DownloadResponse( |
| 27 |
"fake file", attachment=True, basename=r'"malicious\file.exe' |
| 28 |
) |
| 29 |
headers = response.default_headers |
| 30 |
self.assertIn( |
| 31 |
r'filename="\"malicious\\file.exe"', headers["Content-Disposition"] |
| 32 |
) |