Removed misplaced test files due to bad merge.
The latest version of these files are already present in Microsoft.AspNet.Mvc.Xml.Test.
This commit is contained in:
parent
5e704cd5ef
commit
8c32d9e207
|
|
@ -1,499 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
#if ASPNET50
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class XmlDataContractSerializerInputFormatterTest
|
||||
{
|
||||
[DataContract(Name = "DummyClass", Namespace = "")]
|
||||
public class DummyClass
|
||||
{
|
||||
[DataMember]
|
||||
public int SampleInt { get; set; }
|
||||
}
|
||||
|
||||
[DataContract(Name = "SomeDummyClass", Namespace = "")]
|
||||
public class SomeDummyClass : DummyClass
|
||||
{
|
||||
[DataMember]
|
||||
public string SampleString { get; set; }
|
||||
}
|
||||
|
||||
[DataContract(Name = "TestLevelOne", Namespace = "")]
|
||||
public class TestLevelOne
|
||||
{
|
||||
[DataMember]
|
||||
public int SampleInt { get; set; }
|
||||
[DataMember]
|
||||
public string sampleString;
|
||||
public DateTime SampleDate { get; set; }
|
||||
}
|
||||
|
||||
[DataContract(Name = "TestLevelTwo", Namespace = "")]
|
||||
public class TestLevelTwo
|
||||
{
|
||||
[DataMember]
|
||||
public string SampleString { get; set; }
|
||||
[DataMember]
|
||||
public TestLevelOne TestOne { get; set; }
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("application/xml", true)]
|
||||
[InlineData("application/*", true)]
|
||||
[InlineData("*/*", true)]
|
||||
[InlineData("text/xml", true)]
|
||||
[InlineData("text/*", true)]
|
||||
[InlineData("text/json", false)]
|
||||
[InlineData("application/json", false)]
|
||||
[InlineData("", false)]
|
||||
[InlineData(null, false)]
|
||||
[InlineData("invalid", false)]
|
||||
public void CanRead_ReturnsTrueForAnySupportedContentType(string requestContentType, bool expectedCanRead)
|
||||
{
|
||||
// Arrange
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
var contentBytes = Encoding.UTF8.GetBytes("content");
|
||||
|
||||
var actionContext = GetActionContext(contentBytes, contentType: requestContentType);
|
||||
var formatterContext = new InputFormatterContext(actionContext, typeof(string));
|
||||
|
||||
// Act
|
||||
var result = formatter.CanRead(formatterContext);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedCanRead, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void XmlDataContractSerializerFormatterHasProperSuppportedMediaTypes()
|
||||
{
|
||||
// Arrange & Act
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
|
||||
// Assert
|
||||
Assert.True(formatter.SupportedMediaTypes
|
||||
.Select(content => content.ToString())
|
||||
.Contains("application/xml"));
|
||||
Assert.True(formatter.SupportedMediaTypes
|
||||
.Select(content => content.ToString())
|
||||
.Contains("text/xml"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void XmlDataContractSerializerFormatterHasProperSuppportedEncodings()
|
||||
{
|
||||
// Arrange & Act
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
|
||||
// Assert
|
||||
Assert.True(formatter.SupportedEncodings.Any(i => i.WebName == "utf-8"));
|
||||
Assert.True(formatter.SupportedEncodings.Any(i => i.WebName == "utf-16"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterReadsSimpleTypes()
|
||||
{
|
||||
// Arrange
|
||||
var expectedInt = 10;
|
||||
var expectedString = "TestString";
|
||||
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<TestLevelOne><SampleInt>" + expectedInt + "</SampleInt>" +
|
||||
"<sampleString>" + expectedString + "</sampleString></TestLevelOne>";
|
||||
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelOne));
|
||||
|
||||
// Act
|
||||
var model = await formatter.ReadAsync(context);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(model);
|
||||
Assert.IsType<TestLevelOne>(model);
|
||||
|
||||
var levelOneModel = model as TestLevelOne;
|
||||
Assert.Equal(expectedInt, levelOneModel.SampleInt);
|
||||
Assert.Equal(expectedString, levelOneModel.sampleString);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterReadsComplexTypes()
|
||||
{
|
||||
// Arrange
|
||||
var expectedInt = 10;
|
||||
var expectedString = "TestString";
|
||||
var expectedLevelTwoString = "102";
|
||||
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<TestLevelTwo><SampleString>" + expectedLevelTwoString + "</SampleString>" +
|
||||
"<TestOne><SampleInt>" + expectedInt + "</SampleInt>" +
|
||||
"<sampleString>" + expectedString + "</sampleString></TestOne></TestLevelTwo>";
|
||||
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo));
|
||||
|
||||
// Act
|
||||
var model = await formatter.ReadAsync(context);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(model);
|
||||
Assert.IsType<TestLevelTwo>(model);
|
||||
|
||||
var levelTwoModel = model as TestLevelTwo;
|
||||
Assert.Equal(expectedLevelTwoString, levelTwoModel.SampleString);
|
||||
Assert.Equal(expectedInt, levelTwoModel.TestOne.SampleInt);
|
||||
Assert.Equal(expectedString, levelTwoModel.TestOne.sampleString);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterReadsWhenMaxDepthIsModified()
|
||||
{
|
||||
// Arrange
|
||||
var expectedInt = 10;
|
||||
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<DummyClass><SampleInt>" + expectedInt + "</SampleInt></DummyClass>";
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
formatter.MaxDepth = 10;
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(DummyClass));
|
||||
|
||||
|
||||
// Act
|
||||
var model = await formatter.ReadAsync(context);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(model);
|
||||
Assert.IsType<DummyClass>(model);
|
||||
var dummyModel = model as DummyClass;
|
||||
Assert.Equal(expectedInt, dummyModel.SampleInt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterThrowsOnExceededMaxDepth()
|
||||
{
|
||||
if (TestPlatformHelper.IsMono)
|
||||
{
|
||||
// ReaderQuotas are not honored on Mono
|
||||
return;
|
||||
}
|
||||
|
||||
// Arrange
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<TestLevelTwo><SampleString>test</SampleString>" +
|
||||
"<TestOne><SampleInt>10</SampleInt>" +
|
||||
"<sampleString>test</sampleString></TestOne></TestLevelTwo>";
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
formatter.MaxDepth = 1;
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo));
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync(typeof(SerializationException), async () => await formatter.ReadAsync(context));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterThrowsWhenReaderQuotasAreChanged()
|
||||
{
|
||||
if (TestPlatformHelper.IsMono)
|
||||
{
|
||||
// ReaderQuotas are not honored on Mono
|
||||
return;
|
||||
}
|
||||
|
||||
// Arrange
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<TestLevelTwo><SampleString>test</SampleString>" +
|
||||
"<TestOne><SampleInt>10</SampleInt>" +
|
||||
"<sampleString>test</sampleString></TestOne></TestLevelTwo>";
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
formatter.XmlDictionaryReaderQuotas.MaxStringContentLength = 2;
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo));
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync(typeof(SerializationException), async () => await formatter.ReadAsync(context));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void XmlDataContractSerializerFormatterThrowsWhenMaxDepthIsBelowOne()
|
||||
{
|
||||
// Arrange
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws(typeof(ArgumentException), () => formatter.MaxDepth = 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task VerifyStreamIsOpenAfterRead()
|
||||
{
|
||||
// Arrange
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<DummyClass><SampleInt>10</SampleInt></DummyClass>";
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(DummyClass));
|
||||
|
||||
// Act
|
||||
var model = await formatter.ReadAsync(context);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(model);
|
||||
Assert.True(context.ActionContext.HttpContext.Request.Body.CanRead);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterThrowsOnInvalidCharacters()
|
||||
{
|
||||
// Arrange
|
||||
var expectedException = TestPlatformHelper.IsMono ? typeof(SerializationException) :
|
||||
typeof(XmlException);
|
||||
var expectedMessage = TestPlatformHelper.IsMono ?
|
||||
"Expected element 'TestLevelTwo' in namespace '', but found Element node 'DummyClass' in namespace ''" :
|
||||
"The encoding in the declaration 'UTF-8' does not match the encoding of the document 'utf-16LE'.";
|
||||
var inpStart = Encodings.UTF16EncodingLittleEndian.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<DummyClass><SampleInt>");
|
||||
byte[] inp = { 192, 193 };
|
||||
var inpEnd = Encodings.UTF16EncodingLittleEndian.GetBytes("</SampleInt></DummyClass>");
|
||||
|
||||
var contentBytes = new byte[inpStart.Length + inp.Length + inpEnd.Length];
|
||||
Buffer.BlockCopy(inpStart, 0, contentBytes, 0, inpStart.Length);
|
||||
Buffer.BlockCopy(inp, 0, contentBytes, inpStart.Length, inp.Length);
|
||||
Buffer.BlockCopy(inpEnd, 0, contentBytes, inpStart.Length + inp.Length, inpEnd.Length);
|
||||
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo));
|
||||
|
||||
// Act
|
||||
var ex = await Assert.ThrowsAsync(expectedException, () => formatter.ReadAsync(context));
|
||||
Assert.Equal(expectedMessage, ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterIgnoresBOMCharacters()
|
||||
{
|
||||
// Arrange
|
||||
var sampleString = "Test";
|
||||
var sampleStringBytes = Encoding.UTF8.GetBytes(sampleString);
|
||||
var inputStart = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + Environment.NewLine +
|
||||
"<TestLevelTwo><SampleString>" + sampleString);
|
||||
byte[] bom = { 0xef, 0xbb, 0xbf };
|
||||
var inputEnd = Encoding.UTF8.GetBytes("</SampleString></TestLevelTwo>");
|
||||
var expectedBytes = new byte[sampleString.Length + bom.Length];
|
||||
|
||||
var contentBytes = new byte[inputStart.Length + bom.Length + inputEnd.Length];
|
||||
Buffer.BlockCopy(inputStart, 0, contentBytes, 0, inputStart.Length);
|
||||
Buffer.BlockCopy(bom, 0, contentBytes, inputStart.Length, bom.Length);
|
||||
Buffer.BlockCopy(inputEnd, 0, contentBytes, inputStart.Length + bom.Length, inputEnd.Length);
|
||||
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo));
|
||||
|
||||
// Act
|
||||
var model = await formatter.ReadAsync(context);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(model);
|
||||
var levelTwoModel = model as TestLevelTwo;
|
||||
Buffer.BlockCopy(sampleStringBytes, 0, expectedBytes, 0, sampleStringBytes.Length);
|
||||
Buffer.BlockCopy(bom, 0, expectedBytes, sampleStringBytes.Length, bom.Length);
|
||||
Assert.Equal(expectedBytes, Encoding.UTF8.GetBytes(levelTwoModel.SampleString));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerAcceptsUTF16Characters()
|
||||
{
|
||||
// Arrange
|
||||
var expectedInt = 10;
|
||||
var expectedString = "TestString";
|
||||
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>" +
|
||||
"<TestLevelOne><SampleInt>" + expectedInt + "</SampleInt>" +
|
||||
"<sampleString>" + expectedString + "</sampleString></TestLevelOne>";
|
||||
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
var contentBytes = Encodings.UTF16EncodingLittleEndian.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelOne));
|
||||
|
||||
// Act
|
||||
var model = await formatter.ReadAsync(context);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(model);
|
||||
Assert.IsType<TestLevelOne>(model);
|
||||
|
||||
var levelOneModel = model as TestLevelOne;
|
||||
Assert.Equal(expectedInt, levelOneModel.SampleInt);
|
||||
Assert.Equal(expectedString, levelOneModel.sampleString);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterThrowsWhenNotConfiguredWithRootName()
|
||||
{
|
||||
// TODO: Test on Mono platform
|
||||
|
||||
// Arrange
|
||||
const string SubstituteRootName = "SomeOtherClass";
|
||||
const string SubstituteRootNamespace = "http://tempuri.org";
|
||||
|
||||
var input = string.Format(
|
||||
"<{0} xmlns=\"{1}\"><SampleInt xmlns=\"\">1</SampleInt></{0}>",
|
||||
SubstituteRootName,
|
||||
SubstituteRootNamespace);
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(DummyClass));
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync(typeof(SerializationException), async () => await formatter.ReadAsync(context));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterReadsWhenConfiguredWithRootName()
|
||||
{
|
||||
// Arrange
|
||||
var expectedInt = 10;
|
||||
const string SubstituteRootName = "SomeOtherClass";
|
||||
const string SubstituteRootNamespace = "http://tempuri.org";
|
||||
|
||||
var input = string.Format(
|
||||
"<{0} xmlns=\"{1}\"><SampleInt xmlns=\"\">{2}</SampleInt></{0}>",
|
||||
SubstituteRootName,
|
||||
SubstituteRootNamespace,
|
||||
expectedInt);
|
||||
|
||||
var dictionary = new XmlDictionary();
|
||||
var settings = new DataContractSerializerSettings
|
||||
{
|
||||
RootName = dictionary.Add(SubstituteRootName),
|
||||
RootNamespace = dictionary.Add(SubstituteRootNamespace)
|
||||
};
|
||||
var formatter = new XmlDataContractSerializerInputFormatter
|
||||
{
|
||||
SerializerSettings = settings
|
||||
};
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(DummyClass));
|
||||
|
||||
// Act
|
||||
var model = await formatter.ReadAsync(context);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(model);
|
||||
Assert.IsType<DummyClass>(model);
|
||||
|
||||
var dummyModel = model as DummyClass;
|
||||
Assert.Equal(expectedInt, dummyModel.SampleInt);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterThrowsWhenNotConfiguredWithKnownTypes()
|
||||
{
|
||||
// TODO: Test on Mono platform
|
||||
|
||||
// Arrange
|
||||
const string KnownTypeName = "SomeDummyClass";
|
||||
const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
|
||||
|
||||
var input = string.Format(
|
||||
"<DummyClass i:type=\"{0}\" xmlns:i=\"{1}\"><SampleInt>1</SampleInt>"
|
||||
+ "<SampleString>TestString</SampleString></DummyClass>",
|
||||
KnownTypeName,
|
||||
InstanceNamespace);
|
||||
|
||||
var formatter = new XmlDataContractSerializerInputFormatter();
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(DummyClass));
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync(typeof(SerializationException), async () => await formatter.ReadAsync(context));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterReadsWhenConfiguredWithKnownTypes()
|
||||
{
|
||||
// Arrange
|
||||
var expectedInt = 10;
|
||||
var expectedString = "TestString";
|
||||
const string KnownTypeName = "SomeDummyClass";
|
||||
const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
|
||||
|
||||
var input = string.Format(
|
||||
"<DummyClass i:type=\"{0}\" xmlns:i=\"{1}\"><SampleInt>{2}</SampleInt>"
|
||||
+ "<SampleString>{3}</SampleString></DummyClass>",
|
||||
KnownTypeName,
|
||||
InstanceNamespace,
|
||||
expectedInt,
|
||||
expectedString);
|
||||
var settings = new DataContractSerializerSettings
|
||||
{
|
||||
KnownTypes = new[] { typeof(SomeDummyClass) }
|
||||
};
|
||||
var formatter = new XmlDataContractSerializerInputFormatter
|
||||
{
|
||||
SerializerSettings = settings
|
||||
};
|
||||
var contentBytes = Encoding.UTF8.GetBytes(input);
|
||||
var context = GetInputFormatterContext(contentBytes, typeof(DummyClass));
|
||||
|
||||
// Act
|
||||
var model = await formatter.ReadAsync(context);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(model);
|
||||
Assert.IsType<SomeDummyClass>(model);
|
||||
|
||||
var dummyModel = model as SomeDummyClass;
|
||||
Assert.Equal(expectedInt, dummyModel.SampleInt);
|
||||
Assert.Equal(expectedString, dummyModel.SampleString);
|
||||
}
|
||||
|
||||
private InputFormatterContext GetInputFormatterContext(byte[] contentBytes, Type modelType)
|
||||
{
|
||||
var actionContext = GetActionContext(contentBytes);
|
||||
var metadata = new EmptyModelMetadataProvider().GetMetadataForType(null, modelType);
|
||||
return new InputFormatterContext(actionContext, metadata.ModelType);
|
||||
}
|
||||
|
||||
private static ActionContext GetActionContext(byte[] contentBytes,
|
||||
string contentType = "application/xml")
|
||||
{
|
||||
return new ActionContext(GetHttpContext(contentBytes, contentType),
|
||||
new AspNet.Routing.RouteData(),
|
||||
new ActionDescriptor());
|
||||
}
|
||||
private static HttpContext GetHttpContext(byte[] contentBytes,
|
||||
string contentType = "application/xml")
|
||||
{
|
||||
var request = new Mock<HttpRequest>();
|
||||
var headers = new Mock<IHeaderDictionary>();
|
||||
request.SetupGet(r => r.Headers).Returns(headers.Object);
|
||||
request.SetupGet(f => f.Body).Returns(new MemoryStream(contentBytes));
|
||||
request.SetupGet(f => f.ContentType).Returns(contentType);
|
||||
|
||||
var httpContext = new Mock<HttpContext>();
|
||||
httpContext.SetupGet(c => c.Request).Returns(request.Object);
|
||||
httpContext.SetupGet(c => c.Request).Returns(request.Object);
|
||||
return httpContext.Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,555 +0,0 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Core.Collections;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using System.Xml;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Core
|
||||
{
|
||||
public class XmlDataContractSerializerOutputFormatterTests
|
||||
{
|
||||
[DataContract(Name = "DummyClass", Namespace = "")]
|
||||
public class DummyClass
|
||||
{
|
||||
[DataMember]
|
||||
public int SampleInt { get; set; }
|
||||
}
|
||||
|
||||
[DataContract(Name = "SomeDummyClass", Namespace = "")]
|
||||
public class SomeDummyClass : DummyClass
|
||||
{
|
||||
[DataMember]
|
||||
public string SampleString { get; set; }
|
||||
}
|
||||
|
||||
[DataContract(Name = "TestLevelOne", Namespace = "")]
|
||||
public class TestLevelOne
|
||||
{
|
||||
[DataMember]
|
||||
public int SampleInt { get; set; }
|
||||
[DataMember]
|
||||
public string sampleString;
|
||||
}
|
||||
|
||||
[DataContract(Name = "TestLevelTwo", Namespace = "")]
|
||||
public class TestLevelTwo
|
||||
{
|
||||
[DataMember]
|
||||
public string SampleString { get; set; }
|
||||
[DataMember]
|
||||
public TestLevelOne TestOne { get; set; }
|
||||
}
|
||||
|
||||
[DataContract(Name = "Child", Namespace = "")]
|
||||
public class Child
|
||||
{
|
||||
[DataMember]
|
||||
public int Id { get; set; }
|
||||
[DataMember]
|
||||
public Parent Parent { get; set; }
|
||||
}
|
||||
|
||||
[DataContract(Name = "Parent", Namespace = "")]
|
||||
public class Parent
|
||||
{
|
||||
[DataMember]
|
||||
public string Name { get; set; }
|
||||
[DataMember]
|
||||
public List<Child> Children { get; set; }
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> BasicTypeValues
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new object[] { "sampleString",
|
||||
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">sampleString</string>" };
|
||||
yield return new object[] { 5,
|
||||
"<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">5</int>" };
|
||||
yield return new object[] { 5.43,
|
||||
"<double xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">5.43</double>" };
|
||||
yield return new object[] { 'a',
|
||||
"<char xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">97</char>" };
|
||||
yield return new object[] { new DummyClass { SampleInt = 10 },
|
||||
"<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
|
||||
"<SampleInt>10</SampleInt></DummyClass>" };
|
||||
yield return new object[] { new Dictionary<string, string>() { { "Hello", "World" } },
|
||||
"<ArrayOfKeyValueOfstringstring xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" " +
|
||||
"xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"><KeyValueOfstringstring>" +
|
||||
"<Key>Hello</Key><Value>World</Value></KeyValueOfstringstring></ArrayOfKeyValueOfstringstring>" };
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(BasicTypeValues))]
|
||||
public async Task XmlDataContractSerializerOutputFormatterCanWriteBasicTypes(object input, string expectedOutput)
|
||||
{
|
||||
// Arrange
|
||||
var formatter = new XmlDataContractSerializerOutputFormatter();
|
||||
var outputFormatterContext = GetOutputFormatterContext(input, typeof(object));
|
||||
|
||||
// Act
|
||||
await formatter.WriteAsync(outputFormatterContext);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body);
|
||||
outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0;
|
||||
Assert.Equal(expectedOutput,
|
||||
new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8)
|
||||
.ReadToEnd());
|
||||
Assert.True(outputFormatterContext.ActionContext.HttpContext.Response.Body.CanRead);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DefaultConstructor_ExpectedWriterSettings_Created()
|
||||
{
|
||||
// Arrange and Act
|
||||
var formatter = new XmlDataContractSerializerOutputFormatter();
|
||||
|
||||
// Assert
|
||||
var writerSettings = formatter.WriterSettings;
|
||||
Assert.NotNull(writerSettings);
|
||||
Assert.True(writerSettings.OmitXmlDeclaration);
|
||||
Assert.False(writerSettings.CloseOutput);
|
||||
Assert.False(writerSettings.CheckCharacters);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
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();
|
||||
Assert.Equal(expectedOutput, actualOutput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerOutputFormatterWritesSimpleTypes()
|
||||
{
|
||||
// Arrange
|
||||
var sampleInput = new DummyClass { SampleInt = 10 };
|
||||
var formatter = new XmlDataContractSerializerOutputFormatter();
|
||||
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:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
|
||||
"<SampleInt>10</SampleInt></DummyClass>",
|
||||
new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8)
|
||||
.ReadToEnd());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerOutputFormatterWritesComplexTypes()
|
||||
{
|
||||
// Arrange
|
||||
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
|
||||
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());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerOutputFormatterWritesOnModifiedWriterSettings()
|
||||
{
|
||||
// Arrange
|
||||
var sampleInput = new DummyClass { SampleInt = 10 };
|
||||
var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType());
|
||||
var formatter = new XmlDataContractSerializerOutputFormatter(
|
||||
new System.Xml.XmlWriterSettings
|
||||
{
|
||||
OmitXmlDeclaration = false,
|
||||
CloseOutput = 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-8\"?>" +
|
||||
"<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
|
||||
"<SampleInt>10</SampleInt></DummyClass>",
|
||||
new StreamReader(outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8).ReadToEnd());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
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();
|
||||
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());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerOutputFormatterWritesIndentedOutput()
|
||||
{
|
||||
// Arrange
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task VerifyBodyIsNotClosedAfterOutputIsWritten()
|
||||
{
|
||||
// Arrange
|
||||
var sampleInput = new DummyClass { SampleInt = 10 };
|
||||
var formatter = new XmlDataContractSerializerOutputFormatter();
|
||||
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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlSerializerOutputFormatterDoesntFlushOutputStream()
|
||||
{
|
||||
// Arrange
|
||||
var sampleInput = new DummyClass { SampleInt = 10 };
|
||||
var formatter = new XmlDataContractSerializerOutputFormatter();
|
||||
var outputFormatterContext = GetOutputFormatterContext(sampleInput, sampleInput.GetType());
|
||||
|
||||
var response = outputFormatterContext.ActionContext.HttpContext.Response;
|
||||
response.Body = FlushReportingStream.GetThrowingStream();
|
||||
|
||||
// Act & Assert
|
||||
await formatter.WriteAsync(outputFormatterContext);
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> TypesForCanWriteResult
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new object[] { null, typeof(string), true };
|
||||
yield return new object[] { null, null, false };
|
||||
yield return new object[] { new DummyClass { SampleInt = 5 }, null, true };
|
||||
yield return new object[] { new DummyClass { SampleInt = 5 }, typeof(object), true };
|
||||
yield return new object[] { null, typeof(object), true };
|
||||
yield return new object[] {
|
||||
new Dictionary<string, string> { { "Hello", "world" } }, typeof(object), true };
|
||||
yield return new object[] {
|
||||
new Dictionary<string, string> { { "Hello", "world" } }, typeof(Dictionary<string,string>), true };
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(TypesForCanWriteResult))]
|
||||
public void XmlDataContractSerializer_CanWriteResult(object input, Type declaredType, bool expectedOutput)
|
||||
{
|
||||
// Arrange
|
||||
var formatter = new XmlDataContractSerializerOutputFormatter();
|
||||
var outputFormatterContext = GetOutputFormatterContext(input, declaredType);
|
||||
|
||||
// Act
|
||||
var result = formatter.CanWriteResult(outputFormatterContext, MediaTypeHeaderValue.Parse("application/xml"));
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedOutput, result);
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> TypesForGetSupportedContentTypes
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new object[] { typeof(DummyClass), typeof(DummyClass), "application/xml" };
|
||||
yield return new object[] { typeof(DummyClass), typeof(object), "application/xml" };
|
||||
yield return new object[] { null, typeof(DummyClass), "application/xml" };
|
||||
yield return new object[] { typeof(DummyClass), null, "application/xml" };
|
||||
yield return new object[] { typeof(object), null, "application/xml" };
|
||||
yield return new object[] { null, null, null };
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(TypesForGetSupportedContentTypes))]
|
||||
public void XmlDataContractSerializer_GetSupportedContentTypes_Returns_SupportedTypes(Type declaredType, Type runtimeType, object expectedOutput)
|
||||
{
|
||||
// Arrange
|
||||
var formatter = new XmlDataContractSerializerOutputFormatter();
|
||||
|
||||
// Act
|
||||
var result = formatter.GetSupportedContentTypes(
|
||||
declaredType, runtimeType, MediaTypeHeaderValue.Parse("application/xml"));
|
||||
|
||||
// Assert
|
||||
if (expectedOutput != null)
|
||||
{
|
||||
Assert.Equal(expectedOutput, Assert.Single(result).ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Equal(expectedOutput, result);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerOutputFormatterThrowsWhenNotConfiguredWithKnownTypes()
|
||||
{
|
||||
// TODO: Test on Mono platform
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerOutputFormatterThrowsWhenNotConfiguredWithPreserveReferences()
|
||||
{
|
||||
// TODO: Test on Mono platform
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterWritesWhenConfiguredWithRootName()
|
||||
{
|
||||
// Arrange
|
||||
var sampleInt = 10;
|
||||
const string SubstituteRootName = "SomeOtherClass";
|
||||
const string SubstituteRootNamespace = "http://tempuri.org";
|
||||
const string 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
|
||||
Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body);
|
||||
outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0;
|
||||
var actualOutput = new StreamReader(
|
||||
outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8).ReadToEnd();
|
||||
Assert.Equal(expectedOutput, actualOutput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterWritesWhenConfiguredWithKnownTypes()
|
||||
{
|
||||
// Arrange
|
||||
var sampleInt = 10;
|
||||
var sampleString = "TestString";
|
||||
const string KnownTypeName = "SomeDummyClass";
|
||||
const string 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
|
||||
Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body);
|
||||
outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0;
|
||||
var actualOutput = new StreamReader(
|
||||
outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8).ReadToEnd();
|
||||
Assert.Equal(expectedOutput, actualOutput);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlDataContractSerializerFormatterWritesWhenConfiguredWithPreserveReferences()
|
||||
{
|
||||
// Arrange
|
||||
var sampleId = 1;
|
||||
var sampleName = "Parent";
|
||||
const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
|
||||
const string 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
|
||||
Assert.NotNull(outputFormatterContext.ActionContext.HttpContext.Response.Body);
|
||||
outputFormatterContext.ActionContext.HttpContext.Response.Body.Position = 0;
|
||||
var actualOutput = new StreamReader(
|
||||
outputFormatterContext.ActionContext.HttpContext.Response.Body, Encoding.UTF8).ReadToEnd();
|
||||
Assert.Equal(expectedOutput, actualOutput);
|
||||
}
|
||||
|
||||
private OutputFormatterContext GetOutputFormatterContext(object outputValue, Type outputType,
|
||||
string contentType = "application/xml; charset=utf-8")
|
||||
{
|
||||
return new OutputFormatterContext
|
||||
{
|
||||
Object = outputValue,
|
||||
DeclaredType = outputType,
|
||||
ActionContext = GetActionContext(contentType)
|
||||
};
|
||||
}
|
||||
|
||||
private static ActionContext GetActionContext(string contentType)
|
||||
{
|
||||
var request = new Mock<HttpRequest>();
|
||||
var headers = new HeaderDictionary(new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase));
|
||||
headers["Accept-Charset"] = MediaTypeHeaderValue.Parse(contentType).Charset;
|
||||
request.Setup(r => r.ContentType).Returns(contentType);
|
||||
request.SetupGet(r => r.Headers).Returns(headers);
|
||||
var response = new Mock<HttpResponse>();
|
||||
response.SetupGet(f => f.Body).Returns(new MemoryStream());
|
||||
var httpContext = new Mock<HttpContext>();
|
||||
httpContext.SetupGet(c => c.Request).Returns(request.Object);
|
||||
httpContext.SetupGet(c => c.Response).Returns(response.Object);
|
||||
return new ActionContext(httpContext.Object, routeData: null, actionDescriptor: null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue