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));



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to