Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Trim quotes from template #5838

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void OnProvidersExecuting(ActionDescriptorProviderContext context)
}

string template;
if (!PageDirectiveFeature.TryGetRouteTemplate(item, out template))
if (!PageDirectiveFeature.TryGetPageDirective(item, out template))
{
// .cshtml pages without @page are not RazorPages.
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,46 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
public static class PageDirectiveFeature
{
public static bool TryGetRouteTemplate(RazorProjectItem projectItem, out string template)
public static bool TryGetPageDirective(RazorProjectItem projectItem, out string template)
{
if (projectItem == null)
{
throw new ArgumentNullException(nameof(projectItem));
}

const string PageDirective = "@page";

string content;
using (var streamReader = new StreamReader(projectItem.Read()))
var stream = projectItem.Read();

string content = null;
using (var streamReader = new StreamReader(stream))
{
do
{
content = streamReader.ReadLine();
} while (content != null && string.IsNullOrWhiteSpace(content));
content = content?.Trim();
}

if (content == null || !content.StartsWith(PageDirective, StringComparison.Ordinal))
{
content = streamReader.ReadToEnd();
template = null;
return false;
}

if (content.StartsWith(PageDirective, StringComparison.Ordinal))
template = content.Substring(PageDirective.Length, content.Length - PageDirective.Length).TrimStart();

if (template.StartsWith("\"") && template.EndsWith("\""))
{
template = template.Substring(1, template.Length - 2);
}
// If it's not in quotes it's not our template
else
{
var newLineIndex = content.IndexOf(Environment.NewLine, PageDirective.Length);
template = content.Substring(PageDirective.Length, newLineIndex - PageDirective.Length).Trim();
return true;
template = string.Empty;
}

template = null;
return false;
return true;
}
}
}
34 changes: 31 additions & 3 deletions test/Microsoft.AspNetCore.Mvc.FunctionalTests/RazorPagesTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand All @@ -19,6 +18,19 @@ public RazorPagesTest(MvcTestFixture<RazorPagesWebSite.Startup> fixture)

public HttpClient Client { get; }

[Fact]
public async Task NoPage_NotFound()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/NoPage");

// Act
var response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}

[Fact]
public async Task HelloWorld_CanGetContent()
{
Expand Down Expand Up @@ -83,16 +95,32 @@ public async Task HelloWorldWithPageModelHandler_CanGetContent()
Assert.Equal("Hello, pagemodel!", content.Trim());
}

[Fact]
public async Task PageWithoutContent()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/PageWithoutContent/No/Content/Path");

// Act
var response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var content = await response.Content.ReadAsStringAsync();
Assert.Equal("", content);
}

[Fact]
public async Task TempData_SetTempDataInPage_CanReadValue()
{
// Arrange 1
var url = "http://localhost/TempData/SetTempDataOnPageAndRedirect?message=Hi1";
var request = new HttpRequestMessage(HttpMethod.Get, url);

// Act 1
var response = await Client.SendAsync(request);

// Assert 1
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void GetDescriptors_AddsDescriptorsForFileWithPageDirectiveAndRouteTempla
razorProject.Setup(p => p.EnumerateItems("/"))
.Returns(new[]
{
GetProjectItem("/", "/Test.cshtml", $"@page Home {Environment.NewLine}<h1>Hello world</h1>"),
GetProjectItem("/", "/Test.cshtml", $"@page \"Home\" {Environment.NewLine}<h1>Hello world</h1>"),
});
var provider = new PageActionDescriptorProvider(
razorProject.Object,
Expand Down Expand Up @@ -105,7 +105,7 @@ public void GetDescriptors_ThrowsIfRouteTemplatesAreOverriden(string template)
razorProject.Setup(p => p.EnumerateItems("/"))
.Returns(new[]
{
GetProjectItem("/", "/Test.cshtml", $"@page {template} {Environment.NewLine}<h1>Hello world</h1>"),
GetProjectItem("/", "/Test.cshtml", $"@page \"{template}\" {Environment.NewLine}<h1>Hello world</h1>"),
});
var provider = new PageActionDescriptorProvider(
razorProject.Object,
Expand Down Expand Up @@ -166,7 +166,7 @@ public void GetDescriptors_WithNonEmptyPageDirective_MapsIndexToEmptySegment()
razorProject.Setup(p => p.EnumerateItems("/"))
.Returns(new[]
{
GetProjectItem("", "/Catalog/Details/Index.cshtml", $"@page {{id:int?}} {Environment.NewLine}"),
GetProjectItem("", "/Catalog/Details/Index.cshtml", $"@page \"{{id:int?}}\" {Environment.NewLine}"),
});
var provider = new PageActionDescriptorProvider(
razorProject.Object,
Expand Down
Loading