// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Formatters.Xml; using Microsoft.AspNetCore.Testing.xunit; using Xunit; namespace Microsoft.AspNetCore.Mvc.FunctionalTests { public class XmlOutputFormatterTests : IClassFixture> { public XmlOutputFormatterTests(MvcTestFixture fixture) { Client = fixture.Client; } public HttpClient Client { get; } [ConditionalFact] // Mono.Xml2.XmlTextReader.ReadText is unable to read the XML. This is fixed in mono 4.3.0. [FrameworkSkipCondition(RuntimeFrameworks.Mono)] public async Task XmlDataContractSerializerOutputFormatterIsCalled() { // Arrange var request = new HttpRequestMessage( HttpMethod.Post, "http://localhost/Home/GetDummyClass?sampleInput=10"); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml")); // Act var response = await Client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); XmlAssert.Equal( "" + "10", await response.Content.ReadAsStringAsync()); } [Fact] public async Task XmlSerializerOutputFormatterIsCalled() { // Arrange var request = new HttpRequestMessage( HttpMethod.Post, "http://localhost/XmlSerializer/GetDummyClass?sampleInput=10"); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml")); // Act var response = await Client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); XmlAssert.Equal( "10", await response.Content.ReadAsStringAsync()); } [ConditionalFact] // Mono issue - https://github.com/aspnet/External/issues/18 [FrameworkSkipCondition(RuntimeFrameworks.Mono)] public async Task XmlSerializerFailsAndDataContractSerializerIsCalled() { // Arrange var request = new HttpRequestMessage( HttpMethod.Post, "http://localhost/DataContractSerializer/GetPerson?name=HelloWorld"); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml")); // Act var response = await Client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); XmlAssert.Equal( "" + "HelloWorld", await response.Content.ReadAsStringAsync()); } [Fact] public async Task XmlSerializerOutputFormatter_WhenDerivedClassIsReturned() { // Arrange var request = new HttpRequestMessage( HttpMethod.Post, "http://localhost/XmlSerializer/GetDerivedDummyClass?sampleInput=10"); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml")); // Act var response = await Client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); XmlAssert.Equal( "" + "1050", await response.Content.ReadAsStringAsync()); } [ConditionalFact] // Mono.Xml2.XmlTextReader.ReadText is unable to read the XML. This is fixed in mono 4.3.0. [FrameworkSkipCondition(RuntimeFrameworks.Mono)] public async Task XmlDataContractSerializerOutputFormatter_WhenDerivedClassIsReturned() { // Arrange var request = new HttpRequestMessage( HttpMethod.Post, "http://localhost/Home/GetDerivedDummyClass?sampleInput=10"); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml")); // Act var response = await Client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); XmlAssert.Equal( "1050", await response.Content.ReadAsStringAsync()); } [Fact] public async Task XmlSerializerFormatter_DoesNotWriteDictionaryObjects() { // Arrange var request = new HttpRequestMessage( HttpMethod.Post, "http://localhost/XmlSerializer/GetDictionary"); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml")); // Act var response = await Client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.NotAcceptable, response.StatusCode); } } }