Exemplo n.º 1
0
        public async Task XmlDataContractSerializerOutputFormatterWritesComplexTypes()
        {
            // Arrange
            var sampleInput = new TestLevelTwo
            {
                SampleString = "TestString",
                TestOne      = new TestLevelOne
                {
                    SampleInt    = 10,
                    sampleString = "TestLevelOne string"
                }
            };
            var formatter = new XmlDataContractSerializerOutputFormatter(
                XmlOutputFormatter.GetDefaultXmlWriterSettings());
            var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType());

            // Act
            await formatter.WriteAsync(outputFormatterContext);

            // Assert
            Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body);
            outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0;
            Assert.Equal("<TestLevelTwo xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                         "<SampleString>TestString</SampleString>" +
                         "<TestOne><SampleInt>10</SampleInt><sampleString>TestLevelOne string</sampleString>" +
                         "</TestOne></TestLevelTwo>",
                         new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8)
                         .ReadToEnd());
        }
Exemplo n.º 2
0
        public async Task XmlDataContractSerializerOutputFormatterWritesIndentedOutput()
        {
            // Arrange
            var sampleInput = new DummyClass {
                SampleInt = 10
            };
            var formatter = new XmlDataContractSerializerOutputFormatter(
                XmlOutputFormatter.GetDefaultXmlWriterSettings());

            formatter.WriterSettings.Indent = true;
            var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType());

            // Act
            await formatter.WriteAsync(outputFormatterContext);

            // Assert
            Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body);
            outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0;
            var outputString = new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body,
                                                Encoding.UTF8).ReadToEnd();

            Assert.Equal("<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                         "\r\n  <SampleInt>10</SampleInt>\r\n</DummyClass>",
                         outputString);
        }
Exemplo n.º 3
0
        public async Task XmlDataContractSerializerOutputFormatterWritesUTF16Output()
        {
            // Arrange
            var sampleInput = new DummyClass {
                SampleInt = 10
            };
            var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType(),
                                                                   "application/xml; charset=utf-16");
            var formatter = new XmlDataContractSerializerOutputFormatter(
                XmlOutputFormatter.GetDefaultXmlWriterSettings());

            formatter.WriterSettings.OmitXmlDeclaration = false;

            // Act
            await formatter.WriteAsync(outputFormatterContext);

            // Assert
            Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body);
            outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0;
            Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
                         "<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                         "<SampleInt>10</SampleInt></DummyClass>",
                         new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body,
                                          Encodings.UTF16EncodingLittleEndian).ReadToEnd());
        }
Exemplo n.º 4
0
        public void XmlDataContractSerializer_CanWriteResult_ReturnsTrue_ForWritableType()
        {
            // Arrange
            var formatter = new XmlDataContractSerializerOutputFormatter(
                XmlOutputFormatter.GetDefaultXmlWriterSettings());
            var outputFormatterContext = GetOutputFormatterContext(null, typeof(Dictionary <string, string>));

            // Act & Assert
            Assert.True(formatter.CanWriteResult(outputFormatterContext, MediaTypeHeaderValue.Parse("application/xml")));
        }
Exemplo n.º 5
0
        public async Task VerifyBodyIsNotClosedAfterOutputIsWritten()
        {
            // Arrange
            var sampleInput = new DummyClass {
                SampleInt = 10
            };
            var formatter = new XmlDataContractSerializerOutputFormatter(
                XmlOutputFormatter.GetDefaultXmlWriterSettings());
            var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType());

            // Act
            await formatter.WriteAsync(outputFormatterContext);

            // Assert
            Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body);
            Assert.True(outputFormatterContext.ActionContext.HttpContext.Response.Body.CanRead);
        }
Exemplo n.º 6
0
        public async Task XmlSerializerOutputFormatterWritesSimpleTypes()
        {
            // Arrange
            var sampleInput = new DummyClass {
                SampleInt = 10
            };
            var formatter = new XmlSerializerOutputFormatter(
                XmlOutputFormatter.GetDefaultXmlWriterSettings());
            var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType());

            // Act
            await formatter.WriteAsync(outputFormatterContext);

            // Assert
            Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body);
            outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0;
            Assert.Equal("<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><SampleInt>10</SampleInt></DummyClass>",
                         new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8)
                         .ReadToEnd());
            Assert.True(outputFormatterContext.ActionContext.HttpContext.Response.Body.CanRead);
        }