From 19331f95326b18bd6f0400526074da934bf65cf0 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Tue, 21 Feb 2017 15:01:08 -0800 Subject: [PATCH] Adding some basic functional tests I ran into a blocking issue where page handlers aren't picked up, so I fixed it. Logging another issue to follow up with better tests for this. --- .../Internal/PageActionInvokerProvider.cs | 21 +++-- .../Internal/PassThruRazorPageActivator.cs | 6 +- .../RazorPagesTest.cs | 84 +++++++++++++++++++ .../RazorPagesWebSite/HelloWorld.cshtml | 3 + .../HelloWorldWithHandler.cshtml | 12 +++ .../HelloWorldWithPageModelHandler.cs | 17 ++++ .../HelloWorldWithPageModelHandler.cshtml | 4 + .../HelloWorldWithRoute.cshtml | 3 + 8 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs create mode 100644 test/WebSites/RazorPagesWebSite/HelloWorld.cshtml create mode 100644 test/WebSites/RazorPagesWebSite/HelloWorldWithHandler.cshtml create mode 100644 test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cs create mode 100644 test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cshtml create mode 100644 test/WebSites/RazorPagesWebSite/HelloWorldWithRoute.cshtml diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PageActionInvokerProvider.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PageActionInvokerProvider.cs index 3119c501a5..442cc7f023 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PageActionInvokerProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PageActionInvokerProvider.cs @@ -167,7 +167,14 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal { var actionDescriptor = (PageActionDescriptor)context.ActionContext.ActionDescriptor; var compiledType = _loader.Load(actionDescriptor).GetTypeInfo(); + + // If a model type wasn't set in code then the model property's type will be the same + // as the compiled type. var modelType = compiledType.GetProperty(ModelPropertyName)?.PropertyType.GetTypeInfo(); + if (modelType == compiledType) + { + modelType = null; + } var compiledActionDescriptor = new CompiledPageActionDescriptor(actionDescriptor) { @@ -180,16 +187,16 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal Func modelFactory = null; Action modelReleaser = null; - if (modelType != null) + if (modelType == null) { + PopulateHandlerMethodDescriptors(compiledType, compiledActionDescriptor); + } + else + { + PopulateHandlerMethodDescriptors(modelType, compiledActionDescriptor); + modelFactory = _modelFactoryProvider.CreateModelFactory(compiledActionDescriptor); modelReleaser = _modelFactoryProvider.CreateModelDisposer(compiledActionDescriptor); - - if (modelType != compiledType) - { - // If the model and page type are different discover handler methods on the model as well. - PopulateHandlerMethodDescriptors(modelType, compiledActionDescriptor); - } } var pageStartFactories = GetPageStartFactories(compiledActionDescriptor); diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PassThruRazorPageActivator.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PassThruRazorPageActivator.cs index 3305b07f62..aec0c3873f 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PassThruRazorPageActivator.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/PassThruRazorPageActivator.cs @@ -24,7 +24,11 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal { var pageContext = (PageContext)context; var vddType = typeof(ViewDataDictionary<>); - vddType = vddType.MakeGenericType(pageContext.ActionDescriptor.ModelTypeInfo.AsType()); + + var modelTypeInfo = pageContext.ActionDescriptor.ModelTypeInfo ?? pageContext.ActionDescriptor.PageTypeInfo; + + + vddType = vddType.MakeGenericType(modelTypeInfo.AsType()); context.ViewData = (ViewDataDictionary)Activator.CreateInstance(vddType, context.ViewData); } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs new file mode 100644 index 0000000000..1d509eb401 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs @@ -0,0 +1,84 @@ +// 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.Threading.Tasks; +using Xunit; + +namespace Microsoft.AspNetCore.Mvc.FunctionalTests +{ + public class RazorPagesTest : IClassFixture> + { + public RazorPagesTest(MvcTestFixture fixture) + { + Client = fixture.Client; + } + + public HttpClient Client { get; } + + [Fact] + public async Task HelloWorld_CanGetContent() + { + // Arrange + var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/HelloWorld"); + + // Act + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var content = await response.Content.ReadAsStringAsync(); + Assert.Equal("Hello, World!", content.Trim()); + } + + [Fact] + public async Task HelloWorldWithRoute_CanGetContent() + { + // Arrange + var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/HelloWorldWithRoute/Some/Path/route"); + + // Act + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var content = await response.Content.ReadAsStringAsync(); + Assert.Equal("Hello, route!", content.Trim()); + } + + [Fact] + public async Task HelloWorldWithHandler_CanGetContent() + { + // Arrange + var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/HelloWorldWithHandler?message=handler"); + + // Act + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var content = await response.Content.ReadAsStringAsync(); + Assert.Equal("Hello, handler!", content.Trim()); + } + + [Fact] + public async Task HelloWorldWithPageModelHandler_CanGetContent() + { + // Arrange + var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/HelloWorldWithPageModelHandler?message=pagemodel"); + + // Act + var response = await Client.SendAsync(request); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var content = await response.Content.ReadAsStringAsync(); + Assert.Equal("Hello, pagemodel!", content.Trim()); + } + } +} diff --git a/test/WebSites/RazorPagesWebSite/HelloWorld.cshtml b/test/WebSites/RazorPagesWebSite/HelloWorld.cshtml new file mode 100644 index 0000000000..69204d9455 --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/HelloWorld.cshtml @@ -0,0 +1,3 @@ +@page + +Hello, World! \ No newline at end of file diff --git a/test/WebSites/RazorPagesWebSite/HelloWorldWithHandler.cshtml b/test/WebSites/RazorPagesWebSite/HelloWorldWithHandler.cshtml new file mode 100644 index 0000000000..dfca1fb421 --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/HelloWorldWithHandler.cshtml @@ -0,0 +1,12 @@ +@page + +@functions { + public string Message { get; set; } + + public void OnGet(string message) + { + Message = message; + } +} + +Hello, @Message! \ No newline at end of file diff --git a/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cs b/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cs new file mode 100644 index 0000000000..8706216524 --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cs @@ -0,0 +1,17 @@ +// 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 Microsoft.AspNetCore.Mvc.RazorPages; + +namespace RazorPagesWebSite +{ + public class HelloWorldWithPageModelHandler : PageModel + { + public string Message { get; set; } + + public void OnGet(string message) + { + Message = message; + } + } +} diff --git a/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cshtml b/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cshtml new file mode 100644 index 0000000000..707e27e253 --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/HelloWorldWithPageModelHandler.cshtml @@ -0,0 +1,4 @@ +@page +@model RazorPagesWebSite.HelloWorldWithPageModelHandler + +Hello, @Model.Message! \ No newline at end of file diff --git a/test/WebSites/RazorPagesWebSite/HelloWorldWithRoute.cshtml b/test/WebSites/RazorPagesWebSite/HelloWorldWithRoute.cshtml new file mode 100644 index 0000000000..419f760dd5 --- /dev/null +++ b/test/WebSites/RazorPagesWebSite/HelloWorldWithRoute.cshtml @@ -0,0 +1,3 @@ +@page Some/Path/{text} + +Hello, @(ViewContext.RouteData.Values["text"])! \ No newline at end of file