public async Task WriteAsync_ThrowsWhenNotConfiguredWithKnownTypes() { // Arrange var sampleInput = new SomeDummyClass { SampleInt = 1, SampleString = "TestString" }; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, typeof(DummyClass)); // Act & Assert await Assert.ThrowsAsync(typeof(SerializationException), async() => await formatter.WriteAsync(outputFormatterContext)); }
public async Task WriteAsync_WritesWhenConfiguredWithPreserveReferences() { // Arrange var sampleId = 1; var sampleName = "Parent"; var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"; var SerializationNamespace = "http://schemas.microsoft.com/2003/10/Serialization/"; var expectedOutput = string.Format( "<Parent xmlns:i=\"{0}\" z:Id=\"{2}\" xmlns:z=\"{1}\">" + "<Children z:Id=\"2\" z:Size=\"1\">" + "<Child z:Id=\"3\"><Id>{2}</Id><Parent z:Ref=\"1\" i:nil=\"true\" />" + "</Child></Children><Name z:Id=\"4\">{3}</Name></Parent>", InstanceNamespace, SerializationNamespace, sampleId, sampleName); var child = new Child { Id = sampleId }; var parent = new Parent { Name = sampleName, Children = new List <Child> { child } }; child.Parent = parent; var settings = new DataContractSerializerSettings { PreserveObjectReferences = true }; var formatter = new XmlDataContractSerializerOutputFormatter { SerializerSettings = settings }; var outputFormatterContext = GetOutputFormatterContext(parent, parent.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_DoesntFlushOutputStream() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var response = outputFormatterContext.HttpContext.Response; response.Body = FlushReportingStream.GetThrowingStream(); // Act & Assert await formatter.WriteAsync(outputFormatterContext); }
public async Task WriteAsync_VerifyBodyIsNotClosedAfterOutputIsWritten() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; Assert.True(body.CanRead); }
public async Task WriteAsync_CanWriteBasicTypes(object input, string expectedOutput) { // Arrange var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(input, input.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_WritesWhenConfiguredWithKnownTypes() { // Arrange var sampleInt = 10; var sampleString = "TestString"; var KnownTypeName = "SomeDummyClass"; var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"; var expectedOutput = string.Format( "<DummyClass xmlns:i=\"{1}\" xmlns=\"\" i:type=\"{0}\"><SampleInt>{2}</SampleInt>" + "<SampleString>{3}</SampleString></DummyClass>", KnownTypeName, InstanceNamespace, sampleInt, sampleString); var sampleInput = new SomeDummyClass { SampleInt = sampleInt, SampleString = sampleString }; var settings = new DataContractSerializerSettings { KnownTypes = new[] { typeof(SomeDummyClass) } }; var formatter = new XmlDataContractSerializerOutputFormatter { SerializerSettings = settings }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, typeof(DummyClass)); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_WritesWhenConfiguredWithRootName() { // Arrange var sampleInt = 10; var SubstituteRootName = "SomeOtherClass"; var SubstituteRootNamespace = "http://tempuri.org"; var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"; var expectedOutput = string.Format( "<{0} xmlns:i=\"{2}\" xmlns=\"{1}\"><SampleInt xmlns=\"\">{3}</SampleInt></{0}>", SubstituteRootName, SubstituteRootNamespace, InstanceNamespace, sampleInt); var sampleInput = new DummyClass { SampleInt = sampleInt }; var dictionary = new XmlDictionary(); var settings = new DataContractSerializerSettings { RootName = dictionary.Add(SubstituteRootName), RootNamespace = dictionary.Add(SubstituteRootNamespace) }; var formatter = new XmlDataContractSerializerOutputFormatter { SerializerSettings = settings }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_ThrowsWhenNotConfiguredWithPreserveReferences() { // Arrange var child = new Child { Id = 1 }; var parent = new Parent { Name = "Parent", Children = new List <Child> { child } }; child.Parent = parent; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(parent, parent.GetType()); // Act & Assert await Assert.ThrowsAsync(typeof(SerializationException), async() => await formatter.WriteAsync(outputFormatterContext)); }
public async Task WriteAsync_SerializesObjectWhenDeclaredTypeIsDifferentFromActualType() { // Arrange var expected = @"<DummyClass xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns="""" " + @"i:type=""SomeDummyClass""><SampleInt>1</SampleInt><SampleString>Test</SampleString></DummyClass>"; var sampleInput = new SomeDummyClass { SampleInt = 1, SampleString = "Test" }; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, typeof(DummyClass)); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var actual = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expected, actual); }
public async Task WriteAsync_DoesntFlushOutputStream() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var response = outputFormatterContext.HttpContext.Response; response.Body = FlushReportingStream.GetThrowingStream(); // Act & Assert await formatter.WriteAsync(outputFormatterContext); }
public async Task WriteAsync_VerifyBodyIsNotClosedAfterOutputIsWritten() { // Arrange var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; Assert.True(body.CanRead); }
public async Task WriteAsync_WritesIndentedOutput() { // Arrange var expectedOutput = "<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" + "\r\n <SampleInt>10</SampleInt>\r\n</DummyClass>"; var sampleInput = new DummyClass { SampleInt = 10 }; var formatter = new XmlDataContractSerializerOutputFormatter(); formatter.WriterSettings.Indent = true; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_WritesUTF16Output() { // Arrange var expectedOutput = "<?xml version=\"1.0\" encoding=\"utf-16\"?>" + "<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SampleInt>10</SampleInt></DummyClass>"; var sampleInput = new DummyClass { SampleInt = 10 }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType(), "application/xml; charset=utf-16"); var formatter = new XmlDataContractSerializerOutputFormatter(); formatter.WriterSettings.OmitXmlDeclaration = false; // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader( body, new UnicodeEncoding(bigEndian: false, byteOrderMark: false, throwOnInvalidBytes: true)).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_WritesOnModifiedWriterSettings() { // Arrange var expectedOutput = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SampleInt>10</SampleInt></DummyClass>"; var sampleInput = new DummyClass { SampleInt = 10 }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); var formatter = new XmlDataContractSerializerOutputFormatter( new XmlWriterSettings { OmitXmlDeclaration = false, CloseOutput = false }); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_CanWriteBasicTypes(object input, string expectedOutput) { // Arrange var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(input, input.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_WritesWhenConfiguredWithPreserveReferences() { // Arrange var sampleId = 1; var sampleName = "Parent"; var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"; var SerializationNamespace = "http://schemas.microsoft.com/2003/10/Serialization/"; var expectedOutput = string.Format( "<Parent xmlns:i=\"{0}\" z:Id=\"{2}\" xmlns:z=\"{1}\">" + "<Children z:Id=\"2\" z:Size=\"1\">" + "<Child z:Id=\"3\"><Id>{2}</Id><Parent z:Ref=\"1\" i:nil=\"true\" />" + "</Child></Children><Name z:Id=\"4\">{3}</Name></Parent>", InstanceNamespace, SerializationNamespace, sampleId, sampleName); var child = new Child { Id = sampleId }; var parent = new Parent { Name = sampleName, Children = new List<Child> { child } }; child.Parent = parent; var settings = new DataContractSerializerSettings { PreserveObjectReferences = true }; var formatter = new XmlDataContractSerializerOutputFormatter { SerializerSettings = settings }; var outputFormatterContext = GetOutputFormatterContext(parent, parent.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_WritesWhenConfiguredWithRootName() { // Arrange var sampleInt = 10; var SubstituteRootName = "SomeOtherClass"; var SubstituteRootNamespace = "http://tempuri.org"; var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"; var expectedOutput = string.Format( "<{0} xmlns:i=\"{2}\" xmlns=\"{1}\"><SampleInt xmlns=\"\">{3}</SampleInt></{0}>", SubstituteRootName, SubstituteRootNamespace, InstanceNamespace, sampleInt); var sampleInput = new DummyClass { SampleInt = sampleInt }; var dictionary = new XmlDictionary(); var settings = new DataContractSerializerSettings { RootName = dictionary.Add(SubstituteRootName), RootNamespace = dictionary.Add(SubstituteRootNamespace) }; var formatter = new XmlDataContractSerializerOutputFormatter { SerializerSettings = settings }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_ThrowsWhenNotConfiguredWithKnownTypes() { // Arrange var sampleInput = new SomeDummyClass { SampleInt = 1, SampleString = "TestString" }; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, typeof(DummyClass)); // Act & Assert await Assert.ThrowsAsync(typeof(SerializationException), async () => await formatter.WriteAsync(outputFormatterContext)); }
public async Task WriteAsync_ThrowsWhenNotConfiguredWithPreserveReferences() { // Arrange var child = new Child { Id = 1 }; var parent = new Parent { Name = "Parent", Children = new List<Child> { child } }; child.Parent = parent; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(parent, parent.GetType()); // Act & Assert await Assert.ThrowsAsync(typeof(SerializationException), async () => await formatter.WriteAsync(outputFormatterContext)); }
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 body = formatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_WritesWhenConfiguredWithKnownTypes() { // Arrange var sampleInt = 10; var sampleString = "TestString"; var KnownTypeName = "SomeDummyClass"; var InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance"; var expectedOutput = string.Format( "<DummyClass xmlns:i=\"{1}\" xmlns=\"\" i:type=\"{0}\"><SampleInt>{2}</SampleInt>" + "<SampleString>{3}</SampleString></DummyClass>", KnownTypeName, InstanceNamespace, sampleInt, sampleString); var sampleInput = new SomeDummyClass { SampleInt = sampleInt, SampleString = sampleString }; var settings = new DataContractSerializerSettings { KnownTypes = new[] { typeof(SomeDummyClass) } }; var formatter = new XmlDataContractSerializerOutputFormatter { SerializerSettings = settings }; var outputFormatterContext = GetOutputFormatterContext(sampleInput, typeof(DummyClass)); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_WritesComplexTypes() { // Arrange var expectedOutput = "<TestLevelTwo xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<SampleString>TestString</SampleString>" + "<TestOne><SampleInt>10</SampleInt><sampleString>TestLevelOne string</sampleString>" + "</TestOne></TestLevelTwo>"; var sampleInput = new TestLevelTwo { SampleString = "TestString", TestOne = new TestLevelOne { SampleInt = 10, sampleString = "TestLevelOne string" } }; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType()); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_CanWriteBasicTypes(object input, string expectedOutput) { // ConditionalThoery seems to throw when used with Member data. Hence using if case here. if (TestPlatformHelper.IsMono) { // Mono issue - https://github.com/aspnet/External/issues/18 return; } // Arrange var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(input, typeof(object)); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var content = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expectedOutput, content); }
public async Task WriteAsync_SerializesObjectWhenDeclaredTypeIsDifferentFromActualType() { // Arrange var expected = @"<DummyClass xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns="""" " + @"i:type=""SomeDummyClass""><SampleInt>1</SampleInt><SampleString>Test</SampleString></DummyClass>"; var sampleInput = new SomeDummyClass { SampleInt = 1, SampleString = "Test" }; var formatter = new XmlDataContractSerializerOutputFormatter(); var outputFormatterContext = GetOutputFormatterContext(sampleInput, typeof(DummyClass)); // Act await formatter.WriteAsync(outputFormatterContext); // Assert var body = outputFormatterContext.HttpContext.Response.Body; body.Position = 0; var actual = new StreamReader(body).ReadToEnd(); XmlAssert.Equal(expected, actual); }