Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | """Tests for the mimetypes utility module.""" |
| 2 | |
| 3 | import re |
| 4 | from pathlib import Path |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from composio.utils.mimetypes import ( |
| 9 | _default, |
| 10 | _types, |
| 11 | get_extension_from_mime_type, |
| 12 | guess, |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | class TestGuess: |
| 17 | """Test cases for guess() (filename/extension -> MIME type).""" |
| 18 | |
| 19 | @pytest.mark.parametrize( |
| 20 | ("filename", "expected"), |
| 21 | [ |
| 22 | ("a.json", "application/json"), |
| 23 | ("a.html", "text/html"), |
| 24 | ("a.txt", "text/plain"), |
| 25 | ("a.pdf", "application/pdf"), |
| 26 | ("a.css", "text/css"), |
| 27 | ("a.js", "text/javascript"), |
| 28 | ( |
| 29 | "a.pptx", |
| 30 | "application/vnd.openxmlformats-officedocument.presentationml.presentation", |
| 31 | ), |
| 32 | ("a.psd", "image/vnd.adobe.photoshop"), |
| 33 | ("a.py", "text/x-python"), |
| 34 | ("a.h5", "application/x-hdf5"), |
| 35 | ("a.pyc", "application/x-python-code"), |
| 36 | ("a.png", "image/png"), |
| 37 | ("a.jpg", "image/jpeg"), |
| 38 | ("a.jpeg", "image/jpeg"), |
| 39 | ("a.gif", "image/gif"), |
| 40 | ("a.mp4", "video/mp4"), |
| 41 | ("a.zip", "application/zip"), |
| 42 | ], |
| 43 | ) |
| 44 | def test_known_extensions(self, filename: str, expected: str) -> None: |
| 45 | """A known extension resolves to its MIME type.""" |
| 46 | assert guess(filename) == expected |
| 47 | |
| 48 | def test_jpg_uses_the_standard_mime_type(self) -> None: |
| 49 | """``.jpg`` resolves to the IANA-registered ``image/jpeg``, matching |
| 50 | its ``.jpeg``/``.jpe`` siblings and ``mimetypes.guess_type`` in the |
| 51 | standard library, rather than the non-standard ``image/jpg``. The |
| 52 | reverse ``_MIME_TO_EXT`` map still accepts ``image/jpg`` from servers |
| 53 | that send it.""" |
| 54 | assert guess("a.jpg") == "image/jpeg" |
| 55 | assert guess("a.jpeg") == "image/jpeg" |
| 56 | assert guess("a.jpe") == "image/jpeg" |
| 57 | |
| 58 | def test_extension_lookup_is_case_insensitive(self) -> None: |
| 59 | """Suffixes are normalized to lower case, matching the sibling |
| 60 | ``get_extension_from_mime_type`` helper and ``mimetypes.guess_type``.""" |
| 61 | assert guess("photo.PNG") == "image/png" |
| 62 | assert guess("report.PDF") == "application/pdf" |
| 63 | assert guess("archive.ZIP") == "application/zip" |
| 64 | |
| 65 | def test_unknown_extension_returns_default(self) -> None: |
| 66 | assert guess("file.unknownext") == _default |
| 67 | |
| 68 | def test_no_extension_returns_default(self) -> None: |
| 69 | assert guess("noextension") == _default |
| 70 | |
| 71 | def test_uses_only_the_final_suffix(self) -> None: |
| 72 | # Path().suffix is the last component, so ".gz" here, not ".tar.gz". |
| 73 | assert guess("archive.tar.gz") == guess("archive.gz") |
| 74 | |
| 75 | def test_accepts_path_objects(self) -> None: |
| 76 | assert guess(Path("dir/sub/a.png")) == "image/png" |
| 77 | |
| 78 | |
| 79 | TS_MIME_TABLE = ( |
| 80 | Path(__file__).resolve().parents[2] / "ts/packages/core/src/utils/mime.ts" |
| 81 | ) |
| 82 | |
| 83 | |
| 84 | def test_types_match_the_typescript_sdk() -> None: |
| 85 | """A local file uploads with the same MIME type from either SDK.""" |
| 86 | source = TS_MIME_TABLE.read_text() |
| 87 | table = source[source.index("MIME_TYPES_BY_EXTENSION") :] |
| 88 | table = table[: table.index("};")] |
| 89 | ts_types = { |
| 90 | f".{extension}": mime_type |
| 91 | for extension, mime_type in re.findall( |
| 92 | r"^\s+'?([a-z0-9]+)'?: '([^']+)',$", table, re.M |
| 93 | ) |
| 94 | } |
| 95 | assert _types == ts_types |
| 96 | |
| 97 | |
| 98 | class TestGetExtensionFromMimeType: |
| 99 | """Test cases for get_extension_from_mime_type() (MIME type -> extension).""" |
| 100 | |
| 101 | @pytest.mark.parametrize( |
| 102 | ("mime_type", "expected"), |
| 103 | [ |
| 104 | ("image/png", "png"), |
| 105 | ("application/json", "json"), |
| 106 | ("text/html", "html"), |
| 107 | ("application/pdf", "pdf"), |
| 108 | ("image/jpeg", "jpg"), |
| 109 | ("video/mp4", "mp4"), |
| 110 | ("application/vnd.ms-excel", "xls"), |
| 111 | ], |
| 112 | ) |
| 113 | def test_direct_mapping(self, mime_type: str, expected: str) -> None: |
| 114 | assert get_extension_from_mime_type(mime_type) == expected |
| 115 | |
| 116 | def test_strips_parameters(self) -> None: |
| 117 | assert get_extension_from_mime_type("text/plain; charset=utf-8") == "txt" |
| 118 | |
| 119 | def test_is_case_insensitive(self) -> None: |
| 120 | assert get_extension_from_mime_type("APPLICATION/JSON") == "json" |
| 121 | |
| 122 | def test_surrounding_whitespace_is_ignored(self) -> None: |
| 123 | assert get_extension_from_mime_type(" image/png ") == "png" |
| 124 | |
| 125 | @pytest.mark.parametrize( |
| 126 | ("mime_type", "expected"), |
| 127 | [ |
| 128 | ("application/atom+xml", "atom"), |
| 129 | ("application/rss+xml", "rss"), |
| 130 | ], |
| 131 | ) |
| 132 | def test_structured_suffix_known_prefix( |
| 133 | self, mime_type: str, expected: str |
| 134 | ) -> None: |
| 135 | """For known prefixes the prefix wins over the suffix.""" |
| 136 | assert get_extension_from_mime_type(mime_type) == expected |
| 137 | |
| 138 | def test_structured_suffix_uses_suffix(self) -> None: |
| 139 | """A '+json'/'+xml' style suffix maps to that structured format.""" |
| 140 | assert get_extension_from_mime_type("application/vnd.api+json") == "json" |
| 141 | |
| 142 | def test_structured_suffix_unknown_falls_back_to_suffix(self) -> None: |
| 143 | assert get_extension_from_mime_type("application/foo+bar") == "bar" |
| 144 | |
| 145 | def test_unknown_type_uses_subtype(self) -> None: |
| 146 | assert get_extension_from_mime_type("application/x-custom") == "x-custom" |
| 147 | assert get_extension_from_mime_type("totally/unknown") == "unknown" |
| 148 | |
| 149 | def test_empty_subtype_defaults_to_txt(self) -> None: |
| 150 | assert get_extension_from_mime_type("application/") == "txt" |
| 151 | |
| 152 | def test_no_subtype_defaults_to_bin(self) -> None: |
| 153 | assert get_extension_from_mime_type("notamimetype") == "bin" |
| 154 | assert get_extension_from_mime_type("") == "bin" |