2013-11-18 11:12:02 +00:00
|
|
|
"""Unit tests around responses."""
|
2024-08-05 08:51:17 +00:00
|
|
|
|
2013-11-18 11:12:02 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
from django_downloadview.response import DownloadResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DownloadResponseTestCase(unittest.TestCase):
|
|
|
|
|
"""Tests around :class:`django_downloadviews.response.DownloadResponse`."""
|
2020-01-07 14:10:42 +00:00
|
|
|
|
2013-11-18 11:12:02 +00:00
|
|
|
def test_content_disposition_encoding(self):
|
|
|
|
|
"""Content-Disposition header is encoded."""
|
2020-01-07 14:10:42 +00:00
|
|
|
response = DownloadResponse(
|
2020-09-18 08:50:26 +00:00
|
|
|
"fake file",
|
|
|
|
|
attachment=True,
|
|
|
|
|
basename="espacé .txt",
|
2020-01-07 14:10:42 +00:00
|
|
|
)
|
2013-11-18 11:12:02 +00:00
|
|
|
headers = response.default_headers
|
2020-01-07 14:10:42 +00:00
|
|
|
self.assertIn('filename="espace_.txt"', headers["Content-Disposition"])
|
|
|
|
|
self.assertIn(
|
|
|
|
|
"filename*=UTF-8''espac%C3%A9%20.txt", headers["Content-Disposition"]
|
|
|
|
|
)
|
2024-07-30 11:51:25 +00:00
|
|
|
|
|
|
|
|
def test_content_disposition_escaping(self):
|
|
|
|
|
"""Content-Disposition headers escape special characters."""
|
|
|
|
|
response = DownloadResponse(
|
2024-08-05 08:51:17 +00:00
|
|
|
"fake file", attachment=True, basename=r'"malicious\file.exe'
|
2024-07-30 11:51:25 +00:00
|
|
|
)
|
|
|
|
|
headers = response.default_headers
|
|
|
|
|
self.assertIn(
|
2024-08-05 08:51:17 +00:00
|
|
|
r'filename="\"malicious\\file.exe"', headers["Content-Disposition"]
|
|
|
|
|
)
|