147 lines
6.9 KiB
C#
147 lines
6.9 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.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNet.Mvc.Formatters.Xml;
|
|
using Microsoft.AspNet.Testing.xunit;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|
{
|
|
public class MvcSampleTests : IClassFixture<MvcTestFixture<MvcSample.Web.Startup>>
|
|
{
|
|
public MvcSampleTests(MvcTestFixture<MvcSample.Web.Startup> fixture)
|
|
{
|
|
Client = fixture.Client;
|
|
}
|
|
|
|
public HttpClient Client { get; }
|
|
|
|
[Theory]
|
|
[InlineData("")] // Shared/MyView.cshtml
|
|
[InlineData("/")] // Shared/MyView.cshtml
|
|
[InlineData("/Home/Index")] // Shared/MyView.cshtml
|
|
[InlineData("/Home/Create")] // Home/Create.cshtml
|
|
[InlineData("/Home/FlushPoint")] // Home/FlushPoint.cshtml
|
|
[InlineData("/Home/InjectSample")] // Home/InjectSample.cshtml
|
|
[InlineData("/Home/Language")] // Home/Language.cshtml
|
|
[InlineData("/Home/MyView")] // Shared/MyView.cshtml
|
|
[InlineData("/Home/NullUser")] // Home/NullUser.cshtml
|
|
[InlineData("/Home/Post")] // Shared/MyView.cshtml with null User
|
|
[InlineData("/Home/SaveUser")] // Shared/MyView.cshtml with uninitialized User instance
|
|
[InlineData("/Home/ValidationSummary")] // Home/ValidationSummary.cshtml
|
|
public async Task Home_Pages_ReturnSuccess(string path)
|
|
{
|
|
// Arrange & Act
|
|
var response = await Client.GetAsync("http://localhost" + path);
|
|
|
|
// Assert
|
|
Assert.NotNull(response);
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Name=SamplePerson&Address.Street=SampleStreet&Address.City=SampleCity&" +
|
|
"Address.State=SampleState&Address.ZipCode=11&PastJobs[0].JobTitle=SampleJob1&" +
|
|
"PastJobs[0].EmployerName=Employer1&PastJobs[0].Years=2&PastJobs[1].JobTitle=SampleJob2&" +
|
|
"PastJobs[1].EmployerName=Employer2&PastJobs[1].Years=4&PastJobs[2].JobTitle=SampleJob3&" +
|
|
"PastJobs[2].EmployerName=Employer3&PastJobs[2].Years=1", "true")]
|
|
// Input with some special characters
|
|
[InlineData("Name=SamplePerson&Address.Street=SampleStre'et&Address.City=S\ampleCity&" +
|
|
"Address.State=SampleState&Address.ZipCode=11&PastJobs[0].JobTitle=S~ampleJob1&" +
|
|
"PastJobs[0].EmployerName=Employer1&PastJobs[0].Years=2&PastJobs[1].JobTitle=SampleJob2&" +
|
|
"PastJobs[1].EmployerName=Employer2&PastJobs[1].Years=4&PastJobs[2].JobTitle=SampleJob3&" +
|
|
"PastJobs[2].EmployerName=Employer3&PastJobs[2].Years=1", "true")]
|
|
[InlineData("Name=SamplePerson", "false")]
|
|
public async Task FormUrlEncoded_ReturnsAppropriateResults(string input, string expectedOutput)
|
|
{
|
|
// Arrange
|
|
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/FormUrlEncoded/IsValidPerson");
|
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
request.Content = new StringContent(input, Encoding.UTF8, "application/x-www-form-urlencoded");
|
|
|
|
// Act
|
|
var response = await Client.SendAsync(request);
|
|
|
|
// Assert
|
|
Assert.Equal(expectedOutput, await response.Content.ReadAsStringAsync());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FormUrlEncoded_Index_ReturnSuccess()
|
|
{
|
|
// Arrange & Act
|
|
var response = await Client.GetAsync("http://localhost/FormUrlEncoded");
|
|
|
|
// Assert
|
|
Assert.NotNull(response);
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Home_NotFoundAction_Returns404()
|
|
{
|
|
// Arrange & Act
|
|
var response = await Client.GetAsync("http://localhost/Home/NotFound");
|
|
|
|
// Assert
|
|
Assert.NotNull(response);
|
|
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
|
|
}
|
|
|
|
[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 Home_CreateUser_ReturnsXmlBasedOnAcceptHeader()
|
|
{
|
|
// Arrange
|
|
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Home/ReturnUser");
|
|
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml"));
|
|
|
|
// Act
|
|
var response = await Client.SendAsync(request);
|
|
|
|
// Assert
|
|
Assert.NotNull(response);
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
XmlAssert.Equal("<User xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=" +
|
|
"\"http://schemas.datacontract.org/2004/07/MvcSample.Web.Models\"><About>I like playing Football" +
|
|
"</About><Address>My address</Address><Age>13</Age><Alive>true</Alive><Dependent><About i:nil=\"true\" />" +
|
|
"<Address>Dependents address</Address><Age>0</Age><Alive>false</Alive><Dependent i:nil=\"true\" />" +
|
|
"<EnumInformation>Zero</EnumInformation><GPA>0</GPA><Log i:nil=\"true\" /><Name>Dependents name</Name>" +
|
|
"<Profession i:nil=\"true\" /></Dependent><EnumInformation>Zero</EnumInformation><GPA>13.37</GPA><Log i:nil=\"true\" />" +
|
|
"<Name>My name</Name><Profession>Software Engineer</Profession></User>",
|
|
await response.Content.ReadAsStringAsync());
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("http://localhost/Filters/ChallengeUser", HttpStatusCode.Unauthorized)]
|
|
[InlineData("http://localhost/Filters/AllGranted", HttpStatusCode.Unauthorized)]
|
|
[InlineData("http://localhost/Filters/NotGrantedClaim", HttpStatusCode.Unauthorized)]
|
|
public async Task FiltersController_Tests(string url, HttpStatusCode statusCode)
|
|
{
|
|
// Arrange & Act
|
|
var response = await Client.GetAsync(url);
|
|
|
|
// Assert
|
|
Assert.NotNull(response);
|
|
Assert.Equal(statusCode, response.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FiltersController_Crash_ThrowsException()
|
|
{
|
|
// Arrange & Act
|
|
var response = await Client.GetAsync("http://localhost/Filters/Crash?message=HelloWorld");
|
|
|
|
// Assert
|
|
Assert.NotNull(response);
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
Assert.Equal("Boom HelloWorld", await response.Content.ReadAsStringAsync());
|
|
}
|
|
}
|
|
} |