Issue #1081 - XML DataContract Formatter can fail to write when instance type != declaredType.

Adding Functional tests appropriately.
This commit is contained in:
sornaks 2014-09-19 11:04:01 -07:00
parent fe0a9331d2
commit 4c951cc635
6 changed files with 122 additions and 9 deletions

View File

@ -2,10 +2,10 @@
// 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.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.TestHost;
@ -29,10 +29,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
// Act
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Home/GetDummyClass?sampleInput=10");
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
// Act
var response = await client.SendAsync(request);
//Assert
@ -49,10 +49,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
// Act
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/XmlSerializer/GetDummyClass?sampleInput=10");
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
// Act
var response = await client.SendAsync(request);
//Assert
@ -68,11 +68,11 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
// Act
var request = new HttpRequestMessage(HttpMethod.Post,
"http://localhost/DataContractSerializer/GetPerson?name=HelloWorld");
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
// Act
var response = await client.SendAsync(request);
//Assert
@ -82,5 +82,64 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
"<Name>HelloWorld</Name></Person>",
await response.Content.ReadAsStringAsync());
}
[Fact]
public async Task XmlSerializerOutputFormatter_WhenDerivedClassIsReturned()
{
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
var request = new HttpRequestMessage(
HttpMethod.Post, "http://localhost/XmlSerializer/GetDerivedDummyClass?sampleInput=10");
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
// Act
var response = await client.SendAsync(request);
//Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("<DummyClass xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xsi:type=\"DerivedDummyClass\">" +
"<SampleInt>10</SampleInt><SampleIntInDerived>50</SampleIntInDerived></DummyClass>",
await response.Content.ReadAsStringAsync());
}
[Fact]
public async Task XmlDataContractSerializerOutputFormatter_WhenDerivedClassIsReturned()
{
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
var request = new HttpRequestMessage(
HttpMethod.Post, "http://localhost/Home/GetDerivedDummyClass?sampleInput=10");
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
// Act
var response = await client.SendAsync(request);
//Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("<DummyClass xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"i:type=\"DerivedDummyClass\" xmlns=\"http://schemas.datacontract.org/2004/07/FormatterWebSite\"" +
"><SampleInt>10</SampleInt><SampleIntInDerived>50</SampleIntInDerived></DummyClass>",
await response.Content.ReadAsStringAsync());
}
[Fact]
public async Task XmlSerializerFormatter_DoesNotWriteDictionaryObjects()
{
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
var request = new HttpRequestMessage(
HttpMethod.Post, "http://localhost/XmlSerializer/GetDictionary");
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml;charset=utf-8"));
// Act
var response = await client.SendAsync(request);
//Assert
Assert.Equal(HttpStatusCode.NotAcceptable, response.StatusCode);
}
}
}

View File

@ -24,5 +24,15 @@ namespace FormatterWebSite.Controllers
{
return dummy != null;
}
[HttpPost]
public DummyClass GetDerivedDummyClass(int sampleInput)
{
return new DerivedDummyClass
{
SampleInt = sampleInput,
SampleIntInDerived = 50
};
}
}
}

View File

@ -1,6 +1,7 @@
// 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.Collections.Generic;
using Microsoft.AspNet.Mvc;
namespace FormatterWebSite
@ -23,5 +24,24 @@ namespace FormatterWebSite
{
return new DummyClass { SampleInt = sampleInput };
}
[HttpPost]
public DummyClass GetDerivedDummyClass(int sampleInput)
{
return new DerivedDummyClass
{
SampleInt = sampleInput,
SampleIntInDerived = 50
};
}
[HttpPost]
public Dictionary<string, string> GetDictionary()
{
return new Dictionary<string, string>
{
{ "Hello", "World" }
};
}
}
}

View File

@ -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 FormatterWebSite
{
public class DerivedDummyClass : DummyClass
{
public int SampleIntInDerived { get; set; }
}
}

View File

@ -1,8 +1,13 @@
// 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.Runtime.Serialization;
using System.Xml.Serialization;
namespace FormatterWebSite
{
[KnownType(typeof(DerivedDummyClass))]
[XmlInclude(typeof(DerivedDummyClass))]
public class DummyClass
{
public int SampleInt { get; set; }

View File

@ -4,7 +4,16 @@
"Microsoft.AspNet.Mvc.TestConfiguration": ""
},
"configurations": {
"aspnet50": { },
"aspnetcore50": { }
"aspnet50": {
"dependencies": {
"System.Xml": ""
}
},
"aspnetcore50": {
"dependencies": {
"System.Runtime.Serialization.Xml": "4.0.0.0",
"System.Xml.ReaderWriter": "4.0.10.0"
}
}
}
}