From 9c51474375ffc854e19ee7c98df6f2d64cf8bf18 Mon Sep 17 00:00:00 2001 From: Oleksandr Klymenko Date: Sun, 12 Oct 2025 23:38:15 +0200 Subject: [PATCH 1/2] Add edge case and validation tests for MimeTypeDetector Co-authored-by: Oleksandr Klymenko Signed-off-by: Oleksandr Klymenko --- .../gemini/MimeTypeDetectorTests.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/models/spring-ai-vertex-ai-gemini/src/test/java/org/springframework/ai/vertexai/gemini/MimeTypeDetectorTests.java b/models/spring-ai-vertex-ai-gemini/src/test/java/org/springframework/ai/vertexai/gemini/MimeTypeDetectorTests.java index c653df65f35..45b33265f58 100644 --- a/models/spring-ai-vertex-ai-gemini/src/test/java/org/springframework/ai/vertexai/gemini/MimeTypeDetectorTests.java +++ b/models/spring-ai-vertex-ai-gemini/src/test/java/org/springframework/ai/vertexai/gemini/MimeTypeDetectorTests.java @@ -22,6 +22,7 @@ import java.nio.file.Path; import java.util.stream.Stream; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -30,6 +31,7 @@ import org.springframework.util.MimeType; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; /** * @author YunKui Lu @@ -90,4 +92,29 @@ void getMimeTypeByString(String extension, MimeType expectedMimeType) { assertThat(mimeType).isEqualTo(expectedMimeType); } + @Test + void getMimeTypeWithEmptyString() { + assertThatThrownBy(() -> MimeTypeDetector.getMimeType("")).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Unable to detect the MIME type"); + } + + @Test + void getMimeTypeWithMultipleDots() { + // Should use the last extension + MimeType expectedPng = MimeTypeDetector.GEMINI_MIME_TYPES.get("png"); + assertThat(MimeTypeDetector.getMimeType("test.backup.png")).isEqualTo(expectedPng); + assertThat(MimeTypeDetector.getMimeType(new File("archive.tar.gz.png"))).isEqualTo(expectedPng); + } + + @Test + void getMimeTypeWithQueryParameters() { + URI uri = URI.create("https://example.com/styles.css?version=1.2&cache=false"); + + assertThatThrownBy(() -> MimeTypeDetector.getMimeType(uri)).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Unable to detect the MIME type"); + + assertThatThrownBy(() -> MimeTypeDetector.getMimeType(uri.toURL())).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Unable to detect the MIME type"); + } + } From 1a7789d317c47fc7453f9dd9d6950904612c1609 Mon Sep 17 00:00:00 2001 From: Oleksandr Klymenko Date: Mon, 13 Oct 2025 15:06:21 +0200 Subject: [PATCH 2/2] Add support for URLs with query parameters in MimeTypeDetector Co-authored-by: Oleksandr Klymenko Signed-off-by: Oleksandr Klymenko --- .../ai/vertexai/gemini/MimeTypeDetector.java | 4 ++-- .../ai/vertexai/gemini/MimeTypeDetectorTests.java | 12 +++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/MimeTypeDetector.java b/models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/MimeTypeDetector.java index 52b341fec8e..9e16ec21a51 100644 --- a/models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/MimeTypeDetector.java +++ b/models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/MimeTypeDetector.java @@ -59,11 +59,11 @@ public abstract class MimeTypeDetector { static final Map GEMINI_MIME_TYPES = new HashMap<>(); public static MimeType getMimeType(URL url) { - return getMimeType(url.getFile()); + return getMimeType(url.getPath()); } public static MimeType getMimeType(URI uri) { - return getMimeType(uri.toString()); + return getMimeType(uri.getPath()); } public static MimeType getMimeType(File file) { diff --git a/models/spring-ai-vertex-ai-gemini/src/test/java/org/springframework/ai/vertexai/gemini/MimeTypeDetectorTests.java b/models/spring-ai-vertex-ai-gemini/src/test/java/org/springframework/ai/vertexai/gemini/MimeTypeDetectorTests.java index 45b33265f58..fa3012c9abd 100644 --- a/models/spring-ai-vertex-ai-gemini/src/test/java/org/springframework/ai/vertexai/gemini/MimeTypeDetectorTests.java +++ b/models/spring-ai-vertex-ai-gemini/src/test/java/org/springframework/ai/vertexai/gemini/MimeTypeDetectorTests.java @@ -107,14 +107,12 @@ void getMimeTypeWithMultipleDots() { } @Test - void getMimeTypeWithQueryParameters() { - URI uri = URI.create("https://example.com/styles.css?version=1.2&cache=false"); + void getMimeTypeWithQueryParameters() throws MalformedURLException { + MimeType expectedJpg = MimeTypeDetector.GEMINI_MIME_TYPES.get("jpg"); + URI uri = URI.create("https://example.com/image.jpg?version=1.2&cache=false"); - assertThatThrownBy(() -> MimeTypeDetector.getMimeType(uri)).isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("Unable to detect the MIME type"); - - assertThatThrownBy(() -> MimeTypeDetector.getMimeType(uri.toURL())).isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("Unable to detect the MIME type"); + assertThat(MimeTypeDetector.getMimeType(uri)).isEqualTo(expectedJpg); + assertThat(MimeTypeDetector.getMimeType(uri.toURL())).isEqualTo(expectedJpg); } }