Esempio n. 1
0
        public async Task SuppliedWriterSettings_TakeAffect()
        {
            // Arrange
            var writerSettings = FormattingUtilities.GetDefaultXmlWriterSettings();

            writerSettings.OmitXmlDeclaration = false;
            var sampleInput = new DummyClass {
                SampleInt = 10
            };
            var formatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType());
            var formatter        = new XmlDataContractSerializerOutputFormatter(writerSettings);
            var expectedOutput   = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                   "<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                                   "<SampleInt>10</SampleInt></DummyClass>";

            // Act
            await formatter.WriteAsync(formatterContext);

            // Assert
            Assert.Same(writerSettings, formatter.WriterSettings);
            var responseStream = formatterContext.ActionContext.HttpContext.Response.Body;

            Assert.NotNull(responseStream);
            responseStream.Position = 0;
            var actualOutput = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();

            XmlAssert.Equal(expectedOutput, actualOutput);
        }
Esempio n. 2
0
        public async Task SuppliedWriterSettings_TakeAffect()
        {
            // Arrange
            var writerSettings = FormattingUtilities.GetDefaultXmlWriterSettings();

            writerSettings.OmitXmlDeclaration = false;
            var sampleInput = new DummyClass {
                SampleInt = 10
            };
            var formatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType());
            var formatter        = new XmlSerializerOutputFormatter(writerSettings);
            var expectedOutput   = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                   "<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                                   "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>";

            // Act
            await formatter.WriteAsync(formatterContext);

            // Assert
            var body = formatterContext.HttpContext.Response.Body;

            body.Position = 0;

            var content = new StreamReader(body).ReadToEnd();

            XmlAssert.Equal(expectedOutput, content);
        }
 /// <summary>
 /// Initializes a new instance of <see cref="XmlDataContractSerializerOutputFormatter"/>
 /// with default XmlWriterSettings
 /// </summary>
 public XmlDataContractSerializerOutputFormatter() :
     this(FormattingUtilities.GetDefaultXmlWriterSettings())
 {
 }