diff --git a/frappe/tests/test_api.py b/frappe/tests/test_api.py index 17c71f8c75..5098158e0f 100644 --- a/frappe/tests/test_api.py +++ b/frappe/tests/test_api.py @@ -1,3 +1,4 @@ +import json import sys from contextlib import contextmanager from random import choice @@ -6,13 +7,14 @@ from time import time from unittest.mock import patch import requests +from filetype import guess_mime from semantic_version import Version from werkzeug.test import TestResponse import frappe from frappe.installer import update_site_config from frappe.tests.utils import FrappeTestCase, patch_hooks -from frappe.utils import get_site_url, get_test_client +from frappe.utils import cint, get_site_url, get_test_client try: _site = frappe.local.site @@ -325,3 +327,44 @@ def before_request(*args, **kwargs): def after_request(*args, **kwargs): _test_REQ_HOOK["after_request"] = time() + + +class TestResponse(FrappeAPITestCase): + def test_generate_pdf(self): + response = self.get( + f"/api/method/frappe.utils.print_format.download_pdf", + {"sid": self.sid, "doctype": "User", "name": "Guest"}, + ) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.headers["content-type"], "application/pdf") + self.assertGreater(cint(response.headers["content-length"]), 0) + + self.assertEqual(guess_mime(response.data), "application/pdf") + + def test_binary_and_csv_response(self): + def download_template(file_type): + filters = json.dumps({}) + fields = json.dumps({"User": ["name"]}) + return self.post( + "/api/method/frappe.core.doctype.data_import.data_import.download_template", + { + "sid": self.sid, + "doctype": "User", + "export_fields": fields, + "export_filters": filters, + "file_type": file_type, + }, + ) + + response = download_template("Excel") + self.assertEqual(response.status_code, 200) + self.assertEqual(response.headers["content-type"], "application/octet-stream") + self.assertGreater(cint(response.headers["content-length"]), 0) + self.assertEqual( + guess_mime(response.data), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + ) + + response = download_template("CSV") + self.assertEqual(response.status_code, 200) + self.assertIn("text/csv", response.headers["content-type"]) + self.assertGreater(cint(response.headers["content-length"]), 0) diff --git a/frappe/utils/response.py b/frappe/utils/response.py index 7734125249..fcd7b49a0f 100644 --- a/frappe/utils/response.py +++ b/frappe/utils/response.py @@ -112,7 +112,7 @@ def as_json(): def as_pdf(): response = Response() response.mimetype = "application/pdf" - response.headers.add("Content-Disposition", filename=frappe.response["filename"]) + response.headers.add("Content-Disposition", None, filename=frappe.response["filename"]) response.data = frappe.response["filecontent"] return response @@ -120,7 +120,7 @@ def as_pdf(): def as_binary(): response = Response() response.mimetype = "application/octet-stream" - response.headers.add("Content-Disposition", filename=frappe.response["filename"]) + response.headers.add("Content-Disposition", None, filename=frappe.response["filename"]) response.data = frappe.response["filecontent"] return response