aspnetcore/test/Microsoft.AspNetCore.Mvc.Fu.../XmlSerializerInputFormatter...

58 lines
2.2 KiB
C#

// 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.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Testing.xunit;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
{
public class XmlSerializerInputFormatterTests : IClassFixture<MvcTestFixture<XmlFormattersWebSite.Startup>>
{
public XmlSerializerInputFormatterTests(MvcTestFixture<XmlFormattersWebSite.Startup> fixture)
{
Client = fixture.Client;
}
public HttpClient Client { get; }
[Fact]
public async Task CheckIfXmlSerializerInputFormatterIsCalled()
{
// Arrange
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-xmlser");
// 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());
}
[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 ThrowsOnInvalidInput_AndAddsToModelState()
{
// Arrange
var input = "Not a valid xml document";
var content = new StringContent(input, Encoding.UTF8, "application/xml-xmlser");
// Act
var response = await Client.PostAsync("http://localhost/Home/Index", content);
// Assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
var data = await response.Content.ReadAsStringAsync();
Assert.Contains("There is an error in XML document", data);
}
}
}