From b2e14e9c2d7a36c8bba72766e1ff9d2cb32d0839 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Thu, 16 Oct 2025 21:51:24 +0530 Subject: [PATCH 1/3] use mimetypes in webserver for content type --- .../selenium/webdriver/common/webserver.py | 3 ++- .../remote/remote_downloads_tests.py | 22 +++++-------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/py/test/selenium/webdriver/common/webserver.py b/py/test/selenium/webdriver/common/webserver.py index e3d0bb2577d30..1a0da8587c769 100644 --- a/py/test/selenium/webdriver/common/webserver.py +++ b/py/test/selenium/webdriver/common/webserver.py @@ -20,6 +20,7 @@ import contextlib import logging +import mimetypes import os import re import threading @@ -89,7 +90,7 @@ def do_GET(self): self._send_response("text/html") self.wfile.write(html) elif os.path.isfile(file_path): - content_type = "application/json" if file_path.endswith(".json") else "text/html" + content_type, _ = mimetypes.guess_type(file_path) content = self._serve_file(file_path) self._send_response(content_type) self.wfile.write(content) diff --git a/py/test/selenium/webdriver/remote/remote_downloads_tests.py b/py/test/selenium/webdriver/remote/remote_downloads_tests.py index 74259ac22da3d..bd7623956f63f 100644 --- a/py/test/selenium/webdriver/remote/remote_downloads_tests.py +++ b/py/test/selenium/webdriver/remote/remote_downloads_tests.py @@ -28,11 +28,9 @@ def test_get_downloadable_files(driver, pages): _browser_downloads(driver, pages) file_names = driver.get_downloadable_files() - # TODO: why is Chrome downloading files as .html??? - # assert "file_1.txt" in file_names - # assert "file_2.jpg" in file_names - assert any(f in file_names for f in ("file_1.txt", "file_1.htm", "file_1.html")) - assert any(f in file_names for f in ("file_2.jpg", "file_2.htm", "file_2.html")) + + assert "file_1.txt" in file_names + assert "file_2.jpg" in file_names assert type(file_names) is list @@ -42,12 +40,8 @@ def test_download_file(driver, pages): # Get a list of downloadable files and find the txt file downloadable_files = driver.get_downloadable_files() - # TODO: why is Chrome downloading files as .html??? - # text_file_name = next((file for file in downloadable_files if file.endswith(".txt")), None) - text_file_name = next( - (f for f in downloadable_files if all((f.endswith((".txt", ".htm", ".html")), f.startswith("file_1")))), None - ) - assert text_file_name is not None, "Could not find file in downloadable files" + text_file_name = next((file for file in downloadable_files if file.endswith(".txt")), None) + assert text_file_name is not None, "Could not find a .txt file in downloadable files" with tempfile.TemporaryDirectory() as target_directory: driver.download_file(text_file_name, target_directory) @@ -69,8 +63,4 @@ def _browser_downloads(driver, pages): pages.load("downloads/download.html") driver.find_element(By.ID, "file-1").click() driver.find_element(By.ID, "file-2").click() - # TODO: why is Chrome downloading files as .html??? - # WebDriverWait(driver, 5).until(lambda d: "file_2.jpg" in d.get_downloadable_files()) - WebDriverWait(driver, 5).until( - lambda d: any(f in d.get_downloadable_files() for f in ("file_2.jpg", "file_2.htm", "file_2.html")) - ) + WebDriverWait(driver, 3).until(lambda d: "file_2.jpg" in d.get_downloadable_files()) From de49ff07c8d8682c8100cb438c4d4cafa7897358 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Thu, 16 Oct 2025 22:18:57 +0530 Subject: [PATCH 2/3] fix file serving --- py/test/selenium/webdriver/common/webserver.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/py/test/selenium/webdriver/common/webserver.py b/py/test/selenium/webdriver/common/webserver.py index 1a0da8587c769..5407cbf51b69e 100644 --- a/py/test/selenium/webdriver/common/webserver.py +++ b/py/test/selenium/webdriver/common/webserver.py @@ -71,8 +71,20 @@ def _serve_page(self, page_number): def _serve_file(self, file_path): """Serve a file from the HTML root directory.""" - with open(file_path, encoding="latin-1") as f: - return f.read().encode("utf-8") + content_type, _ = mimetypes.guess_type(file_path) + + if content_type and ( + content_type.startswith("image/") + or content_type.startswith("application/") + or content_type.startswith("video/") + or content_type.startswith("audio/") + ): + with open(file_path, "rb") as f: + return f.read() + else: + # text files + with open(file_path, encoding="latin-1") as f: + return f.read().encode("utf-8") def _send_response(self, content_type="text/html"): """Send a response.""" From 0cb52a19990e586ad3a1d3840afac30efedd06bc Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Thu, 16 Oct 2025 22:58:05 +0530 Subject: [PATCH 3/3] use filetype --- .../selenium/webdriver/common/webserver.py | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/py/test/selenium/webdriver/common/webserver.py b/py/test/selenium/webdriver/common/webserver.py index 5407cbf51b69e..82e0b239af26c 100644 --- a/py/test/selenium/webdriver/common/webserver.py +++ b/py/test/selenium/webdriver/common/webserver.py @@ -20,11 +20,12 @@ import contextlib import logging -import mimetypes import os import re import threading +import filetype + try: from urllib import request as urllib_request except ImportError: @@ -71,20 +72,20 @@ def _serve_page(self, page_number): def _serve_file(self, file_path): """Serve a file from the HTML root directory.""" - content_type, _ = mimetypes.guess_type(file_path) - - if content_type and ( - content_type.startswith("image/") - or content_type.startswith("application/") - or content_type.startswith("video/") - or content_type.startswith("audio/") - ): - with open(file_path, "rb") as f: - return f.read() + with open(file_path, "rb") as f: + content = f.read() + + kind = filetype.guess(content) + if kind is not None: + return content, kind.mime + + # fallback for text files that filetype can't detect + if file_path.endswith(".txt"): + return content, "text/plain" + elif file_path.endswith(".json"): + return content, "application/json" else: - # text files - with open(file_path, encoding="latin-1") as f: - return f.read().encode("utf-8") + return content, "text/html" def _send_response(self, content_type="text/html"): """Send a response.""" @@ -102,8 +103,7 @@ def do_GET(self): self._send_response("text/html") self.wfile.write(html) elif os.path.isfile(file_path): - content_type, _ = mimetypes.guess_type(file_path) - content = self._serve_file(file_path) + content, content_type = self._serve_file(file_path) self._send_response(content_type) self.wfile.write(content) else: