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.
This commit is contained in:
Ryan Nowak 2017-02-21 15:01:08 -08:00
parent 36e4dbe941
commit 19331f9532
8 changed files with 142 additions and 8 deletions

View File

@ -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<PageContext, object> modelFactory = null;
Action<PageContext, object> 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);

View File

@ -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);
}

View File

@ -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<MvcTestFixture<RazorPagesWebSite.Startup>>
{
public RazorPagesTest(MvcTestFixture<RazorPagesWebSite.Startup> 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());
}
}
}

View File

@ -0,0 +1,3 @@
@page
Hello, World!

View File

@ -0,0 +1,12 @@
@page
@functions {
public string Message { get; set; }
public void OnGet(string message)
{
Message = message;
}
}
Hello, @Message!

View File

@ -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;
}
}
}

View File

@ -0,0 +1,4 @@
@page
@model RazorPagesWebSite.HelloWorldWithPageModelHandler
Hello, @Model.Message!

View File

@ -0,0 +1,3 @@
@page Some/Path/{text}
Hello, @(ViewContext.RouteData.Values["text"])!