Add handler functional test

This commit is contained in:
Ryan Brandenburg 2017-03-13 12:25:07 -07:00
parent b283740530
commit e6cb639cc0
5 changed files with 276 additions and 1 deletions

View File

@ -19,6 +19,153 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
public HttpClient Client { get; }
[Fact]
public async Task Page_Handler_FormAction()
{
// Arrange & Act
var content = await Client.GetStringAsync("http://localhost/HandlerTestPage/Customer");
// Assert
Assert.StartsWith("Method: OnGetCustomer", content.Trim());
}
[Fact]
public async Task Page_Handler_Async()
{
// Arrange
var getResponse = await Client.GetAsync("http://localhost/HandlerTestPage");
var getResponseBody = await getResponse.Content.ReadAsStringAsync();
var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(getResponseBody, "/ModelHandlerTestPage");
var cookie = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(getResponse);
var postRequest = new HttpRequestMessage(HttpMethod.Post, "http://localhost/HandlerTestPage");
postRequest.Headers.Add("Cookie", cookie.Key + "=" + cookie.Value);
postRequest.Headers.Add("RequestVerificationToken", formToken);
// Act
var response = await Client.SendAsync(postRequest);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.StartsWith("Method: OnPostAsync", content.Trim());
}
[Fact]
public async Task Page_Handler_AsyncFormAction()
{
// Arrange & Act
var content = await Client.GetStringAsync("http://localhost/HandlerTestPage/ViewCustomer");
// Assert
Assert.StartsWith("Method: OnGetViewCustomerAsync", content.Trim());
}
[Fact]
public async Task Page_Handler_ReturnTypeImplementsIActionResult()
{
// Arrange
var getResponse = await Client.GetAsync("http://localhost/HandlerTestPage");
var getResponseBody = await getResponse.Content.ReadAsStringAsync();
var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(getResponseBody, "/ModelHandlerTestPage");
var cookie = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(getResponse);
var postRequest = new HttpRequestMessage(HttpMethod.Post, "http://localhost/HandlerTestPage/CustomActionResult");
postRequest.Headers.Add("Cookie", cookie.Key + "=" + cookie.Value);
postRequest.Headers.Add("RequestVerificationToken", formToken);
// Act
var response = await Client.SendAsync(postRequest);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("CustomActionResult", content);
}
[Fact]
public async Task Page_Handler_AsyncReturnTypeImplementsIActionResult()
{
// Arrange & Act
var content = await Client.GetStringAsync("http://localhost/HandlerTestPage/CustomActionResult");
// Assert
Assert.Equal("CustomActionResult", content);
}
[Fact]
public async Task PageModel_Handler_FormAction()
{
// Arrange & Act
var content = await Client.GetStringAsync("http://localhost/ModelHandlerTestPage/Customer");
// Assert
Assert.StartsWith("Method: OnGetCustomer", content.Trim());
}
[Fact]
public async Task PageModel_Handler_Async()
{
// Arrange
var getResponse = await Client.GetAsync("http://localhost/ModelHandlerTestPage");
var getResponseBody = await getResponse.Content.ReadAsStringAsync();
var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(getResponseBody, "/ModelHandlerTestPage");
var cookie = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(getResponse);
var postRequest = new HttpRequestMessage(HttpMethod.Post, "http://localhost/ModelHandlerTestPage");
postRequest.Headers.Add("Cookie", cookie.Key + "=" + cookie.Value);
postRequest.Headers.Add("RequestVerificationToken", formToken);
// Act
var response = await Client.SendAsync(postRequest);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.StartsWith("Method: OnPostAsync", content.Trim());
}
[Fact]
public async Task PageModel_Handler_AsyncFormAction()
{
// Arrange & Act
var content = await Client.GetStringAsync("http://localhost/ModelHandlerTestPage/ViewCustomer");
// Assert
Assert.StartsWith("Method: OnGetViewCustomerAsync", content.Trim());
}
[Fact]
public async Task PageModel_Handler_ReturnTypeImplementsIActionResult()
{
// Arrange
var getResponse = await Client.GetAsync("http://localhost/ModelHandlerTestPage");
var getResponseBody = await getResponse.Content.ReadAsStringAsync();
var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(getResponseBody, "/ModelHandlerTestPage");
var cookie = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(getResponse);
var postRequest = new HttpRequestMessage(HttpMethod.Post, "http://localhost/ModelHandlerTestPage/CustomActionResult");
postRequest.Headers.Add("Cookie", cookie.Key + "=" + cookie.Value);
postRequest.Headers.Add("RequestVerificationToken", formToken);
// Act
var response = await Client.SendAsync(postRequest);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("CustomActionResult", content);
}
[Fact]
public async Task PageModel_Handler_AsyncReturnTypeImplementsIActionResult()
{
// Arrange & Act
var content = await Client.GetStringAsync("http://localhost/ModelHandlerTestPage/CustomActionResult");
// Assert
Assert.Equal("CustomActionResult", content);
}
[Fact]
public async Task Page_SetsPath()
{
@ -207,6 +354,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
// Act2
response = await Client.SendAsync(request);
// Assert 2
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("Hi1", content.Trim());
}
@ -228,9 +376,10 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
request = new HttpRequestMessage(HttpMethod.Get, response.Headers.Location);
request.Headers.Add("Cookie", GetCookie(response));
// Act2
// Act 2
response = await Client.SendAsync(request);
// Assert 2
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("Hi2", content.Trim());
}

