From 70abe99bfd8aa92b00e88f899e102f680f309bd6 Mon Sep 17 00:00:00 2001 From: Rocco Corsi <5201151+rcorsi@users.noreply.github.com> Date: Mon, 26 May 2025 12:18:32 -0600 Subject: [PATCH] Tests: Add test for MimeType sniffing from filenames --- Tests/LibCore/CMakeLists.txt | 1 + Tests/LibCore/TestLibCoreMimeType.cpp | 55 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 Tests/LibCore/TestLibCoreMimeType.cpp diff --git a/Tests/LibCore/CMakeLists.txt b/Tests/LibCore/CMakeLists.txt index 78853982d7d..0ae6bc412fc 100644 --- a/Tests/LibCore/CMakeLists.txt +++ b/Tests/LibCore/CMakeLists.txt @@ -5,6 +5,7 @@ set(TEST_SOURCES TestLibCoreFileWatcher.cpp # FIXME: Identify and address the commit that caused this to start failing at runtime #TestLibCoreMappedFile.cpp + TestLibCoreMimeType.cpp TestLibCorePromise.cpp TestLibCoreSharedSingleProducerCircularQueue.cpp # FIXME: Identify and address the commit that caused this to start failing at runtime diff --git a/Tests/LibCore/TestLibCoreMimeType.cpp b/Tests/LibCore/TestLibCoreMimeType.cpp new file mode 100644 index 00000000000..56271b54538 --- /dev/null +++ b/Tests/LibCore/TestLibCoreMimeType.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2025, the Ladybird developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +static void check_filename_mimetype(Vector const& filepaths, StringView expected_mime_type) +{ + for (auto const& filename : filepaths) { + dbgln(filename, "\n"); + auto const& guessed_mime_type = Core::guess_mime_type_based_on_filename(filename); + EXPECT_EQ(guessed_mime_type, expected_mime_type); + } +} + +auto text_plain_filenames = Vector { + "main.c"sv, + "hello.txt"sv, + ".history"sv, + ".shellrc"sv, + "CMakeList.txt"sv, +}; +// FIXME: fails because .xht extension is in MimeType text/html and application/xhtml+xml +// auto html_filenames = Vector {"about.html"sv, "send-data-blob.htm"sv, "content.xht"sv, "dir/settings.html"sv,}; +auto xhtml_filenames = Vector { + "about.xhtml"sv, + "content.xht"sv, +}; +auto gzip_filenames = Vector { + "download.iso.gz"sv, + "backup.gzip"sv, + "hello.html.gz"sv, +}; +auto markdown_filenames = Vector { + "README.md"sv, + "changelog.md"sv, +}; +auto shell_filenames = Vector { + "script.sh"sv, +}; + +TEST_CASE(various_types_guessed) +{ + check_filename_mimetype(text_plain_filenames, "text/plain"sv); + // FIXME: fails because .xht extension is in MimeType text/html and application/xhtml+xml + // check_filename_mimetype(html_filenames, "text/html"sv); + check_filename_mimetype(xhtml_filenames, "application/xhtml+xml"sv); + check_filename_mimetype(gzip_filenames, "application/gzip"sv); + check_filename_mimetype(markdown_filenames, "text/markdown"sv); + check_filename_mimetype(shell_filenames, "text/x-shellscript"sv); +}