Removing XmlSerializerInputFormatter from the list of Formatters.
Introducing a functional test for XmlSerializerInputFormatter.
This commit is contained in:
parent
4c951cc635
commit
0b1ad4ce35
13
Mvc.sln
13
Mvc.sln
|
|
@ -76,6 +76,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AddServicesWebSite", "test\
|
|||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "FiltersWebSite", "test\WebSites\FiltersWebSite\FiltersWebSite.kproj", "{1976AC4A-FEA4-4587-A158-D9F79736D2B6}"
|
||||
EndProject
|
||||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "XmlSerializerWebSite", "test\WebSites\XmlSerializerWebSite\XmlSerializerWebSite.kproj", "{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -386,6 +388,16 @@ Global
|
|||
{1976AC4A-FEA4-4587-A158-D9F79736D2B6}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{1976AC4A-FEA4-4587-A158-D9F79736D2B6}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{1976AC4A-FEA4-4587-A158-D9F79736D2B6}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -422,5 +434,6 @@ Global
|
|||
{A353B17E-A940-4CE8-8BF9-179E24A9041F} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
||||
{6A0B65CE-6B01-40D0-840D-EFF3680D1547} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
||||
{1976AC4A-FEA4-4587-A158-D9F79736D2B6} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
||||
{96107AC0-18E2-474D-BAB4-2FFF2185FBCD} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
|||
|
|
@ -40,12 +40,9 @@ namespace Microsoft.AspNet.Mvc
|
|||
indent: false));
|
||||
options.OutputFormatters.Add(
|
||||
new XmlDataContractSerializerOutputFormatter(XmlOutputFormatter.GetDefaultXmlWriterSettings()));
|
||||
options.OutputFormatters.Add(
|
||||
new XmlSerializerOutputFormatter(XmlOutputFormatter.GetDefaultXmlWriterSettings()));
|
||||
|
||||
// Set up default input formatters.
|
||||
options.InputFormatters.Add(new JsonInputFormatter());
|
||||
options.InputFormatters.Add(new XmlSerializerInputFormatter());
|
||||
options.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());
|
||||
|
||||
// Set up ValueProviders
|
||||
|
|
|
|||
|
|
@ -27,7 +27,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
var client = server.CreateClient();
|
||||
var sampleInputInt = 10;
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<DummyClass><SampleInt>" + sampleInputInt.ToString() + "</SampleInt></DummyClass>";
|
||||
"<DummyClass xmlns=\"http://schemas.datacontract.org/2004/07/FormatterWebSite\"><SampleInt>"
|
||||
+ sampleInputInt.ToString() + "</SampleInt></DummyClass>";
|
||||
var content = new StringContent(input, Encoding.UTF8, "application/xml");
|
||||
|
||||
// Act
|
||||
|
|
@ -103,8 +104,5 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
Assert.Equal("dummy", result.ParameterName);
|
||||
Assert.Equal(expectedSource, result.Source);
|
||||
}
|
||||
|
||||
// TODO: By default XmlSerializerInputFormatter is called because of the order in which
|
||||
// the formatters are registered. Add a test to call into DataContractSerializerInputFormatter.
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
// 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.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.TestHost;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||
{
|
||||
public class XmlSerializerInputFormatterTests
|
||||
{
|
||||
private readonly IServiceProvider _services = TestHelper.CreateServices("XmlSerializerWebSite");
|
||||
private readonly Action<IApplicationBuilder> _app = new XmlSerializerWebSite.Startup().Configure;
|
||||
|
||||
[Fact]
|
||||
public async Task CheckIfXmlSerializerInputFormatterIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
var server = TestServer.Create(_services, _app);
|
||||
var client = server.CreateClient();
|
||||
var sampleInputInt = 10;
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<DummyClass><SampleInt>"
|
||||
+ sampleInputInt.ToString() + "</SampleInt></DummyClass>";
|
||||
var content = new StringContent(input, Encoding.UTF8, "application/xml");
|
||||
|
||||
// Act
|
||||
var response = await client.PostAsync("http://localhost/Home/Index", content);
|
||||
|
||||
//Assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.Equal(sampleInputInt.ToString(), await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task XmlSerializerFormatter_ThrowsOnIncorrectInputNamespace()
|
||||
{
|
||||
// Arrange
|
||||
var server = TestServer.Create(_services, _app);
|
||||
var client = server.CreateClient();
|
||||
var sampleInputInt = 10;
|
||||
var input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<DummyClas xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" " +
|
||||
"i:type=\"DerivedDummyClass\" xmlns=\"http://schemas.datacontract.org/2004/07/XmlSerializerWebSite\">" +
|
||||
"<SampleInt>" + sampleInputInt.ToString() + "</SampleInt></DummyClass>";
|
||||
var content = new StringContent(input, Encoding.UTF8, "application/xml");
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
async () => await client.PostAsync("http://localhost/Home/Index", content));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -26,6 +26,7 @@
|
|||
"RoutingWebSite": "",
|
||||
"RazorWebSite": "",
|
||||
"ValueProvidersSite": "",
|
||||
"XmlSerializerWebSite": "",
|
||||
"Xunit.KRunner": "1.0.0-*"
|
||||
},
|
||||
"commands": {
|
||||
|
|
|
|||
|
|
@ -73,12 +73,11 @@ namespace Microsoft.AspNet.Mvc
|
|||
setup.Setup(mvcOptions);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(5, mvcOptions.OutputFormatters.Count);
|
||||
Assert.Equal(4, mvcOptions.OutputFormatters.Count);
|
||||
Assert.IsType<HttpNoContentOutputFormatter>(mvcOptions.OutputFormatters[0].Instance);
|
||||
Assert.IsType<TextPlainFormatter>(mvcOptions.OutputFormatters[1].Instance);
|
||||
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[2].Instance);
|
||||
Assert.IsType<XmlDataContractSerializerOutputFormatter>(mvcOptions.OutputFormatters[3].Instance);
|
||||
Assert.IsType<XmlSerializerOutputFormatter>(mvcOptions.OutputFormatters[4].Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -92,10 +91,9 @@ namespace Microsoft.AspNet.Mvc
|
|||
setup.Setup(mvcOptions);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(3, mvcOptions.InputFormatters.Count);
|
||||
Assert.Equal(2, mvcOptions.InputFormatters.Count);
|
||||
Assert.IsType<JsonInputFormatter>(mvcOptions.InputFormatters[0].Instance);
|
||||
Assert.IsType<XmlSerializerInputFormatter>(mvcOptions.InputFormatters[1].Instance);
|
||||
Assert.IsType<XmlDataContractSerializerInputFormatter>(mvcOptions.InputFormatters[2].Instance);
|
||||
Assert.IsType<XmlDataContractSerializerInputFormatter>(mvcOptions.InputFormatters[1].Instance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
// 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 Microsoft.AspNet.Mvc;
|
||||
|
||||
namespace XmlSerializerWebSite
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
[HttpPost]
|
||||
public IActionResult Index([FromBody]DummyClass dummyObject)
|
||||
{
|
||||
return Content(dummyObject.SampleInt.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
// 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.
|
||||
|
||||
namespace XmlSerializerWebSite
|
||||
{
|
||||
public class DummyClass
|
||||
{
|
||||
public int SampleInt { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
// 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 Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
|
||||
namespace XmlSerializerWebSite
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
var configuration = app.GetTestConfiguration();
|
||||
|
||||
// Set up application services
|
||||
app.UseServices(services =>
|
||||
{
|
||||
// Add MVC services to the services container
|
||||
services.AddMvc(configuration);
|
||||
|
||||
services.SetupOptions<MvcOptions>(options =>
|
||||
{
|
||||
options.InputFormatters.Clear();
|
||||
options.InputFormatters.Insert(0, new XmlSerializerInputFormatter());
|
||||
});
|
||||
});
|
||||
|
||||
// Add MVC to the request pipeline
|
||||
app.UseMvc(routes =>
|
||||
{
|
||||
routes.MapRoute("ActionAsMethod", "{controller}/{action}",
|
||||
defaults: new { controller = "Home", action = "Index" });
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="__ToolsVersion__" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>96107ac0-18e2-474d-bab4-2fff2185fbcd</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="$(OutputType) == 'Console'">
|
||||
<DebuggerFlavor>ConsoleDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="$(OutputType) == 'Web'">
|
||||
<DebuggerFlavor>WebDebugger</DebuggerFlavor>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" Label="Configuration">
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Mvc": "",
|
||||
"Microsoft.AspNet.Mvc.TestConfiguration": ""
|
||||
},
|
||||
"configurations": {
|
||||
"aspnet50": { },
|
||||
"aspnetcore50": { }
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue