// 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.Text;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Xml;
using Microsoft.AspNet.Testing;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc
{
public class XmlSerializerInputFormatterTest
{
public class DummyClass
{
public int SampleInt { get; set; }
}
public class TestLevelOne
{
public int SampleInt { get; set; }
public string sampleString;
public DateTime SampleDate { get; set; }
}
public class TestLevelTwo
{
public string SampleString { get; set; }
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("invalid", false)]
[InlineData(null, false)]
public void CanRead_ReturnsTrueForAnySupportedContentType(string requestContentType, bool expectedCanRead)
{
// Arrange
var formatter = new XmlSerializerInputFormatter();
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 HasProperSuppportedMediaTypes()
{
// Arrange & Act
var formatter = new XmlSerializerInputFormatter();
// 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 HasProperSuppportedEncodings()
{
// Arrange & Act
var formatter = new XmlSerializerInputFormatter();
// 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 ReadAsync_ReadsSimpleTypes()
{
// Arrange
var expectedInt = 10;
var expectedString = "TestString";
var expectedDateTime = XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc);
var input = "" +
"" + expectedInt + "" +
"" + expectedString + "" +
"" + expectedDateTime + "";
var formatter = new XmlSerializerInputFormatter();
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(model);
var levelOneModel = model as TestLevelOne;
Assert.Equal(expectedInt, levelOneModel.SampleInt);
Assert.Equal(expectedString, levelOneModel.sampleString);
Assert.Equal(XmlConvert.ToDateTime(expectedDateTime, XmlDateTimeSerializationMode.Utc),
levelOneModel.SampleDate);
}
[Fact]
public async Task ReadAsync_ReadsComplexTypes()
{
// Arrange
var expectedInt = 10;
var expectedString = "TestString";
var expectedDateTime = XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc);
var expectedLevelTwoString = "102";
var input = "" +
"" + expectedLevelTwoString + "" +
"" + expectedInt + "" +
"" + expectedString + "" +
"" + expectedDateTime + "";
var formatter = new XmlSerializerInputFormatter();
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(model);
var levelTwoModel = model as TestLevelTwo;
Assert.Equal(expectedLevelTwoString, levelTwoModel.SampleString);
Assert.Equal(expectedInt, levelTwoModel.TestOne.SampleInt);
Assert.Equal(expectedString, levelTwoModel.TestOne.sampleString);
Assert.Equal(XmlConvert.ToDateTime(expectedDateTime, XmlDateTimeSerializationMode.Utc),
levelTwoModel.TestOne.SampleDate);
}
[Fact]
public async Task ReadAsync_ReadsWhenMaxDepthIsModified()
{
// Arrange
var expectedInt = 10;
var input = "" +
"" + expectedInt + "";
var formatter = new XmlSerializerInputFormatter();
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(model);
var dummyModel = model as DummyClass;
Assert.Equal(expectedInt, dummyModel.SampleInt);
}
[Fact]
public async Task ReadAsync_ThrowsOnExceededMaxDepth()
{
if (TestPlatformHelper.IsMono)
{
// ReaderQuotas are not honored on Mono
return;
}
// Arrange
var input = "" +
"test" +
"10" +
"test" +
"" + XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc)
+ "";
var formatter = new XmlSerializerInputFormatter();
formatter.MaxDepth = 1;
var contentBytes = Encoding.UTF8.GetBytes(input);
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo));
// Act & Assert
await Assert.ThrowsAsync(typeof(InvalidOperationException), () => formatter.ReadAsync(context));
}
[Fact]
public async Task ReadAsync_ThrowsWhenReaderQuotasAreChanged()
{
if (TestPlatformHelper.IsMono)
{
// ReaderQuotas are not honored on Mono
return;
}
// Arrange
var input = "" +
"test" +
"10" +
"test" +
"" + XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc)
+ "";
var formatter = new XmlSerializerInputFormatter();
formatter.XmlDictionaryReaderQuotas.MaxStringContentLength = 10;
var contentBytes = Encoding.UTF8.GetBytes(input);
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo));
// Act & Assert
await Assert.ThrowsAsync(typeof(InvalidOperationException), () => formatter.ReadAsync(context));
}
[Fact]
public void SetMaxDepth_ThrowsWhenMaxDepthIsBelowOne()
{
// Arrange
var formatter = new XmlSerializerInputFormatter();
// Act & Assert
Assert.Throws(typeof(ArgumentException), () => formatter.MaxDepth = 0);
}
[Fact]
public async Task ReadAsync_VerifyStreamIsOpenAfterRead()
{
// Arrange
var input = "" +
"10";
var formatter = new XmlSerializerInputFormatter();
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 ReadAsync_ThrowsOnInvalidCharacters()
{
// Arrange
var expectedException = TestPlatformHelper.IsMono ? typeof(InvalidOperationException) :
typeof(XmlException);
var expectedMessage = TestPlatformHelper.IsMono ?
"There is an error in XML document." :
"The encoding in the declaration 'UTF-8' does not match the encoding of the document 'utf-16LE'.";
var inpStart = Encodings.UTF16EncodingLittleEndian.GetBytes("" +
"");
byte[] inp = { 192, 193 };
var inpEnd = Encodings.UTF16EncodingLittleEndian.GetBytes("");
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 XmlSerializerInputFormatter();
var context = GetInputFormatterContext(contentBytes, typeof(TestLevelTwo));
// Act and Assert
var ex = await Assert.ThrowsAsync(expectedException, () => formatter.ReadAsync(context));
Assert.Equal(expectedMessage, ex.Message);
}
[Fact]
public async Task ReadAsync_IgnoresBOMCharacters()
{
// Arrange
var sampleString = "Test";
var sampleStringBytes = Encoding.UTF8.GetBytes(sampleString);
var inputStart = Encoding.UTF8.GetBytes("" + Environment.NewLine +
"" + sampleString);
byte[] bom = { 0xef, 0xbb, 0xbf };
var inputEnd = Encoding.UTF8.GetBytes("");
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 XmlSerializerInputFormatter();
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 ReadAsync_AcceptsUTF16Characters()
{
// Arrange
var expectedInt = 10;
var expectedString = "TestString";
var expectedDateTime = XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc);
var input = "" +
"" + expectedInt + "" +
"" + expectedString + "" +
"" + expectedDateTime + "";
var formatter = new XmlSerializerInputFormatter();
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(model);
var levelOneModel = model as TestLevelOne;
Assert.Equal(expectedInt, levelOneModel.SampleInt);
Assert.Equal(expectedString, levelOneModel.sampleString);
Assert.Equal(XmlConvert.ToDateTime(expectedDateTime, XmlDateTimeSerializationMode.Utc), levelOneModel.SampleDate);
}
[Fact]
public async Task ReadAsync_ReadsSerializableErrorXml()
{
// Arrange
var serializableErrorXml = "" +
"Test Error 1 Test Error 2Test Error 3";
var formatter = new XmlSerializerInputFormatter();
var contentBytes = Encodings.UTF8EncodingWithoutBOM.GetBytes(serializableErrorXml);
var context = GetInputFormatterContext(contentBytes, typeof(SerializableError));
// Act
var model = await formatter.ReadAsync(context);
// Assert
var serializableError = model as SerializableError;
Assert.NotNull(serializableError);
Assert.Equal("Test Error 1 Test Error 2", serializableError["key1"]);
Assert.Equal("Test Error 3", serializableError["key2"]);
}
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();
var headers = new Mock();
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.SetupGet(c => c.Request).Returns(request.Object);
httpContext.SetupGet(c => c.Request).Returns(request.Object);
return httpContext.Object;
}
}
}
#endif