From 0b1ad4ce3547fd049e3ddfbf745c29b1d5da9f7e Mon Sep 17 00:00:00 2001 From: sornaks Date: Tue, 16 Sep 2014 09:43:09 -0700 Subject: [PATCH] Removing XmlSerializerInputFormatter from the list of Formatters. Introducing a functional test for XmlSerializerInputFormatter. --- Mvc.sln | 13 +++++ src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs | 3 - .../InputFormatterTests.cs | 6 +- .../XmlSerializerInputFormatterTests.cs | 58 +++++++++++++++++++ .../project.json | 1 + .../MvcOptionSetupTest.cs | 8 +-- .../Controllers/HomeController.cs | 16 +++++ .../XmlSerializerWebSite/Models/DummyClass.cs | 10 ++++ test/WebSites/XmlSerializerWebSite/Startup.cs | 39 +++++++++++++ .../XmlSerializerWebSite.kproj | 28 +++++++++ .../XmlSerializerWebSite/project.json | 10 ++++ 11 files changed, 180 insertions(+), 12 deletions(-) create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/XmlSerializerInputFormatterTests.cs create mode 100644 test/WebSites/XmlSerializerWebSite/Controllers/HomeController.cs create mode 100644 test/WebSites/XmlSerializerWebSite/Models/DummyClass.cs create mode 100644 test/WebSites/XmlSerializerWebSite/Startup.cs create mode 100644 test/WebSites/XmlSerializerWebSite/XmlSerializerWebSite.kproj create mode 100644 test/WebSites/XmlSerializerWebSite/project.json diff --git a/Mvc.sln b/Mvc.sln index a0d0a70a8c..f40c765cac 100644 --- a/Mvc.sln +++ b/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 diff --git a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs index 3fced5aeb9..46484c9f8a 100644 --- a/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs +++ b/src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs @@ -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 diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/InputFormatterTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/InputFormatterTests.cs index 9750b485b0..bae0422055 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/InputFormatterTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/InputFormatterTests.cs @@ -27,7 +27,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests var client = server.CreateClient(); var sampleInputInt = 10; var input = "" + - "" + sampleInputInt.ToString() + ""; + "" + + sampleInputInt.ToString() + ""; 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. } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/XmlSerializerInputFormatterTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/XmlSerializerInputFormatterTests.cs new file mode 100644 index 0000000000..7d2840796d --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/XmlSerializerInputFormatterTests.cs @@ -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 _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 = "" + + "" + + sampleInputInt.ToString() + ""; + 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 = "" + + "" + + "" + sampleInputInt.ToString() + ""; + var content = new StringContent(input, Encoding.UTF8, "application/xml"); + + // Act & Assert + await Assert.ThrowsAsync( + async () => await client.PostAsync("http://localhost/Home/Index", content)); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index 39ce9d4c2a..a0a93c6e57 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -26,6 +26,7 @@ "RoutingWebSite": "", "RazorWebSite": "", "ValueProvidersSite": "", + "XmlSerializerWebSite": "", "Xunit.KRunner": "1.0.0-*" }, "commands": { diff --git a/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs b/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs index 823d541ab5..26b62dbf2c 100644 --- a/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs +++ b/test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs @@ -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(mvcOptions.OutputFormatters[0].Instance); Assert.IsType(mvcOptions.OutputFormatters[1].Instance); Assert.IsType(mvcOptions.OutputFormatters[2].Instance); Assert.IsType(mvcOptions.OutputFormatters[3].Instance); - Assert.IsType(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(mvcOptions.InputFormatters[0].Instance); - Assert.IsType(mvcOptions.InputFormatters[1].Instance); - Assert.IsType(mvcOptions.InputFormatters[2].Instance); + Assert.IsType(mvcOptions.InputFormatters[1].Instance); } [Fact] diff --git a/test/WebSites/XmlSerializerWebSite/Controllers/HomeController.cs b/test/WebSites/XmlSerializerWebSite/Controllers/HomeController.cs new file mode 100644 index 0000000000..2c1aa72be5 --- /dev/null +++ b/test/WebSites/XmlSerializerWebSite/Controllers/HomeController.cs @@ -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()); + } + } +} \ No newline at end of file diff --git a/test/WebSites/XmlSerializerWebSite/Models/DummyClass.cs b/test/WebSites/XmlSerializerWebSite/Models/DummyClass.cs new file mode 100644 index 0000000000..a57cf1a601 --- /dev/null +++ b/test/WebSites/XmlSerializerWebSite/Models/DummyClass.cs @@ -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; } + } +} \ No newline at end of file diff --git a/test/WebSites/XmlSerializerWebSite/Startup.cs b/test/WebSites/XmlSerializerWebSite/Startup.cs new file mode 100644 index 0000000000..9876e21748 --- /dev/null +++ b/test/WebSites/XmlSerializerWebSite/Startup.cs @@ -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(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" }); + + }); + } + } +} diff --git a/test/WebSites/XmlSerializerWebSite/XmlSerializerWebSite.kproj b/test/WebSites/XmlSerializerWebSite/XmlSerializerWebSite.kproj new file mode 100644 index 0000000000..c5354310ca --- /dev/null +++ b/test/WebSites/XmlSerializerWebSite/XmlSerializerWebSite.kproj @@ -0,0 +1,28 @@ + + + + 12.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 96107ac0-18e2-474d-bab4-2fff2185fbcd + Library + + + + ConsoleDebugger + + + WebDebugger + + + + + + + + 2.0 + + + \ No newline at end of file diff --git a/test/WebSites/XmlSerializerWebSite/project.json b/test/WebSites/XmlSerializerWebSite/project.json new file mode 100644 index 0000000000..9ab0d45e17 --- /dev/null +++ b/test/WebSites/XmlSerializerWebSite/project.json @@ -0,0 +1,10 @@ +{ + "dependencies": { + "Microsoft.AspNet.Mvc": "", + "Microsoft.AspNet.Mvc.TestConfiguration": "" + }, + "configurations": { + "aspnet50": { }, + "aspnetcore50": { } + } +}