diff options
author | Simon Law <simon.law@ecometrica.com> | 2012-07-23 15:29:39 -0400 |
---|---|---|
committer | Simon Law <simon.law@ecometrica.com> | 2012-07-23 15:29:39 -0400 |
commit | 4a27fe9f16ecd4bc6f94ad89046767450316490c (patch) | |
tree | 659a75357afd37449685adbd70657b3a21530755 /wkhtmltopdf/tests.py | |
parent | 8c5b30925654309da9e79bf8bb65e010068bd903 (diff) | |
download | django-wkhtmltopdf-4a27fe9f16ecd4bc6f94ad89046767450316490c.tar.gz django-wkhtmltopdf-4a27fe9f16ecd4bc6f94ad89046767450316490c.tar.bz2 django-wkhtmltopdf-4a27fe9f16ecd4bc6f94ad89046767450316490c.zip |
PDFResponse is more robust:
* Now matches HttpResponse in function signature.
* Modern Django content_type/mimetype handling.
* Sanitizes and quotes filenames in Content-Disposition header.
* Tests.
Diffstat (limited to 'wkhtmltopdf/tests.py')
-rw-r--r-- | wkhtmltopdf/tests.py | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/wkhtmltopdf/tests.py b/wkhtmltopdf/tests.py index 5f90c33..6b49378 100644 --- a/wkhtmltopdf/tests.py +++ b/wkhtmltopdf/tests.py @@ -10,7 +10,7 @@ from django.test import TestCase from .subprocess import CalledProcessError from .utils import _options_to_args, template_to_temp_file, wkhtmltopdf -from .views import PdfResponse, PdfTemplateView +from .views import PDFResponse, PdfResponse, PdfTemplateView class TestUtils(TestCase): @@ -69,7 +69,47 @@ class TestUtils(TestCase): class TestViews(TestCase): + def test_pdf_response(self): + """Should generate the correct HttpResonse object and mimetype""" + # 404 + response = PDFResponse(content='', status=404) + self.assertEqual(response.status_code, 404) + self.assertEqual(response.content, '') + self.assertEqual(response['Content-Type'], 'application/pdf') + self.assertFalse(response.has_header('Content-Disposition')) + + content = '%PDF-1.4\n%%EOF' + # Without filename + response = PDFResponse(content=content) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, content) + self.assertEqual(response['Content-Type'], 'application/pdf') + self.assertFalse(response.has_header('Content-Disposition')) + + # With filename + response = PDFResponse(content=content, filename="nospace.pdf") + self.assertEqual(response['Content-Disposition'], + 'attachment; filename="nospace.pdf"') + response = PDFResponse(content=content, filename="one space.pdf") + self.assertEqual(response['Content-Disposition'], + 'attachment; filename="one space.pdf"') + response = PDFResponse(content=content, filename="4'5\".pdf") + self.assertEqual(response['Content-Disposition'], + 'attachment; filename="4\'5.pdf"') + response = PDFResponse(content=content, filename=u"♥.pdf") + self.assertEqual(response['Content-Disposition'], + 'attachment; filename="?.pdf"') + + # Content-Type + response = PDFResponse(content=content, + content_type='application/x-pdf') + self.assertEqual(response['Content-Type'], 'application/x-pdf') + response = PDFResponse(content=content, + mimetype='application/x-pdf') + self.assertEqual(response['Content-Type'], 'application/x-pdf') + def test_deprecated(self): + """Should warn when using deprecated views.""" with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') PdfTemplateView() |