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()); }
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); }
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()); }
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"))); }
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); }
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); }