Routing DI Changes II : Adding services for routing.
This commit is contained in:
parent
28b89c8fe5
commit
a499d4a92a
|
|
@ -1,9 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.AspNet.Routing.Constraints;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace RoutingSample.Web
|
||||
{
|
||||
|
|
@ -11,7 +14,10 @@ namespace RoutingSample.Web
|
|||
{
|
||||
public void Configure(IBuilder builder)
|
||||
{
|
||||
var collectionBuilder = new RouteBuilder();
|
||||
builder.UseServices(services =>
|
||||
{
|
||||
services.Add(RoutingServices.GetDefaultServices());
|
||||
});
|
||||
|
||||
var endpoint1 = new DelegateRouteEndpoint(async (context) =>
|
||||
await context
|
||||
|
|
@ -20,37 +26,38 @@ namespace RoutingSample.Web
|
|||
.WriteAsync(
|
||||
"match1, route values -" + context.RouteData.Values.Print()));
|
||||
|
||||
var endpoint2 = new DelegateRouteEndpoint(async (context) =>
|
||||
var endpoint2 = new DelegateRouteEndpoint(async (context) =>
|
||||
await context
|
||||
.HttpContext
|
||||
.Response
|
||||
.WriteAsync("Hello, World!"));
|
||||
|
||||
collectionBuilder.DefaultHandler = endpoint1;
|
||||
collectionBuilder.ServiceProvider = builder.ApplicationServices;
|
||||
var routeBuilder = new RouteBuilder();
|
||||
routeBuilder.DefaultHandler = endpoint1;
|
||||
routeBuilder.ServiceProvider = builder.ApplicationServices;
|
||||
|
||||
collectionBuilder.AddPrefixRoute("api/store");
|
||||
routeBuilder.AddPrefixRoute("api/store");
|
||||
|
||||
collectionBuilder.MapRoute("defaultRoute",
|
||||
"api/constraint/{controller}",
|
||||
null,
|
||||
new { controller = "my.*" });
|
||||
collectionBuilder.MapRoute("regexStringRoute",
|
||||
"api/rconstraint/{controller}",
|
||||
new { foo = "Bar" },
|
||||
new { controller = new RegexConstraint("^(my.*)$") });
|
||||
collectionBuilder.MapRoute("regexRoute",
|
||||
"api/r2constraint/{controller}",
|
||||
new { foo = "Bar2" },
|
||||
new { controller = new RegexConstraint(new Regex("^(my.*)$")) });
|
||||
routeBuilder.MapRoute("defaultRoute",
|
||||
"api/constraint/{controller}",
|
||||
null,
|
||||
new { controller = "my.*" });
|
||||
routeBuilder.MapRoute("regexStringRoute",
|
||||
"api/rconstraint/{controller}",
|
||||
new { foo = "Bar" },
|
||||
new { controller = new RegexConstraint("^(my.*)$") });
|
||||
routeBuilder.MapRoute("regexRoute",
|
||||
"api/r2constraint/{controller}",
|
||||
new { foo = "Bar2" },
|
||||
new { controller = new RegexConstraint(new Regex("^(my.*)$")) });
|
||||
|
||||
collectionBuilder.MapRoute("parameterConstraintRoute",
|
||||
"api/{controller}/{*extra}",
|
||||
new { controller = "Store" });
|
||||
routeBuilder.MapRoute("parameterConstraintRoute",
|
||||
"api/{controller}/{*extra}",
|
||||
new { controller = "Store" });
|
||||
|
||||
collectionBuilder.AddPrefixRoute("hello/world", endpoint2);
|
||||
collectionBuilder.AddPrefixRoute("", endpoint2);
|
||||
builder.UseRouter(collectionBuilder.Build());
|
||||
routeBuilder.AddPrefixRoute("hello/world", endpoint2);
|
||||
routeBuilder.AddPrefixRoute("", endpoint2);
|
||||
builder.UseRouter(routeBuilder.Build());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,9 @@
|
|||
"version": "0.1-alpha-*",
|
||||
"dependencies": {
|
||||
"Helios": "0.1-alpha-*",
|
||||
"Microsoft.AspNet.Routing" : ""
|
||||
"Microsoft.AspNet.Routing" : "",
|
||||
"Microsoft.AspNet.RequestContainer": "0.1-alpha-*",
|
||||
"Microsoft.Framework.OptionsModel": "0.1-alpha-*"
|
||||
},
|
||||
"configurations": {
|
||||
"net45": { },
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ using System.Collections.Generic;
|
|||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNet.Routing.Constraints;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.AspNet.Routing
|
||||
{
|
||||
|
|
@ -17,26 +18,14 @@ namespace Microsoft.AspNet.Routing
|
|||
/// </summary>
|
||||
public class DefaultInlineConstraintResolver : IInlineConstraintResolver
|
||||
{
|
||||
private readonly IDictionary<string, Type> _inlineConstraintMap = GetDefaultConstraintMap();
|
||||
private readonly IDictionary<string, Type> _inlineConstraintMap;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mutable dictionary that maps constraint keys to a particular constraint type.
|
||||
/// </summary>
|
||||
public IDictionary<string, Type> ConstraintMap
|
||||
public DefaultInlineConstraintResolver(IServiceProvider serviceProvider,
|
||||
IOptionsAccessor<RouteOptions> routeOptions)
|
||||
{
|
||||
get
|
||||
{
|
||||
return _inlineConstraintMap;
|
||||
}
|
||||
}
|
||||
|
||||
private static IDictionary<string, Type> GetDefaultConstraintMap()
|
||||
{
|
||||
return new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
// Type-specific constraints
|
||||
{ "int", typeof(IntRouteConstraint) },
|
||||
};
|
||||
_serviceProvider = serviceProvider;
|
||||
_inlineConstraintMap = routeOptions.Options.ConstraintMap;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
|||
|
|
@ -18,7 +18,9 @@
|
|||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="project.json" />
|
||||
<Content Include="Resources.resx" />
|
||||
<Content Include="Resources.resx">
|
||||
<SubType>Designer</SubType>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BuilderExtensions.cs" />
|
||||
|
|
@ -30,6 +32,7 @@
|
|||
<Compile Include="INamedRouter.cs" />
|
||||
<Compile Include="InlineRouteParameterParser.cs" />
|
||||
<Compile Include="IRouteBuilder.cs" />
|
||||
<Compile Include="RouteOptions.cs" />
|
||||
<Compile Include="IRouteCollection.cs" />
|
||||
<Compile Include="IRouteConstraint.cs" />
|
||||
<Compile Include="IRouter.cs" />
|
||||
|
|
@ -43,6 +46,7 @@
|
|||
<Compile Include="RouteContext.cs" />
|
||||
<Compile Include="RouteData.cs" />
|
||||
<Compile Include="RouteDirection.cs" />
|
||||
<Compile Include="RouterServices.cs" />
|
||||
<Compile Include="RouterMiddleware.cs" />
|
||||
<Compile Include="RouteValueDictionary.cs" />
|
||||
<Compile Include="Template\Template.cs" />
|
||||
|
|
|
|||
|
|
@ -10,6 +10,22 @@ namespace Microsoft.AspNet.Routing
|
|||
private static readonly ResourceManager _resourceManager
|
||||
= new ResourceManager("Microsoft.AspNet.Routing.Resources", typeof(Resources).GetTypeInfo().Assembly);
|
||||
|
||||
/// <summary>
|
||||
/// The '{0}' property of '{1}' must not be null.
|
||||
/// </summary>
|
||||
internal static string PropertyOfTypeCannotBeNull
|
||||
{
|
||||
get { return GetString("PropertyOfTypeCannotBeNull"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The '{0}' property of '{1}' must not be null.
|
||||
/// </summary>
|
||||
internal static string FormatPropertyOfTypeCannotBeNull(object p0, object p1)
|
||||
{
|
||||
return string.Format(CultureInfo.CurrentCulture, GetString("PropertyOfTypeCannotBeNull"), p0, p1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The supplied route name '{0}' is ambiguous and matched more than one route.
|
||||
/// </summary>
|
||||
|
|
@ -123,7 +139,7 @@ namespace Microsoft.AspNet.Routing
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// The route parameter '{0}' has both an inline deafult value and an explicit default value specified. A route parameter cannot contain an inline default value when a default value is specified explicitly. Consider removing one of them.
|
||||
/// The route parameter '{0}' has both an inline default value and an explicit default value specified. A route parameter cannot contain an inline default value when a default value is specified explicitly. Consider removing one of them.
|
||||
/// </summary>
|
||||
internal static string TemplateRoute_CannotHaveDefaultValueSpecifiedInlineAndExplicitly
|
||||
{
|
||||
|
|
@ -131,7 +147,7 @@ namespace Microsoft.AspNet.Routing
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// The route parameter '{0}' has both an inline deafult value and an explicit default value specified. A route parameter cannot contain an inline default value when a default value is specified explicitly. Consider removing one of them.
|
||||
/// The route parameter '{0}' has both an inline default value and an explicit default value specified. A route parameter cannot contain an inline default value when a default value is specified explicitly. Consider removing one of them.
|
||||
/// </summary>
|
||||
internal static string FormatTemplateRoute_CannotHaveDefaultValueSpecifiedInlineAndExplicitly(object p0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -117,6 +117,9 @@
|
|||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<data name="PropertyOfTypeCannotBeNull" xml:space="preserve">
|
||||
<value>The '{0}' property of '{1}' must not be null.</value>
|
||||
</data>
|
||||
<data name="NamedRoutes_AmbiguousRoutesFound" xml:space="preserve">
|
||||
<value>The supplied route name '{0}' is ambiguous and matched more than one route.</value>
|
||||
</data>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. 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.Collections.Generic;
|
||||
using Microsoft.AspNet.Routing.Constraints;
|
||||
|
||||
namespace Microsoft.AspNet.Routing
|
||||
{
|
||||
public class RouteOptions
|
||||
{
|
||||
private IDictionary<string, Type> _constraintTypeMap = GetDefaultConstraintMap();
|
||||
|
||||
public IDictionary<string, Type> ConstraintMap
|
||||
{
|
||||
get
|
||||
{
|
||||
return _constraintTypeMap;
|
||||
}
|
||||
set
|
||||
{
|
||||
if(value == null)
|
||||
{
|
||||
throw new ArgumentNullException("value",
|
||||
Resources.FormatPropertyOfTypeCannotBeNull(
|
||||
"ConstraintMap", typeof(RouteOptions)));
|
||||
}
|
||||
|
||||
_constraintTypeMap = value;
|
||||
}
|
||||
}
|
||||
|
||||
private static IDictionary<string, Type> GetDefaultConstraintMap()
|
||||
{
|
||||
return new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
// Type-specific constraints
|
||||
{ "int", typeof(IntRouteConstraint) },
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.Framework.ConfigurationModel;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.NestedProviders;
|
||||
|
||||
namespace Microsoft.AspNet.Routing
|
||||
{
|
||||
public class RoutingServices
|
||||
{
|
||||
public static IEnumerable<IServiceDescriptor> GetDefaultServices()
|
||||
{
|
||||
return GetDefaultServices(new Configuration());
|
||||
}
|
||||
|
||||
public static IEnumerable<IServiceDescriptor> GetDefaultServices(IConfiguration configuration)
|
||||
{
|
||||
var describe = new ServiceDescriber(configuration);
|
||||
|
||||
yield return describe.Transient<IInlineConstraintResolver, DefaultInlineConstraintResolver>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,9 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Http": "0.1-alpha-*",
|
||||
"Microsoft.Framework.DependencyInjection" : "0.1-alpha-*"
|
||||
"Microsoft.Framework.DependencyInjection" : "0.1-alpha-*",
|
||||
"Microsoft.AspNet.RequestContainer": "0.1-alpha-*",
|
||||
"Microsoft.Framework.OptionsModel": "0.1-alpha-*"
|
||||
},
|
||||
"configurations": {
|
||||
"net45": {},
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
#if NET45
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Routing.Constraints;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Routing.Tests
|
||||
|
|
@ -14,8 +18,12 @@ namespace Microsoft.AspNet.Routing.Tests
|
|||
[Fact]
|
||||
public void ResolveConstraint_IntConstraint_ResolvesCorrectly()
|
||||
{
|
||||
// Arrange & Act
|
||||
var constraint = new DefaultInlineConstraintResolver().ResolveConstraint("int");
|
||||
// Arrange
|
||||
var routeOptions = new RouteOptions();
|
||||
var constraintResolver = GetInlineConstraintResolver(routeOptions);
|
||||
|
||||
// Act
|
||||
var constraint = constraintResolver.ResolveConstraint("int");
|
||||
|
||||
// Assert
|
||||
Assert.IsType<IntRouteConstraint>(constraint);
|
||||
|
|
@ -24,9 +32,13 @@ namespace Microsoft.AspNet.Routing.Tests
|
|||
[Fact]
|
||||
public void ResolveConstraint_IntConstraintWithArgument_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var routeOptions = new RouteOptions();
|
||||
var constraintResolver = GetInlineConstraintResolver(routeOptions);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<InvalidOperationException>(
|
||||
() => new DefaultInlineConstraintResolver().ResolveConstraint("int(5)"));
|
||||
() => constraintResolver.ResolveConstraint("int(5)"));
|
||||
Assert.Equal("Could not find a constructor for constraint type 'IntRouteConstraint'"+
|
||||
" with the following number of parameters: 1.",
|
||||
ex.Message);
|
||||
|
|
@ -36,8 +48,9 @@ namespace Microsoft.AspNet.Routing.Tests
|
|||
public void ResolveConstraint_SupportsCustomConstraints()
|
||||
{
|
||||
// Arrange
|
||||
var resolver = new DefaultInlineConstraintResolver();
|
||||
resolver.ConstraintMap.Add("custom", typeof(CustomRouteConstraint));
|
||||
var routeOptions = new RouteOptions();
|
||||
routeOptions.ConstraintMap.Add("custom", typeof(CustomRouteConstraint));
|
||||
var resolver = GetInlineConstraintResolver(routeOptions);
|
||||
|
||||
// Act
|
||||
var constraint = resolver.ResolveConstraint("custom(argument)");
|
||||
|
|
@ -50,8 +63,9 @@ namespace Microsoft.AspNet.Routing.Tests
|
|||
public void ResolveConstraint_CustomConstraintThatDoesNotImplementIRouteConstraint_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var resolver = new DefaultInlineConstraintResolver();
|
||||
resolver.ConstraintMap.Add("custom", typeof(string));
|
||||
var routeOptions = new RouteOptions();
|
||||
routeOptions.ConstraintMap.Add("custom", typeof(string));
|
||||
var resolver = GetInlineConstraintResolver(routeOptions);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<InvalidOperationException>(() => resolver.ResolveConstraint("custom"));
|
||||
|
|
@ -60,6 +74,16 @@ namespace Microsoft.AspNet.Routing.Tests
|
|||
ex.Message);
|
||||
}
|
||||
|
||||
private IInlineConstraintResolver GetInlineConstraintResolver(RouteOptions routeOptions)
|
||||
{
|
||||
var optionsAccessor = new Mock<IOptionsAccessor<RouteOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Options).Returns(routeOptions);
|
||||
var serviceProvider = new Mock<IServiceProvider>();
|
||||
serviceProvider.Setup(o => o.GetService(It.Is<Type>(type => type == typeof(ITypeActivator))))
|
||||
.Returns(new TypeActivator());
|
||||
return new DefaultInlineConstraintResolver(serviceProvider.Object, optionsAccessor.Object);
|
||||
}
|
||||
|
||||
private class CustomRouteConstraint : IRouteConstraint
|
||||
{
|
||||
public CustomRouteConstraint(string pattern)
|
||||
|
|
@ -79,3 +103,4 @@ namespace Microsoft.AspNet.Routing.Tests
|
|||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ using System.Linq;
|
|||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Routing.Constraints;
|
||||
using Microsoft.AspNet.Routing.Template;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Routing.Tests
|
||||
|
|
@ -164,7 +167,7 @@ namespace Microsoft.AspNet.Routing.Tests
|
|||
[Fact]
|
||||
public void ParseRouteParameter_ConstraintWithCommaInPattern_PatternIsParsedCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
// Arrange & Act
|
||||
var templatePart = ParseParameter(@"param:test(\w,\w)");
|
||||
|
||||
// Assert
|
||||
|
|
@ -267,22 +270,29 @@ namespace Microsoft.AspNet.Routing.Tests
|
|||
|
||||
private TemplatePart ParseParameter(string routeParameter)
|
||||
{
|
||||
var constraintResolver = new DefaultInlineConstraintResolver();
|
||||
|
||||
// TODO: This will be removed once this is supported in product code.
|
||||
constraintResolver.ConstraintMap.Add("test", typeof(TestRouteConstraint));
|
||||
var constraintResolver = GetConstraintResolver();
|
||||
var templatePart = InlineRouteParameterParser.ParseRouteParameter(routeParameter, constraintResolver);
|
||||
return templatePart;
|
||||
}
|
||||
|
||||
private static Template.Template ParseRouteTemplate(string template)
|
||||
{
|
||||
var constraintResolver = new DefaultInlineConstraintResolver();
|
||||
|
||||
constraintResolver.ConstraintMap.Add("test", typeof(TestRouteConstraint));
|
||||
var constraintResolver = GetConstraintResolver();
|
||||
return TemplateParser.Parse(template, constraintResolver);
|
||||
}
|
||||
|
||||
private static IInlineConstraintResolver GetConstraintResolver()
|
||||
{
|
||||
var services = new ServiceCollection { OptionsServices.GetDefaultServices() };
|
||||
services.SetupOptions<RouteOptions>(options =>
|
||||
options
|
||||
.ConstraintMap
|
||||
.Add("test", typeof(TestRouteConstraint)));
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
var accessor = serviceProvider.GetService<IOptionsAccessor<RouteOptions>>();
|
||||
return new DefaultInlineConstraintResolver(serviceProvider, accessor);
|
||||
}
|
||||
|
||||
private class TestRouteConstraint : IRouteConstraint
|
||||
{
|
||||
public TestRouteConstraint(string pattern)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Routing.Tests
|
||||
{
|
||||
public class RouteOptionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void ConstraintMap_SettingNullValue_Throws()
|
||||
{
|
||||
// Arrange
|
||||
var options = new RouteOptions();
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<ArgumentNullException>(() => options.ConstraintMap = null);
|
||||
Assert.Equal("The 'ConstraintMap' property of 'Microsoft.AspNet.Routing.RouteOptions' must not be null." +
|
||||
"\r\nParameter name: value", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +1,22 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
#if NET45
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Routing.Template.Tests
|
||||
{
|
||||
public class TemplateBinderTests
|
||||
{
|
||||
private static IInlineConstraintResolver _inlineConstraintResolver = GetInlineConstraintResolver();
|
||||
|
||||
public static IEnumerable<object[]> EmptyAndNullDefaultValues
|
||||
{
|
||||
get
|
||||
|
|
@ -127,7 +134,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
string expected)
|
||||
{
|
||||
// Arrange
|
||||
var binder = new TemplateBinder(TemplateParser.Parse(template, new DefaultInlineConstraintResolver()),
|
||||
var binder = new TemplateBinder(TemplateParser.Parse(template, _inlineConstraintResolver),
|
||||
defaults);
|
||||
|
||||
// Act & Assert
|
||||
|
|
@ -961,7 +968,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
string expected)
|
||||
{
|
||||
// Arrange
|
||||
var binder = new TemplateBinder(TemplateParser.Parse(template, new DefaultInlineConstraintResolver()), defaults);
|
||||
var binder = new TemplateBinder(TemplateParser.Parse(template, _inlineConstraintResolver), defaults);
|
||||
|
||||
// Act & Assert
|
||||
var acceptedValues = binder.GetAcceptedValues(ambientValues, values);
|
||||
|
|
@ -1025,6 +1032,14 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
expected);
|
||||
}
|
||||
|
||||
private static IInlineConstraintResolver GetInlineConstraintResolver()
|
||||
{
|
||||
var services = new ServiceCollection { OptionsServices.GetDefaultServices() };
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
var accessor = serviceProvider.GetService<IOptionsAccessor<RouteOptions>>();
|
||||
return new DefaultInlineConstraintResolver(serviceProvider, accessor);
|
||||
}
|
||||
|
||||
private class PathAndQuery
|
||||
{
|
||||
public PathAndQuery(string uri)
|
||||
|
|
@ -1053,3 +1068,4 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,13 +1,21 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
#if NET45
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Routing.Constraints;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Routing.Template.Tests
|
||||
{
|
||||
public class TemplateMatcherTests
|
||||
{
|
||||
private static IInlineConstraintResolver _inlineConstraintResolver = GetInlineConstraintResolver();
|
||||
|
||||
[Fact]
|
||||
public void MatchSingleRoute()
|
||||
{
|
||||
|
|
@ -784,13 +792,13 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
|
||||
private TemplateMatcher CreateMatcher(string template)
|
||||
{
|
||||
return new TemplateMatcher(TemplateParser.Parse(template, new DefaultInlineConstraintResolver()));
|
||||
return new TemplateMatcher(TemplateParser.Parse(template, _inlineConstraintResolver));
|
||||
}
|
||||
|
||||
private static void RunTest(string template, string path, IDictionary<string, object> defaults, IDictionary<string, object> expected)
|
||||
{
|
||||
// Arrange
|
||||
var matcher = new TemplateMatcher(TemplateParser.Parse(template, new DefaultInlineConstraintResolver()));
|
||||
var matcher = new TemplateMatcher(TemplateParser.Parse(template, _inlineConstraintResolver));
|
||||
|
||||
// Act
|
||||
var match = matcher.Match(path, defaults);
|
||||
|
|
@ -810,5 +818,14 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IInlineConstraintResolver GetInlineConstraintResolver()
|
||||
{
|
||||
var services = new ServiceCollection { OptionsServices.GetDefaultServices() };
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
var accessor = serviceProvider.GetService<IOptionsAccessor<RouteOptions>>();
|
||||
return new DefaultInlineConstraintResolver(serviceProvider, accessor);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,16 +1,21 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
#if NET45
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.DependencyInjection.Fallback;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Routing.Template.Tests
|
||||
{
|
||||
public class TemplateRouteParserTests
|
||||
{
|
||||
private IInlineConstraintResolver _inlineConstraintResolver = new DefaultInlineConstraintResolver();
|
||||
private IInlineConstraintResolver _inlineConstraintResolver = GetInlineConstraintResolver();
|
||||
|
||||
[Fact]
|
||||
public void Parse_SingleLiteral()
|
||||
|
|
@ -440,7 +445,15 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
"A catch-all parameter cannot be marked optional." + Environment.NewLine +
|
||||
"Parameter name: routeTemplate");
|
||||
}
|
||||
|
||||
|
||||
private static IInlineConstraintResolver GetInlineConstraintResolver()
|
||||
{
|
||||
var services = new ServiceCollection { OptionsServices.GetDefaultServices() };
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
var accessor = serviceProvider.GetService<IOptionsAccessor<RouteOptions>>();
|
||||
return new DefaultInlineConstraintResolver(serviceProvider, accessor);
|
||||
}
|
||||
|
||||
private class TemplateEqualityComparer : IEqualityComparer<Template>
|
||||
{
|
||||
public bool Equals(Template x, Template y)
|
||||
|
|
@ -516,3 +529,4 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -17,6 +17,8 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
{
|
||||
public class TemplateRouteTests
|
||||
{
|
||||
private static IInlineConstraintResolver _inlineConstraintResolver = GetInlineConstraintResolver();
|
||||
|
||||
#region Route Matching
|
||||
|
||||
// PathString in HttpAbstractions guarantees a leading slash - so no value in testing other cases.
|
||||
|
|
@ -507,7 +509,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
|
||||
var serviceProviderMock = new Mock<IServiceProvider>();
|
||||
serviceProviderMock.Setup(o => o.GetService(typeof(IInlineConstraintResolver)))
|
||||
.Returns(new DefaultInlineConstraintResolver());
|
||||
.Returns(_inlineConstraintResolver);
|
||||
routeBuilder.ServiceProvider = serviceProviderMock.Object;
|
||||
|
||||
return routeBuilder;
|
||||
|
|
@ -515,7 +517,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
|
||||
private static TemplateRoute CreateRoute(string template, bool accept = true)
|
||||
{
|
||||
return new TemplateRoute(CreateTarget(accept), template, new DefaultInlineConstraintResolver());
|
||||
return new TemplateRoute(CreateTarget(accept), template, _inlineConstraintResolver);
|
||||
}
|
||||
|
||||
private static TemplateRoute CreateRoute(string template, object defaults, bool accept = true, IDictionary<string, object> constraints = null)
|
||||
|
|
@ -524,7 +526,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
template,
|
||||
new RouteValueDictionary(defaults),
|
||||
constraints,
|
||||
new DefaultInlineConstraintResolver());
|
||||
_inlineConstraintResolver);
|
||||
}
|
||||
|
||||
private static TemplateRoute CreateRoute(IRouter target, string template)
|
||||
|
|
@ -533,7 +535,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
template,
|
||||
new RouteValueDictionary(),
|
||||
constraints: null,
|
||||
inlineConstraintResolver: new DefaultInlineConstraintResolver());
|
||||
inlineConstraintResolver: _inlineConstraintResolver);
|
||||
}
|
||||
|
||||
private static TemplateRoute CreateRoute(IRouter target, string template, object defaults)
|
||||
|
|
@ -542,7 +544,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
template,
|
||||
new RouteValueDictionary(defaults),
|
||||
constraints: null,
|
||||
inlineConstraintResolver: new DefaultInlineConstraintResolver());
|
||||
inlineConstraintResolver: _inlineConstraintResolver);
|
||||
}
|
||||
|
||||
private static IRouter CreateTarget(bool accept = true)
|
||||
|
|
@ -560,6 +562,13 @@ namespace Microsoft.AspNet.Routing.Template.Tests
|
|||
|
||||
return target.Object;
|
||||
}
|
||||
|
||||
private static IInlineConstraintResolver GetInlineConstraintResolver()
|
||||
{
|
||||
var resolverMock = new Mock<IInlineConstraintResolver>();
|
||||
resolverMock.Setup(o => o.ResolveConstraint("int")).Returns(new IntRouteConstraint());
|
||||
return resolverMock.Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue