diff --git a/packages/serialization/form/kiota_serialization_form/form_parse_node_factory.py b/packages/serialization/form/kiota_serialization_form/form_parse_node_factory.py index 750a8937..2963b4aa 100644 --- a/packages/serialization/form/kiota_serialization_form/form_parse_node_factory.py +++ b/packages/serialization/form/kiota_serialization_form/form_parse_node_factory.py @@ -25,7 +25,7 @@ def get_root_parse_node(self, content_type: str, content: bytes) -> ParseNode: if not content_type: raise TypeError("Content Type cannot be null") valid_content_type = self.get_valid_content_type() - if valid_content_type.casefold() != content_type.casefold(): + if valid_content_type.casefold() != content_type.split(";")[0].strip().casefold(): raise TypeError(f"Expected {valid_content_type} as content type") if not content: diff --git a/packages/serialization/form/kiota_serialization_form/form_serialization_writer_factory.py b/packages/serialization/form/kiota_serialization_form/form_serialization_writer_factory.py index 3ae51fcf..dd4ded6e 100644 --- a/packages/serialization/form/kiota_serialization_form/form_serialization_writer_factory.py +++ b/packages/serialization/form/kiota_serialization_form/form_serialization_writer_factory.py @@ -24,7 +24,7 @@ def get_serialization_writer(self, content_type: str) -> SerializationWriter: if not content_type: raise TypeError("Content Type cannot be null") valid_content_type = self.get_valid_content_type() - if valid_content_type.casefold() != content_type.casefold(): + if valid_content_type.casefold() != content_type.split(";")[0].strip().casefold(): raise TypeError(f"Expected {valid_content_type} as content type") return FormSerializationWriter() diff --git a/packages/serialization/form/tests/unit/test_form_parse_node_factory.py b/packages/serialization/form/tests/unit/test_form_parse_node_factory.py index 2e05e104..f3e6f9b7 100644 --- a/packages/serialization/form/tests/unit/test_form_parse_node_factory.py +++ b/packages/serialization/form/tests/unit/test_form_parse_node_factory.py @@ -45,4 +45,39 @@ def test_get_root_parse_node_empty_form(): def test_get_valid_content_type(): factory = FormParseNodeFactory() content_type = factory.get_valid_content_type() - assert content_type == FORM_CONTENT_TYPE \ No newline at end of file + assert content_type == FORM_CONTENT_TYPE + + +@pytest.mark.parametrize( + "content_type", + [ + "application/x-www-form-urlencoded; charset=utf-8", + "application/x-www-form-urlencoded; charset=UTF-8", + "application/x-www-form-urlencoded ; charset=utf-8", + "APPLICATION/X-WWW-FORM-URLENCODED", + "APPLICATION/X-WWW-FORM-URLENCODED; CHARSET=UTF-8", + "Application/x-www-form-urlencoded; charset=utf-8", + ], +) +def test_get_root_parse_node_with_parameters_and_case_variants(sample_form_string, content_type): + factory = FormParseNodeFactory() + sample_form_string_bytes = sample_form_string.encode('utf-8') + root = factory.get_root_parse_node(content_type, sample_form_string_bytes) + assert isinstance(root, FormParseNode) + + +@pytest.mark.parametrize( + "content_type", + [ + ";", + ";;", + "; charset=utf-8", + " ; charset=utf-8", + ], +) +def test_get_root_parse_node_delimiter_only(sample_form_string, content_type): + with pytest.raises(TypeError) as e_info: + factory = FormParseNodeFactory() + sample_form_string_bytes = sample_form_string.encode('utf-8') + factory.get_root_parse_node(content_type, sample_form_string_bytes) + assert str(e_info.value) == f"Expected {FORM_CONTENT_TYPE} as content type" diff --git a/packages/serialization/form/tests/unit/test_form_serialization_writer_factory.py b/packages/serialization/form/tests/unit/test_form_serialization_writer_factory.py index 4ecf4126..a67d17a0 100644 --- a/packages/serialization/form/tests/unit/test_form_serialization_writer_factory.py +++ b/packages/serialization/form/tests/unit/test_form_serialization_writer_factory.py @@ -29,4 +29,37 @@ def test_get_serialization_writer_unsupported_content_type(): def test_get_valid_content_type(): factory = FormSerializationWriterFactory() content_type = factory.get_valid_content_type() - assert content_type == FORM_CONTENT_TYPE \ No newline at end of file + assert content_type == FORM_CONTENT_TYPE + + +@pytest.mark.parametrize( + "content_type", + [ + "application/x-www-form-urlencoded; charset=utf-8", + "application/x-www-form-urlencoded; charset=UTF-8", + "application/x-www-form-urlencoded ; charset=utf-8", + "APPLICATION/X-WWW-FORM-URLENCODED", + "APPLICATION/X-WWW-FORM-URLENCODED; CHARSET=UTF-8", + "Application/x-www-form-urlencoded; charset=utf-8", + ], +) +def test_get_serialization_writer_with_parameters_and_case_variants(content_type): + factory = FormSerializationWriterFactory() + writer = factory.get_serialization_writer(content_type) + assert isinstance(writer, FormSerializationWriter) + + +@pytest.mark.parametrize( + "content_type", + [ + ";", + ";;", + "; charset=utf-8", + " ; charset=utf-8", + ], +) +def test_get_serialization_writer_delimiter_only(content_type): + with pytest.raises(TypeError) as e_info: + factory = FormSerializationWriterFactory() + factory.get_serialization_writer(content_type) + assert str(e_info.value) == f"Expected {FORM_CONTENT_TYPE} as content type"