// 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.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Mvc.Xml; using Microsoft.AspNet.TestHost; using Xunit; namespace Microsoft.AspNet.Mvc.FunctionalTests { public class SerializableErrorTests { private readonly IServiceProvider _services = TestHelper.CreateServices(nameof(XmlFormattersWebSite)); private readonly Action _app = new XmlFormattersWebSite.Startup().Configure; [Theory] #if !ASPNETCORE50 [InlineData("application/xml-xmlser")] #endif [InlineData("application/xml-dcs")] public async Task ModelStateErrors_AreSerialized(string acceptHeader) { // Arrange var server = TestServer.Create(_services, _app); var client = server.CreateClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptHeader)); var expectedXml = "key1-errorThe input was not valid."; // Act var response = await client.GetAsync("http://localhost/SerializableError/ModelStateErrors"); //Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotNull(response.Content.Headers.ContentType); Assert.Equal(acceptHeader, response.Content.Headers.ContentType.MediaType); var responseData = await response.Content.ReadAsStringAsync(); XmlAssert.Equal(expectedXml, responseData); } [Theory] #if !ASPNETCORE50 [InlineData("application/xml-xmlser")] #endif [InlineData("application/xml-dcs")] public async Task PostedSerializableError_IsBound(string acceptHeader) { // Arrange var server = TestServer.Create(_services, _app); var client = server.CreateClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptHeader)); var expectedXml = "key1-errorThe input was not valid."; var requestContent = new StringContent(expectedXml, Encoding.UTF8, acceptHeader); // Act var response = await client.PostAsync("http://localhost/SerializableError/LogErrors", requestContent); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content); Assert.NotNull(response.Content.Headers.ContentType); Assert.Equal(acceptHeader, response.Content.Headers.ContentType.MediaType); var responseData = await response.Content.ReadAsStringAsync(); XmlAssert.Equal(expectedXml, responseData); } [Theory] [InlineData("application/xml-xmlser")] [InlineData("application/xml-dcs")] public async Task IsReturnedInExpectedFormat(string acceptHeader) { // Arrange var server = TestServer.Create(_services, _app); var client = server.CreateClient(); var input = "" + "" + "2foo"; var expected = "The field Id must be between 10 and 100." + "The field Name must be a string or array type with a minimum " + "length of '15'."; var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/SerializableError/CreateEmployee"); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(acceptHeader)); request.Content = new StringContent(input, Encoding.UTF8, "application/xml-dcs"); // Act var response = await client.SendAsync(request); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); var responseData = await response.Content.ReadAsStringAsync(); XmlAssert.Equal(expected, responseData); } } }