diff --git a/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNodeFactory.java b/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNodeFactory.java index adb97018..39151393 100644 --- a/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNodeFactory.java +++ b/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormParseNodeFactory.java @@ -28,7 +28,7 @@ public FormParseNodeFactory() {} Objects.requireNonNull(rawResponse, "parameter rawResponse cannot be null"); if (contentType.isEmpty()) { throw new NullPointerException("contentType cannot be empty"); - } else if (!contentType.equals(validContentType)) { + } else if (!contentType.split(";", 2)[0].trim().equalsIgnoreCase(validContentType)) { throw new IllegalArgumentException("expected a " + validContentType + " content type"); } String rawText; diff --git a/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormSerializationWriterFactory.java b/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormSerializationWriterFactory.java index 3cb27ce2..030529e7 100644 --- a/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormSerializationWriterFactory.java +++ b/components/serialization/form/src/main/java/com/microsoft/kiota/serialization/FormSerializationWriterFactory.java @@ -20,7 +20,7 @@ public FormSerializationWriterFactory() {} Objects.requireNonNull(contentType, "parameter contentType cannot be null"); if (contentType.isEmpty()) { throw new NullPointerException("contentType cannot be empty"); - } else if (!contentType.equals(validContentType)) { + } else if (!contentType.split(";", 2)[0].trim().equalsIgnoreCase(validContentType)) { throw new IllegalArgumentException("expected a " + validContentType + " content type"); } return new FormSerializationWriter(); diff --git a/components/serialization/form/src/test/java/com/microsoft/kiota/serialization/ParseNodeFactoryTests.java b/components/serialization/form/src/test/java/com/microsoft/kiota/serialization/ParseNodeFactoryTests.java index c91e7eea..d1033f55 100644 --- a/components/serialization/form/src/test/java/com/microsoft/kiota/serialization/ParseNodeFactoryTests.java +++ b/components/serialization/form/src/test/java/com/microsoft/kiota/serialization/ParseNodeFactoryTests.java @@ -4,6 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; @@ -37,4 +39,40 @@ public void throwsArgumentNullExceptionForNoContentType() throws UnsupportedEnco assertThrows( NullPointerException.class, () -> _parseNodeFactory.getParseNode("", rawResponse)); } + + @ParameterizedTest + @ValueSource( + strings = { + "application/x-www-form-urlencoded; charset=utf-8", + "application/x-www-form-urlencoded; charset=UTF-8", + "application/x-www-form-urlencoded; charset=us-ascii", + "APPLICATION/X-WWW-FORM-URLENCODED", + "APPLICATION/X-WWW-FORM-URLENCODED; CHARSET=UTF-8", + "Application/x-www-form-urlencoded", + "Application/x-www-form-urlencoded; charset=utf-8", + }) + public void getsParseNodeForContentTypeWithParametersOrCaseVariants(final String inputType) + throws UnsupportedEncodingException { + final var initialString = "key1=value1&key2=value2"; + final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8")); + final var parseNode = _parseNodeFactory.getParseNode(inputType, rawResponse); + assertNotNull(parseNode); + } + + @ParameterizedTest + @ValueSource( + strings = { + ";", + ";;", + "; charset=utf-8", + " ; charset=utf-8", + }) + public void throwsIllegalArgumentExceptionForDelimiterOnlyOrMissingMediaType( + final String inputType) throws UnsupportedEncodingException { + final var initialString = "key1=value1&key2=value2"; + final var rawResponse = new ByteArrayInputStream(initialString.getBytes("UTF-8")); + assertThrows( + IllegalArgumentException.class, + () -> _parseNodeFactory.getParseNode(inputType, rawResponse)); + } } diff --git a/components/serialization/form/src/test/java/com/microsoft/kiota/serialization/SerializationWriterFactoryTests.java b/components/serialization/form/src/test/java/com/microsoft/kiota/serialization/SerializationWriterFactoryTests.java index fdbb3797..154fa938 100644 --- a/components/serialization/form/src/test/java/com/microsoft/kiota/serialization/SerializationWriterFactoryTests.java +++ b/components/serialization/form/src/test/java/com/microsoft/kiota/serialization/SerializationWriterFactoryTests.java @@ -4,6 +4,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; public class SerializationWriterFactoryTests { private static final FormSerializationWriterFactory _serializationWriterFactory = @@ -30,4 +32,36 @@ public void throwsArgumentNullExceptionForNoContentType() { NullPointerException.class, () -> _serializationWriterFactory.getSerializationWriter("")); } + + @ParameterizedTest + @ValueSource( + strings = { + "application/x-www-form-urlencoded; charset=utf-8", + "application/x-www-form-urlencoded; charset=UTF-8", + "application/x-www-form-urlencoded; charset=us-ascii", + "APPLICATION/X-WWW-FORM-URLENCODED", + "APPLICATION/X-WWW-FORM-URLENCODED; CHARSET=UTF-8", + "Application/x-www-form-urlencoded", + "Application/x-www-form-urlencoded; charset=utf-8", + }) + public void getsWriterForContentTypeWithParametersOrCaseVariants(final String inputType) { + final var serializationWriter = + _serializationWriterFactory.getSerializationWriter(inputType); + assertNotNull(serializationWriter); + } + + @ParameterizedTest + @ValueSource( + strings = { + ";", + ";;", + "; charset=utf-8", + " ; charset=utf-8", + }) + public void throwsIllegalArgumentExceptionForDelimiterOnlyOrMissingMediaType( + final String inputType) { + assertThrows( + IllegalArgumentException.class, + () -> _serializationWriterFactory.getSerializationWriter(inputType)); + } }