Skip to content

Commit

Permalink
Problem: Router doesn't work with root URIs
Browse files Browse the repository at this point in the history
Registering URI containing only app name ('/MyApp') throws an exception
  • Loading branch information
joozek78 committed Sep 20, 2018
1 parent 7933956 commit 9fc7f9f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using System;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NUnit.Framework;
using Starcounter.Startup.Routing;
using Starcounter.Startup.Routing.Activation;
using Starcounter.Startup.Routing.Middleware;

namespace Starcounter.Startup.Tests.Routing.Middleware
{
public class UriHelperTests
{
[TestCase("/app/partial", "/app")]
[TestCase("/app/partial/dogs", "/app/dogs")]
[TestCase("/APP/PARTIAL/dogs", "/app/dogs")]
[TestCase("/app/partial/dogs/1", "/app/dogs/1")]
Expand All @@ -18,6 +23,7 @@ public void TestPartialToPage(string inputPartialUri, string expectedOutput)
.Should().BeEquivalentTo(expectedOutput);
}

[TestCase("/app", "/app/partial")]
[TestCase("/app/dogs", "/app/partial/dogs")]
[TestCase("/APP/dogs", "/app/partial/dogs")]
[TestCase("/app/dogs/1", "/app/partial/dogs/1")]
Expand All @@ -30,6 +36,7 @@ public void TestPageToPartial(string inputPageUri, string expectedOutput)
}

[TestCase("/app/dogs", false)]
[TestCase("/app/partial", true)]
[TestCase("/app/partial/dogs/1", true)]
[TestCase("/APP/PARTIAL/dogs/1", true)] // Self.GET lowers case of blended URIs
[TestCase("/app/partialdogs/2", false)]
Expand Down
6 changes: 5 additions & 1 deletion Starcounter.Startup/Routing/Middleware/UriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Starcounter.Startup.Routing.Middleware
{
public class UriHelper
{
private static readonly Regex PartialUriRegex = new Regex($"^/[^/]+{PartialPart}/", RegexOptions.IgnoreCase);
private static readonly Regex PartialUriRegex = new Regex($"^/[^/]+{PartialPart}(/|$)", RegexOptions.IgnoreCase);
private static readonly Regex TemplateArgumentRegex = new Regex(Regex.Escape("{?}"));
private const string PartialPart = "/partial";

Expand Down Expand Up @@ -88,6 +88,10 @@ public static string WithArguments(string uriTemplate, params string[] arguments
private static string ExtractApplicationName(string partialUri)
{
var indexOfSlash = partialUri.IndexOf("/", 1, StringComparison.Ordinal);
if (indexOfSlash == -1)
{
return partialUri.Substring(1);
}
var applicationName = partialUri.Substring(1, indexOfSlash - 1);
return applicationName;
}
Expand Down
8 changes: 8 additions & 0 deletions Starcounter.Startup/Routing/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public void HandleGet(Type pageType, HandlerOptions handlerOptions = null)
foreach (var urlAttribute in urlAttributes)
{
var pageUri = urlAttribute.Value;
if (pageUri[0] != '/')
{
throw new InvalidOperationException(StringsFormatted.Router_ViewModelRegisteredWithMalformedUri(pageType, pageUri));
}
if (UriHelper.IsPartialUri(pageUri))
{
throw new InvalidOperationException(StringsFormatted.Router_ViewModelRegisteredWithPartialUri(pageType, pageUri));
}
if (urlAttribute.External)
{
HandleGet(pageUri, pageType, handlerOptions);
Expand Down
18 changes: 18 additions & 0 deletions Starcounter.Startup/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Starcounter.Startup/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,10 @@
<data name="UriHelper_MalformedUri" xml:space="preserve">
<value>URI '{0}' is malformed: URI should start with a '/'</value>
</data>
<data name="Router_ViewModelRegisteredWithPartialUri" xml:space="preserve">
<value>View-model {0} is registered with partial URI: '{1}'. Use '{2}' instead</value>
</data>
<data name="Router_ViewModelRegisteredWithMalformedUri" xml:space="preserve">
<value>View-model {0} is registered with malformed URI: '{1}'. URI has to start with a slash sign ('/')</value>
</data>
</root>
7 changes: 7 additions & 0 deletions Starcounter.Startup/StringsFormatted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using Starcounter.Startup.Routing;
using Starcounter.Startup.Routing.Activation;
using Starcounter.Startup.Routing.Middleware;

namespace Starcounter.Startup
{
Expand Down Expand Up @@ -68,5 +69,11 @@ public static string UriHelper_CantFillUriTemplateSlotCountMismatch(string uriTe
String.Format(Strings.UriHelper_CantFillUriTemplateSlotCountMismatch, uriTemplate, slotsCount, arguments.Length, String.Join(",", arguments));

public static string UriHelper_MalformedUri(string pageUri) => String.Format(Strings.UriHelper_MalformedUri, pageUri);

public static string Router_ViewModelRegisteredWithMalformedUri(Type pageType, string pageUri) =>
String.Format(Strings.Router_ViewModelRegisteredWithMalformedUri, pageType, pageUri);

public static string Router_ViewModelRegisteredWithPartialUri(Type pageType, string pageUri) =>
String.Format(Strings.Router_ViewModelRegisteredWithPartialUri, pageType, pageUri, UriHelper.PartialToPage(pageUri));
}
}

0 comments on commit 9fc7f9f

Please sign in to comment.