XML Decoder: Checking for invalid content outside of a root node #1448

This commit is contained in:
Mike Farah
2022-11-27 17:46:50 +11:00
parent 02be2b2918
commit 68f47c02c8
5 changed files with 33 additions and 9 deletions
+14 -1
View File
@@ -235,9 +235,15 @@ const expectedXmlWithProcInstAndDirectives = `<?xml version="1.0"?>
var xmlScenarios = []formatScenario{
{
skipDoc: true,
input: "<root>value<!-- comment--> </root>",
input: " <root>value<!-- comment--> </root>",
expected: "root: value # comment\n",
},
{
skipDoc: true,
input: "value<root>value</root>",
expectedError: "bad file 'sample.yml': invalid XML: Encountered chardata [value] outside of XML node",
scenarioType: "decode-error",
},
{
skipDoc: true,
input: "<root> <!-- comment-->value</root>",
@@ -502,6 +508,13 @@ func testXMLScenario(t *testing.T, s formatScenario) {
prefs := NewDefaultXmlPreferences()
prefs.SkipDirectives = true
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewXMLDecoder(prefs), NewXMLEncoder(2, prefs)), s.description)
case "decode-error":
result, err := processFormatScenario(s, NewXMLDecoder(NewDefaultXmlPreferences()), NewYamlEncoder(2, false, ConfiguredYamlPreferences))
if err == nil {
t.Errorf("Expected error '%v' but it worked: %v", s.expectedError, result)
} else {
test.AssertResultComplexWithContext(t, s.expectedError, err.Error(), s.description)
}
case "encode-error":
result, err := processFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewXMLEncoder(2, NewDefaultXmlPreferences()))
if err == nil {