fix: PDF and raw response (#22402)

This commit is contained in:
Ankush Menat 2023-09-13 22:12:49 +05:30 committed by GitHub
parent 9244140816
commit 909457de3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 3 deletions

View file

@ -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)

View file

@ -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