Skip to content

Commit 8a91342

Browse files
authored
[py]: guess mimetypes in webserver for content serving (#16449)
1 parent 10e563a commit 8a91342

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

py/test/selenium/webdriver/common/webserver.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import re
2525
import threading
2626

27+
import filetype
28+
2729
try:
2830
from urllib import request as urllib_request
2931
except ImportError:
@@ -70,8 +72,20 @@ def _serve_page(self, page_number):
7072

7173
def _serve_file(self, file_path):
7274
"""Serve a file from the HTML root directory."""
73-
with open(file_path, encoding="latin-1") as f:
74-
return f.read().encode("utf-8")
75+
with open(file_path, "rb") as f:
76+
content = f.read()
77+
78+
kind = filetype.guess(content)
79+
if kind is not None:
80+
return content, kind.mime
81+
82+
# fallback for text files that filetype can't detect
83+
if file_path.endswith(".txt"):
84+
return content, "text/plain"
85+
elif file_path.endswith(".json"):
86+
return content, "application/json"
87+
else:
88+
return content, "text/html"
7589

7690
def _send_response(self, content_type="text/html"):
7791
"""Send a response."""
@@ -89,8 +103,7 @@ def do_GET(self):
89103
self._send_response("text/html")
90104
self.wfile.write(html)
91105
elif os.path.isfile(file_path):
92-
content_type = "application/json" if file_path.endswith(".json") else "text/html"
93-
content = self._serve_file(file_path)
106+
content, content_type = self._serve_file(file_path)
94107
self._send_response(content_type)
95108
self.wfile.write(content)
96109
else:

py/test/selenium/webdriver/remote/remote_downloads_tests.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828
def test_get_downloadable_files(driver, pages):
2929
_browser_downloads(driver, pages)
3030
file_names = driver.get_downloadable_files()
31-
# TODO: why is Chrome downloading files as .html???
32-
# assert "file_1.txt" in file_names
33-
# assert "file_2.jpg" in file_names
34-
assert any(f in file_names for f in ("file_1.txt", "file_1.htm", "file_1.html"))
35-
assert any(f in file_names for f in ("file_2.jpg", "file_2.htm", "file_2.html"))
31+
32+
assert "file_1.txt" in file_names
33+
assert "file_2.jpg" in file_names
3634
assert type(file_names) is list
3735

3836

@@ -42,12 +40,8 @@ def test_download_file(driver, pages):
4240

4341
# Get a list of downloadable files and find the txt file
4442
downloadable_files = driver.get_downloadable_files()
45-
# TODO: why is Chrome downloading files as .html???
46-
# text_file_name = next((file for file in downloadable_files if file.endswith(".txt")), None)
47-
text_file_name = next(
48-
(f for f in downloadable_files if all((f.endswith((".txt", ".htm", ".html")), f.startswith("file_1")))), None
49-
)
50-
assert text_file_name is not None, "Could not find file in downloadable files"
43+
text_file_name = next((file for file in downloadable_files if file.endswith(".txt")), None)
44+
assert text_file_name is not None, "Could not find a .txt file in downloadable files"
5145

5246
with tempfile.TemporaryDirectory() as target_directory:
5347
driver.download_file(text_file_name, target_directory)
@@ -69,8 +63,4 @@ def _browser_downloads(driver, pages):
6963
pages.load("downloads/download.html")
7064
driver.find_element(By.ID, "file-1").click()
7165
driver.find_element(By.ID, "file-2").click()
72-
# TODO: why is Chrome downloading files as .html???
73-
# WebDriverWait(driver, 5).until(lambda d: "file_2.jpg" in d.get_downloadable_files())
74-
WebDriverWait(driver, 5).until(
75-
lambda d: any(f in d.get_downloadable_files() for f in ("file_2.jpg", "file_2.htm", "file_2.html"))
76-
)
66+
WebDriverWait(driver, 3).until(lambda d: "file_2.jpg" in d.get_downloadable_files())

0 commit comments

Comments
 (0)