Browse Source

Fix URL encoding in content accessor by replacing quote_plus with quote for file URLs (#2542)

* Fix URL encoding in content accessor by replacing quote_plus with quote for file URLs

* Add test for file download handling with spaces in file path

This commit introduces a new test case to verify the functionality of file downloads when the file path contains spaces. The test creates a temporary file, sets the content variable in the GUI, and checks the generated markdown string against expected output, ensuring proper URL encoding for spaces.

* Update expected output for file download test to reflect correct file path with spaces

This commit modifies the expected output in the test for file downloads to ensure the correct URL is generated when the file path contains spaces. The change updates the defaultContent attribute to include the proper path format.
PRANSHU GUPTA 1 tháng trước cách đây
mục cha
commit
0336c92137

+ 1 - 1
taipy/gui/data/content_accessor.py

@@ -113,7 +113,7 @@ class _ContentAccessor:
             self.__content_paths[url_path] = dir_path
             file_url = f"{url_path}/{path.name}"
             self.__url_is_image[file_url] = image
-            return (urllib.parse.quote_plus(file_url, safe="/"),)
+            return (urllib.parse.quote(file_url, safe="/"),)
         elif _has_magic_module:
             try:
                 mime = magic.from_buffer(value, mime=True)

+ 20 - 0
tests/gui/control/test_file_download.py

@@ -50,6 +50,26 @@ def test_file_download_path_md(gui: Gui, test_client, helpers):
     helpers.test_control_md(gui, md_string, expected_list)
 
 
+def test_file_download_with_spaces_path_md(gui: Gui, test_client, helpers):
+    resources_dir = pathlib.Path(__file__).parent.parent / "resources"
+    test_file_path = resources_dir / "test file with spaces.txt"
+
+    try:
+        with open(test_file_path, "w") as f:
+            f.write("Test content")
+
+        gui._bind_var_val("content", str(test_file_path.resolve()))
+        md_string = "<|{content}|file_download|>"
+        expected_list = [
+            "<FileDownload",
+            'defaultContent="/taipy-content/taipyStatic0/test%20file%20with%20spaces.txt"',
+        ]
+        helpers.test_control_md(gui, md_string, expected_list)
+    finally:
+        if test_file_path.exists():
+            test_file_path.unlink()
+
+
 def test_file_download_any_file_md(gui: Gui, test_client, helpers):
     with open(os.path.abspath(__file__), "rb") as content:
         gui._bind_var_val("content", content.read())