public void WriteXml_WritesValidXml()
        {
            // Arrange
            var problemDetails = new ValidationProblemDetails
            {
                Title      = "Some title",
                Detail     = "Some detail",
                Extensions =
                {
                    ["key1"]   = "Test Value 1",
                    ["[Key2]"] = "Test Value 2"
                },
                Errors =
                {
                    { "error1",   new[] { "Test error 1", "Test error 2" } },
                    { "[error2]", new[] { "Test error 3" } },
                    { "",         new[] { "Test error 4" } },
                }
            };

            var wrapper         = new ValidationProblemDetailsWrapper(problemDetails);
            var outputStream    = new MemoryStream();
            var expectedContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                  "<problem xmlns=\"urn:ietf:rfc:7807\">" +
                                  "<detail>Some detail</detail>" +
                                  "<title>Some title</title>" +
                                  "<key1>Test Value 1</key1>" +
                                  "<_x005B_Key2_x005D_>Test Value 2</_x005B_Key2_x005D_>" +
                                  "<MVC-Errors>" +
                                  "<error1>Test error 1 Test error 2</error1>" +
                                  "<_x005B_error2_x005D_>Test error 3</_x005B_error2_x005D_>" +
                                  "<MVC-Empty>Test error 4</MVC-Empty>" +
                                  "</MVC-Errors>" +
                                  "</problem>";

            // Act
            using (var xmlWriter = XmlWriter.Create(outputStream))
            {
                var dataContractSerializer = new DataContractSerializer(wrapper.GetType());
                dataContractSerializer.WriteObject(xmlWriter, wrapper);
            }
            outputStream.Position = 0;
            var res = new StreamReader(outputStream, Encoding.UTF8).ReadToEnd();

            // Assert
            Assert.Equal(expectedContent, res);
        }
Ejemplo n.º 2
0
        public void WriteXml_WithNoValidationErrors()
        {
            // Arrange
            var problemDetails = new ValidationProblemDetails
            {
                Title      = "Some title",
                Detail     = "Some detail",
                Extensions =
                {
                    ["key1"]   = "Test Value 1",
                    ["[Key2]"] = "Test Value 2"
                },
            };

            var wrapper         = new ValidationProblemDetailsWrapper(problemDetails);
            var outputStream    = new MemoryStream();
            var expectedContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                  "<ValidationProblemDetails>" +
                                  "<Detail>Some detail</Detail>" +
                                  "<Title>Some title</Title>" +
                                  "<key1>Test Value 1</key1>" +
                                  "<_x005B_Key2_x005D_>Test Value 2</_x005B_Key2_x005D_>" +
                                  "</ValidationProblemDetails>";

            // Act
            using (var xmlWriter = XmlWriter.Create(outputStream))
            {
                var dataContractSerializer = new DataContractSerializer(wrapper.GetType());
                dataContractSerializer.WriteObject(xmlWriter, wrapper);
            }
            outputStream.Position = 0;
            var res = new StreamReader(outputStream, Encoding.UTF8).ReadToEnd();

            // Assert
            Assert.Equal(expectedContent, res);
        }