[
https://issues.apache.org/jira/browse/KNOX-3452?focusedWorklogId=1043293&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1043293
]
ASF GitHub Bot logged work on KNOX-3452:
----------------------------------------
Author: ASF GitHub Bot
Created on: 22/Sep/26 08:53
Start Date: 22/Sep/26 08:53
Worklog Time Spent: 10m
Work Description: Raghav-Mah3shwari commented on code in PR #1393:
URL: https://github.com/apache/knox/pull/1393#discussion_r4069936901
##########
gateway-util-common/src/test/java/org/apache/knox/gateway/util/MimeTypesTest.java:
##########
@@ -0,0 +1,271 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.knox.gateway.util;
+
+import jakarta.activation.MimeType;
+import org.junit.Test;
+
+import java.nio.charset.StandardCharsets;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class MimeTypesTest {
+
+ @Test
+ public void testCreateReturnsNullForNullBaseType() {
+ assertThat(MimeTypes.create(null, StandardCharsets.UTF_8.name()),
nullValue());
+ }
+
+ @Test
+ public void testCreateAddsEncodingWhenCharsetIsMissing() {
+ MimeType type = MimeTypes.create("application/json",
StandardCharsets.UTF_8.name());
+
+ assertThat(type.getBaseType(), is("application/json"));
+ assertThat(MimeTypes.getCharset(type, null),
is(StandardCharsets.UTF_8.name()));
+ }
+
+ @Test
+ public void testCreateAddsCustomEncodingWhenCharsetIsMissing() {
+ MimeType type = MimeTypes.create("text/plain", "ISO-8859-1");
+
+ assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+ }
+
+ @Test
+ public void testCreateSupportsWildcardMimeType() {
+ MimeType type = MimeTypes.create("text/*", StandardCharsets.UTF_8.name());
+
+ assertThat(type.getBaseType(), is("text/*"));
+ assertThat(MimeTypes.getCharset(type, null),
is(StandardCharsets.UTF_8.name()));
+ }
+
+ @Test
+ public void testCreatePreservesExistingCharset() {
+ MimeType type = MimeTypes.create("application/json; charset=ISO-8859-1",
StandardCharsets.UTF_8.name());
+
+ assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+ }
+
+ @Test
+ public void testCreateReadsQuotedCharsetValue() {
+ MimeType type = MimeTypes.create("text/plain; charset=\"ISO-8859-1\"",
null);
+
+ assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+ }
+
+ @Test
+ public void testCreatePreservesOtherMimeTypeParameters() {
+ MimeType type = MimeTypes.create("application/json; version=1",
StandardCharsets.UTF_8.name());
+
+ assertThat(type.getParameter("version"), is("1"));
+ assertThat(MimeTypes.getCharset(type, null),
is(StandardCharsets.UTF_8.name()));
+ }
+
+ @Test
+ public void testCreatePreservesOtherParametersWhenCharsetAlreadyExists() {
+ MimeType type = MimeTypes.create(
+ "application/json; version=1; charset=ISO-8859-1",
StandardCharsets.UTF_8.name());
+
+ assertThat(type.getParameter("version"), is("1"));
+ assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1"));
+ }
+
+ @Test
+ public void
testCreateRecognizesCharsetParameterRegardlessOfParameterNameCase() {
Review Comment:
The case-insensitive logic is inside Jakarta Activation, not in Knox’s
MimeTypes class.
The call chain is:
MimeTypes.getCharset(...)
-> type.getParameter("charset")
-> MimeTypeParameterList.get("charset")
In the Jakarta Activation implementation, MimeTypeParameterList.get
normalizes the lookup key:
parameters.get(name.trim().toLowerCase(Locale.ENGLISH));
Issue Time Tracking
-------------------
Worklog Id: (was: 1043293)
Time Spent: 1h (was: 50m)
> Add comprehensive unit tests for MimeTypes
> ------------------------------------------
>
> Key: KNOX-3452
> URL: https://issues.apache.org/jira/browse/KNOX-3452
> Project: Apache Knox
> Issue Type: Task
> Reporter: Raghav Maheshwari
> Priority: Major
> Time Spent: 1h
> Remaining Estimate: 0h
>
> Add comprehensive unit test coverage for
> org.apache.knox.gateway.util.MimeTypes.
> The tests should cover MIME type creation, charset handling, invalid input,
> charset fallback behavior, MIME parameter preservation, wildcard MIME types,
> normalization, and default charset lookup.
> Test coverage:
> •Null MIME type creation
> •Valid MIME type creation
> •Custom charset insertion
> •UTF-8 charset insertion
> •Existing charset preservation
> •Quoted charset values
> •Case-insensitive charset parameter names
> •Preservation of additional MIME parameters
> •Wildcard MIME types
> •Malformed, empty, and whitespace-only MIME types
> •Charset lookup with explicit and fallback values
> •Charset lookup with null MIME types
> •Adding and replacing charset parameters
> •Preservation of parameters when setting charset
> •Default charset lookup for supported MIME types
> •Case and whitespace normalization
> •Unknown, empty, parameterized, and structured MIME types
--
This message was sent by Atlassian Jira
(v8.20.10#820010)