diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs index cef92cd86c..17761cede0 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs @@ -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()); } diff --git a/test/WebSites/RazorPagesWebSite/CustomActionResult.cs b/test/WebSites/RazorPagesWebSite/CustomActionResult.cs new file mode 100644 index 0000000000..65ab917212 --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/CustomActionResult.cs @@ -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)); + } + } +} diff --git a/test/WebSites/RazorPagesWebSite/HandlerTestPage.cshtml b/test/WebSites/RazorPagesWebSite/HandlerTestPage.cshtml new file mode 100644 index 0000000000..7239ce3a49 --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/HandlerTestPage.cshtml @@ -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 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 OnPostCustomActionResult() + { + await TaskCache.CompletedTask; + return new CustomActionResult(); + } + + public CustomActionResult OnGetCustomACtionResultAsync() + { + return new CustomActionResult(); + } +} + +Method: @MethodName +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() +} diff --git a/test/WebSites/RazorPagesWebSite/ModelHandlerTestModel.cs b/test/WebSites/RazorPagesWebSite/ModelHandlerTestModel.cs new file mode 100644 index 0000000000..62ff94c90c --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/ModelHandlerTestModel.cs @@ -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 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 OnPostCustomActionResult() + { + await TaskCache.CompletedTask; + return new CustomActionResult(); + } + + public CustomActionResult OnGetCustomActionResultAsync() + { + return new CustomActionResult(); + } + } +} diff --git a/test/WebSites/RazorPagesWebSite/ModelHandlerTestPage.cshtml b/test/WebSites/RazorPagesWebSite/ModelHandlerTestPage.cshtml new file mode 100644 index 0000000000..a44692279d --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/ModelHandlerTestPage.cshtml @@ -0,0 +1,9 @@ +@page "{formaction?}" +@model RazorPagesWebSite.ModelHandlerTestModel + +Method: @Model.MethodName +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() +} +~/HelloWorld.cshtml \ No newline at end of file