@@ -26,6 +26,52 @@ def test_from_file_path(tmp_path, request):
2626 assert b .meta == {"foo" : "bar" }
2727
2828
29+ @pytest .mark .parametrize (
30+ "file_path, expected_mime_types" ,
31+ [
32+ ("spam.jpeg" , {"image/jpeg" }),
33+ ("spam.jpg" , {"image/jpeg" }),
34+ ("spam.png" , {"image/png" }),
35+ ("spam.gif" , {"image/gif" }),
36+ ("spam.svg" , {"image/svg+xml" }),
37+ ("spam.js" , {"text/javascript" , "application/javascript" }),
38+ ("spam.txt" , {"text/plain" }),
39+ ("spam.html" , {"text/html" }),
40+ ("spam.htm" , {"text/html" }),
41+ ("spam.css" , {"text/css" }),
42+ ("spam.csv" , {"text/csv" }),
43+ ("spam.md" , {"text/markdown" }), # custom mapping
44+ ("spam.markdown" , {"text/markdown" }), # custom mapping
45+ ("spam.msg" , {"application/vnd.ms-outlook" }), # custom mapping
46+ ("spam.pdf" , {"application/pdf" }),
47+ ("spam.xml" , {"application/xml" , "text/xml" }),
48+ ("spam.json" , {"application/json" }),
49+ ("spam.doc" , {"application/msword" }),
50+ ("spam.docx" , {"application/vnd.openxmlformats-officedocument.wordprocessingml.document" }),
51+ ("spam.xls" , {"application/vnd.ms-excel" }),
52+ ("spam.xlsx" , {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }),
53+ ("spam.ppt" , {"application/vnd.ms-powerpoint" }),
54+ ("spam.pptx" , {"application/vnd.openxmlformats-officedocument.presentationml.presentation" }),
55+ ],
56+ )
57+ def test_from_file_path_guess_mime_type (file_path , expected_mime_types , tmp_path ):
58+ test_file = tmp_path / file_path
59+ test_file .touch ()
60+
61+ b = ByteStream .from_file_path (test_file , guess_mime_type = True )
62+ assert b .mime_type in expected_mime_types
63+
64+
65+ def test_explicit_mime_type_is_not_overwritten_by_guessing (tmp_path ):
66+ # create empty file with correct extension
67+ test_file = tmp_path / "sample.md"
68+ test_file .touch ()
69+
70+ explicit_mime_type = "text/x-rst"
71+ b = ByteStream .from_file_path (test_file , mime_type = explicit_mime_type , guess_mime_type = True )
72+ assert b .mime_type == explicit_mime_type
73+
74+
2975def test_from_string ():
3076 test_string = "Hello, world!"
3177 b = ByteStream .from_string (test_string )
0 commit comments