1
+"""Tests for the mimetypes utility module."""
2
+
3
+from pathlib import Path
4
+
5
+import pytest
6
+
7
+from composio.utils.mimetypes import (
8
+ _default,
9
+ get_extension_from_mime_type,
10
+ guess,
11
+)
12
+
13
+
14
+class TestGuess:
15
+ """Test cases for guess() (filename/extension -> MIME type)."""
16
+
17
+ @pytest.mark.parametrize(
18
+ ("filename", "expected"),
19
+ [
20
+ ("a.json", "application/json"),
21
+ ("a.html", "text/html"),
22
+ ("a.txt", "text/plain"),
23
+ ("a.pdf", "application/pdf"),
24
+ ("a.css", "text/css"),
25
+ ("a.js", "application/javascript"),
26
+ ("a.png", "image/png"),
27
+ ("a.gif", "image/gif"),
28
+ ("a.mp4", "video/mp4"),
29
+ ("a.zip", "application/zip"),
30
+ ],
31
+ )
32
+ def test_known_extensions(self, filename: str, expected: str) -> None:
33
+ """A known extension resolves to its MIME type."""
34
+ assert guess(filename) == expected
35
+
36
+ def test_extension_lookup_is_case_sensitive(self) -> None:
37
+ """The extension table is lower-case, so an upper-case suffix misses."""
38
+ assert guess("photo.PNG") == _default
39
+
40
+ def test_unknown_extension_returns_default(self) -> None:
41
+ assert guess("file.unknownext") == _default
42
+
43
+ def test_no_extension_returns_default(self) -> None:
44
+ assert guess("noextension") == _default
45
+
46
+ def test_uses_only_the_final_suffix(self) -> None:
47
+ # Path().suffix is the last component, so ".gz" here, not ".tar.gz".
48
+ assert guess("archive.tar.gz") == guess("archive.gz")
49
+
50
+ def test_accepts_path_objects(self) -> None:
51
+ assert guess(Path("dir/sub/a.png")) == "image/png"
52
+
53
+
54
+class TestGetExtensionFromMimeType:
55
+ """Test cases for get_extension_from_mime_type() (MIME type -> extension)."""
56
+
57
+ @pytest.mark.parametrize(
58
+ ("mime_type", "expected"),
59
+ [
60
+ ("image/png", "png"),
61
+ ("application/json", "json"),
62
+ ("text/html", "html"),
63
+ ("application/pdf", "pdf"),
64
+ ("image/jpeg", "jpg"),
65
+ ("video/mp4", "mp4"),
66
+ ("application/vnd.ms-excel", "xls"),
67
+ ],
68
+ )
69
+ def test_direct_mapping(self, mime_type: str, expected: str) -> None:
70
+ assert get_extension_from_mime_type(mime_type) == expected
71
+
72
+ def test_strips_parameters(self) -> None:
73
+ assert get_extension_from_mime_type("text/plain; charset=utf-8") == "txt"
74
+
75
+ def test_is_case_insensitive(self) -> None:
76
+ assert get_extension_from_mime_type("APPLICATION/JSON") == "json"
77
+
78
+ def test_surrounding_whitespace_is_ignored(self) -> None:
79
+ assert get_extension_from_mime_type(" image/png ") == "png"
80
+
81
+ @pytest.mark.parametrize(
82
+ ("mime_type", "expected"),
83
+ [
84
+ ("application/atom+xml", "atom"),
85
+ ("application/rss+xml", "rss"),
86
+ ],
87
+ )
88
+ def test_structured_suffix_known_prefix(
89
+ self, mime_type: str, expected: str
90
+ ) -> None:
91
+ """For known prefixes the prefix wins over the suffix."""
92
+ assert get_extension_from_mime_type(mime_type) == expected
93
+
94
+ def test_structured_suffix_uses_suffix(self) -> None:
95
+ """A '+json'/'+xml' style suffix maps to that structured format."""
96
+ assert get_extension_from_mime_type("application/vnd.api+json") == "json"
97
+
98
+ def test_structured_suffix_unknown_falls_back_to_suffix(self) -> None:
99
+ assert get_extension_from_mime_type("application/foo+bar") == "bar"
100
+
101
+ def test_unknown_type_uses_subtype(self) -> None:
102
+ assert get_extension_from_mime_type("application/x-custom") == "x-custom"
103
+ assert get_extension_from_mime_type("totally/unknown") == "unknown"
104
+
105
+ def test_empty_subtype_defaults_to_txt(self) -> None:
106
+ assert get_extension_from_mime_type("application/") == "txt"
107
+
108
+ def test_no_subtype_defaults_to_bin(self) -> None:
109
+ assert get_extension_from_mime_type("notamimetype") == "bin"
110
+ assert get_extension_from_mime_type("") == "bin"