View File

@ -0,0 +1,18 @@
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace RazorPagesWebSite
{
public class CustomActionResult : IActionResult
{
public Task ExecuteResultAsync(ActionContext context)
{
context.HttpContext.Response.StatusCode = 200;
return context.HttpContext.Response.WriteAsync(nameof(CustomActionResult));
}
}
}

View File

@ -0,0 +1,49 @@
@page "{formaction?}"
@using Microsoft.AspNetCore.Mvc.Internal
@using RazorPagesWebSite
@functions{
public string MethodName { get; set; }
public IActionResult OnGet()
{
return View();
}
public async Task<IActionResult> OnPostAsync()
{
await TaskCache.CompletedTask;
MethodName = nameof(OnPostAsync);
return View();
}
public async Task OnGetCustomer()
{
await TaskCache.CompletedTask;
MethodName = nameof(OnGetCustomer);
}
public async Task OnGetViewCustomerAsync()
{
await TaskCache.CompletedTask;
MethodName = nameof(OnGetViewCustomerAsync);
}
public async Task<CustomActionResult> OnPostCustomActionResult()
{
await TaskCache.CompletedTask;
return new CustomActionResult();
}
public CustomActionResult OnGetCustomACtionResultAsync()
{
return new CustomActionResult();
}
}
Method: @MethodName
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}

View File

@ -0,0 +1,50 @@
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RazorPagesWebSite
{
public class ModelHandlerTestModel : PageModel
{
public string MethodName { get; set; }
public IActionResult OnGet()
{
return View();
}
public async Task<IActionResult> OnPostAsync()
{
await TaskCache.CompletedTask;
MethodName = nameof(OnPostAsync);
return View();
}
public async Task OnGetCustomer()
{
await TaskCache.CompletedTask;
MethodName = nameof(OnGetCustomer);
}
public async Task OnGetViewCustomerAsync()
{
await TaskCache.CompletedTask;
MethodName = nameof(OnGetViewCustomerAsync);
}
public async Task<CustomActionResult> OnPostCustomActionResult()
{
await TaskCache.CompletedTask;
return new CustomActionResult();
}
public CustomActionResult OnGetCustomActionResultAsync()
{
return new CustomActionResult();
}
}
}

View File

@ -0,0 +1,9 @@
@page "{formaction?}"
@model RazorPagesWebSite.ModelHandlerTestModel
Method: @Model.MethodName
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
}
<a href="~/HelloWorld.cshtml">~/HelloWorld.cshtml</a>