Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Removing XmlSerializerInputFormatter from the list of Formatters.
Browse files Browse the repository at this point in the history
Introducing a functional test for XmlSerializerInputFormatter.
  • Loading branch information
sornaks committed Sep 22, 2014
1 parent 4c951cc commit 0b1ad4c
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 12 deletions.
13 changes: 13 additions & 0 deletions Mvc.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 0 additions & 3 deletions src/Microsoft.AspNet.Mvc/MvcOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ public void Setup(MvcOptions options)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public async Task CheckIfXmlInputFormatterIsBeingCalled()
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
Expand Down Expand Up @@ -103,8 +104,5 @@ public async Task JsonInputFormatter_IsSelectedForJsonRequest(string requestCont
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.
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
1 change: 1 addition & 0 deletions test/Microsoft.AspNet.Mvc.FunctionalTests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"RoutingWebSite": "",
"RazorWebSite": "",
"ValueProvidersSite": "",
"XmlSerializerWebSite": "",
"Xunit.KRunner": "1.0.0-*"
},
"commands": {
Expand Down
8 changes: 3 additions & 5 deletions test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ public void Setup_SetsUpOutputFormatters()
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]
Expand All @@ -92,10 +91,9 @@ public void Setup_SetsUpInputFormatters()
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]
Expand Down
16 changes: 16 additions & 0 deletions test/WebSites/XmlSerializerWebSite/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
10 changes: 10 additions & 0 deletions test/WebSites/XmlSerializerWebSite/Models/DummyClass.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
39 changes: 39 additions & 0 deletions test/WebSites/XmlSerializerWebSite/Startup.cs
Original file line number Diff line number Diff line change
@@ -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" });
});
}
}
}
28 changes: 28 additions & 0 deletions test/WebSites/XmlSerializerWebSite/XmlSerializerWebSite.kproj
Original file line number Diff line number Diff line change
@@ -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>
10 changes: 10 additions & 0 deletions test/WebSites/XmlSerializerWebSite/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"dependencies": {
"Microsoft.AspNet.Mvc": "",
"Microsoft.AspNet.Mvc.TestConfiguration": ""
},
"configurations": {
"aspnet50": { },
"aspnetcore50": { }
}
}

0 comments on commit 0b1ad4c

Please sign in to comment.