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

Commit

Permalink
Using new StringRouteConstraint for area constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
ivano authored and rynowak committed Aug 8, 2016
1 parent 663f6a1 commit 6e5187c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;

namespace Microsoft.AspNetCore.Builder
{
Expand Down Expand Up @@ -131,7 +132,7 @@ public static IRouteBuilder MapAreaRoute(
defaultsDictionary["area"] = defaultsDictionary["area"] ?? areaName;

var constraintsDictionary = new RouteValueDictionary(constraints);
constraintsDictionary["area"] = constraintsDictionary["area"] ?? areaName;
constraintsDictionary["area"] = constraintsDictionary["area"] ?? new StringRouteConstraint(areaName);

routeBuilder.MapRoute(name, template, defaultsDictionary, constraintsDictionary, dataTokens);
return routeBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -33,7 +34,7 @@ public void MapAreaRoute_Simple()
kvp =>
{
Assert.Equal(kvp.Key, "area");
Assert.IsType<RegexRouteConstraint>(kvp.Value);
Assert.IsType<StringRouteConstraint>(kvp.Value);
});
Assert.Empty(route.DataTokens);
Assert.Collection(
Expand Down Expand Up @@ -68,7 +69,7 @@ public void MapAreaRoute_Defaults()
kvp =>
{
Assert.Equal(kvp.Key, "area");
Assert.IsType<RegexRouteConstraint>(kvp.Value);
Assert.IsType<StringRouteConstraint>(kvp.Value);
});
Assert.Empty(route.DataTokens);
Assert.Collection(
Expand Down Expand Up @@ -109,7 +110,7 @@ public void MapAreaRoute_DefaultsAndConstraints()
kvp =>
{
Assert.Equal(kvp.Key, "area");
Assert.IsType<RegexRouteConstraint>(kvp.Value);
Assert.IsType<StringRouteConstraint>(kvp.Value);
},
kvp =>
{
Expand Down Expand Up @@ -156,7 +157,7 @@ public void MapAreaRoute_DefaultsConstraintsAndDataTokens()
kvp =>
{
Assert.Equal(kvp.Key, "area");
Assert.IsType<RegexRouteConstraint>(kvp.Value);
Assert.IsType<StringRouteConstraint>(kvp.Value);
},
kvp =>
{
Expand Down Expand Up @@ -227,6 +228,48 @@ public void MapAreaRoute_DoesNotReplaceValuesForAreaIfAlreadyPresentInConstraint
});
}

[Fact]
public void MapAreaRoute_UsesPassedInAreaNameAsIs()
{
// Arrange
var builder = CreateRouteBuilder();
var areaName = "user.admin";

// Act
builder.MapAreaRoute(name: null, areaName: areaName, template: "site/Admin/");

// Assert
var route = Assert.IsType<Route>((Assert.Single(builder.Routes)));

Assert.Null(route.Name);
Assert.Equal("site/Admin/", route.RouteTemplate);
Assert.Collection(
route.Constraints.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal(kvp.Key, "area");
Assert.IsType<StringRouteConstraint>(kvp.Value);

var values = new RouteValueDictionary(new { area = areaName });
var match = kvp.Value.Match(
httpContext: Mock.Of<HttpContext>(),
route: new Mock<IRouter>().Object,
routeKey: kvp.Key,
values: values,
routeDirection: RouteDirection.UrlGeneration);

Assert.True(match);
});
Assert.Empty(route.DataTokens);
Assert.Collection(
route.Defaults.OrderBy(kvp => kvp.Key),
kvp =>
{
Assert.Equal(kvp.Key, "area");
Assert.Equal(kvp.Value, areaName);
});
}

private IServiceProvider CreateServices()
{
var services = new ServiceCollection();
Expand Down

0 comments on commit 6e5187c

Please sign in to comment.