#265 Remove some overloads for Run, Map, and MapWhen.
This commit is contained in:
parent
43c3913b86
commit
7d7cd5fde7
|
|
@ -10,19 +10,6 @@ namespace Microsoft.AspNet.Builder
|
||||||
{
|
{
|
||||||
public static class MapExtensions
|
public static class MapExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// If the request path starts with the given pathMatch, execute the app configured via configuration parameter instead of
|
|
||||||
/// continuing to the next component in the pipeline.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="app"></param>
|
|
||||||
/// <param name="pathMatch">The path to match</param>
|
|
||||||
/// <param name="configuration">The branch to take for positive path matches</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IApplicationBuilder Map([NotNull] this IApplicationBuilder app, [NotNull] string pathMatch, [NotNull] Action<IApplicationBuilder> configuration)
|
|
||||||
{
|
|
||||||
return Map(app, new PathString(pathMatch), configuration);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// If the request path starts with the given pathMatch, execute the app configured via configuration parameter instead of
|
/// If the request path starts with the given pathMatch, execute the app configured via configuration parameter instead of
|
||||||
/// continuing to the next component in the pipeline.
|
/// continuing to the next component in the pipeline.
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,11 @@ using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Builder.Extensions;
|
using Microsoft.AspNet.Builder.Extensions;
|
||||||
|
using Microsoft.Framework.Internal;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Builder
|
namespace Microsoft.AspNet.Builder
|
||||||
{
|
{
|
||||||
using Microsoft.Framework.Internal;
|
|
||||||
using Predicate = Func<HttpContext, bool>;
|
using Predicate = Func<HttpContext, bool>;
|
||||||
using PredicateAsync = Func<HttpContext, Task<bool>>;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extension methods for the MapWhenMiddleware
|
/// Extension methods for the MapWhenMiddleware
|
||||||
|
|
@ -39,28 +38,5 @@ namespace Microsoft.AspNet.Builder
|
||||||
};
|
};
|
||||||
return app.Use(next => new MapWhenMiddleware(next, options).Invoke);
|
return app.Use(next => new MapWhenMiddleware(next, options).Invoke);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Branches the request pipeline based on the async result of the given predicate.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="app"></param>
|
|
||||||
/// <param name="predicate">Invoked asynchronously with the request environment to determine if the branch should be taken</param>
|
|
||||||
/// <param name="configuration">Configures a branch to take</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IApplicationBuilder MapWhenAsync([NotNull] this IApplicationBuilder app, [NotNull] PredicateAsync predicate, [NotNull] Action<IApplicationBuilder> configuration)
|
|
||||||
{
|
|
||||||
// create branch
|
|
||||||
var branchBuilder = app.New();
|
|
||||||
configuration(branchBuilder);
|
|
||||||
var branch = branchBuilder.Build();
|
|
||||||
|
|
||||||
// put middleware in pipeline
|
|
||||||
var options = new MapWhenOptions
|
|
||||||
{
|
|
||||||
PredicateAsync = predicate,
|
|
||||||
Branch = branch,
|
|
||||||
};
|
|
||||||
return app.Use(next => new MapWhenMiddleware(next, options).Invoke);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -20,27 +20,13 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
|
|
||||||
public async Task Invoke([NotNull] HttpContext context)
|
public async Task Invoke([NotNull] HttpContext context)
|
||||||
{
|
{
|
||||||
if (_options.Predicate != null)
|
if (_options.Predicate(context))
|
||||||
{
|
{
|
||||||
if (_options.Predicate(context))
|
await _options.Branch(context);
|
||||||
{
|
|
||||||
await _options.Branch(context);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
await _next(context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (await _options.PredicateAsync(context))
|
await _next(context);
|
||||||
{
|
|
||||||
await _options.Branch(context);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
await _next(context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,6 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Func<HttpContext, bool> Predicate { get; set; }
|
public Func<HttpContext, bool> Predicate { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The async user callback that determines if the branch should be taken
|
|
||||||
/// </summary>
|
|
||||||
public Func<HttpContext, Task<bool>> PredicateAsync { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The branch taken for a positive match
|
/// The branch taken for a positive match
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -14,25 +14,5 @@ namespace Microsoft.AspNet.Builder
|
||||||
{
|
{
|
||||||
app.Use(_ => handler);
|
app.Use(_ => handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Run<TService1>(this IApplicationBuilder app, Func<HttpContext, TService1, Task> handler)
|
|
||||||
{
|
|
||||||
app.Use<TService1>((ctx, _, s1) => handler(ctx, s1));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Run<TService1, TService2>(this IApplicationBuilder app, Func<HttpContext, TService1, TService2, Task> handler)
|
|
||||||
{
|
|
||||||
app.Use<TService1, TService2>((ctx, _, s1, s2) => handler(ctx, s1, s2));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Run<TService1, TService2, TService3>(this IApplicationBuilder app, Func<HttpContext, TService1, TService2, TService3, Task> handler)
|
|
||||||
{
|
|
||||||
app.Use<TService1, TService2, TService3>((ctx, _, s1, s2, s3) => handler(ctx, s1, s2, s3));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Run<TService1, TService2, TService3, TService4>(this IApplicationBuilder app, Func<HttpContext, TService1, TService2, TService3, TService4, Task> handler)
|
|
||||||
{
|
|
||||||
app.Use<TService1, TService2, TService3, TService4>((ctx, _, s1, s2, s3, s4) => handler(ctx, s1, s2, s3, s4));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -60,7 +60,7 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
{
|
{
|
||||||
HttpContext context = CreateRequest(basePath, requestPath);
|
HttpContext context = CreateRequest(basePath, requestPath);
|
||||||
var builder = new ApplicationBuilder(serviceProvider: null);
|
var builder = new ApplicationBuilder(serviceProvider: null);
|
||||||
builder.Map(matchPath, UseSuccess);
|
builder.Map(new PathString(matchPath), UseSuccess);
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
app.Invoke(context).Wait();
|
app.Invoke(context).Wait();
|
||||||
|
|
||||||
|
|
@ -81,7 +81,7 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
{
|
{
|
||||||
HttpContext context = CreateRequest(basePath, requestPath);
|
HttpContext context = CreateRequest(basePath, requestPath);
|
||||||
var builder = new ApplicationBuilder(serviceProvider: null);
|
var builder = new ApplicationBuilder(serviceProvider: null);
|
||||||
builder.Map(matchPath, subBuilder => subBuilder.Run(Success));
|
builder.Map(new PathString(matchPath), subBuilder => subBuilder.Run(Success));
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
app.Invoke(context).Wait();
|
app.Invoke(context).Wait();
|
||||||
|
|
||||||
|
|
@ -96,7 +96,7 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
[InlineData("/foo/cho/")]
|
[InlineData("/foo/cho/")]
|
||||||
public void MatchPathWithTrailingSlashThrowsException(string matchPath)
|
public void MatchPathWithTrailingSlashThrowsException(string matchPath)
|
||||||
{
|
{
|
||||||
Should.Throw<ArgumentException>(() => new ApplicationBuilder(serviceProvider: null).Map(matchPath, map => { }).Build());
|
Should.Throw<ArgumentException>(() => new ApplicationBuilder(serviceProvider: null).Map(new PathString(matchPath), map => { }).Build());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
|
@ -111,7 +111,7 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
{
|
{
|
||||||
HttpContext context = CreateRequest(basePath, requestPath);
|
HttpContext context = CreateRequest(basePath, requestPath);
|
||||||
var builder = new ApplicationBuilder(serviceProvider: null);
|
var builder = new ApplicationBuilder(serviceProvider: null);
|
||||||
builder.Map(matchPath, UseNotImplemented);
|
builder.Map(new PathString(matchPath), UseNotImplemented);
|
||||||
builder.Run(Success);
|
builder.Run(Success);
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
app.Invoke(context).Wait();
|
app.Invoke(context).Wait();
|
||||||
|
|
@ -133,7 +133,7 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
{
|
{
|
||||||
HttpContext context = CreateRequest(basePath, requestPath);
|
HttpContext context = CreateRequest(basePath, requestPath);
|
||||||
var builder = new ApplicationBuilder(serviceProvider: null);
|
var builder = new ApplicationBuilder(serviceProvider: null);
|
||||||
builder.Map(matchPath, UseNotImplemented);
|
builder.Map(new PathString(matchPath), UseNotImplemented);
|
||||||
builder.Run(Success);
|
builder.Run(Success);
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
app.Invoke(context).Wait();
|
app.Invoke(context).Wait();
|
||||||
|
|
@ -147,12 +147,12 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
public void ChainedRoutes_Success()
|
public void ChainedRoutes_Success()
|
||||||
{
|
{
|
||||||
var builder = new ApplicationBuilder(serviceProvider: null);
|
var builder = new ApplicationBuilder(serviceProvider: null);
|
||||||
builder.Map("/route1", map =>
|
builder.Map(new PathString("/route1"), map =>
|
||||||
{
|
{
|
||||||
map.Map((string)"/subroute1", UseSuccess);
|
map.Map(new PathString("/subroute1"), UseSuccess);
|
||||||
map.Run(NotImplemented);
|
map.Run(NotImplemented);
|
||||||
});
|
});
|
||||||
builder.Map("/route2/subroute2", UseSuccess);
|
builder.Map(new PathString("/route2/subroute2"), UseSuccess);
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
HttpContext context = CreateRequest(string.Empty, "/route1");
|
HttpContext context = CreateRequest(string.Empty, "/route1");
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
public class MapPredicateMiddlewareTests
|
public class MapPredicateMiddlewareTests
|
||||||
{
|
{
|
||||||
private static readonly Predicate NotImplementedPredicate = new Predicate(envionment => { throw new NotImplementedException(); });
|
private static readonly Predicate NotImplementedPredicate = new Predicate(envionment => { throw new NotImplementedException(); });
|
||||||
private static readonly PredicateAsync NotImplementedPredicateAsync = new PredicateAsync(envionment => { throw new NotImplementedException(); });
|
|
||||||
|
|
||||||
private static Task Success(HttpContext context)
|
private static Task Success(HttpContext context)
|
||||||
{
|
{
|
||||||
|
|
@ -49,16 +48,6 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<bool> TruePredicateAsync(HttpContext context)
|
|
||||||
{
|
|
||||||
return Task.FromResult<bool>(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Task<bool> FalsePredicateAsync(HttpContext context)
|
|
||||||
{
|
|
||||||
return Task.FromResult<bool>(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void NullArguments_ArgumentNullException()
|
public void NullArguments_ArgumentNullException()
|
||||||
{
|
{
|
||||||
|
|
@ -113,31 +102,6 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
Assert.Equal(200, context.Response.StatusCode);
|
Assert.Equal(200, context.Response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void PredicateAsyncTrueAction_BranchTaken()
|
|
||||||
{
|
|
||||||
HttpContext context = CreateRequest();
|
|
||||||
var builder = new ApplicationBuilder(serviceProvider: null);
|
|
||||||
builder.MapWhenAsync(TruePredicateAsync, UseSuccess);
|
|
||||||
var app = builder.Build();
|
|
||||||
app.Invoke(context).Wait();
|
|
||||||
|
|
||||||
Assert.Equal(200, context.Response.StatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void PredicateAsyncFalseAction_PassThrough()
|
|
||||||
{
|
|
||||||
HttpContext context = CreateRequest();
|
|
||||||
var builder = new ApplicationBuilder(serviceProvider: null);
|
|
||||||
builder.MapWhenAsync(FalsePredicateAsync, UseNotImplemented);
|
|
||||||
builder.Run(Success);
|
|
||||||
var app = builder.Build();
|
|
||||||
app.Invoke(context).Wait();
|
|
||||||
|
|
||||||
Assert.Equal(200, context.Response.StatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ChainedPredicates_Success()
|
public void ChainedPredicates_Success()
|
||||||
{
|
{
|
||||||
|
|
@ -155,23 +119,6 @@ namespace Microsoft.AspNet.Builder.Extensions
|
||||||
Assert.Equal(200, context.Response.StatusCode);
|
Assert.Equal(200, context.Response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ChainedPredicatesAsync_Success()
|
|
||||||
{
|
|
||||||
var builder = new ApplicationBuilder(serviceProvider: null);
|
|
||||||
builder.MapWhenAsync(TruePredicateAsync, map1 =>
|
|
||||||
{
|
|
||||||
map1.MapWhenAsync((PredicateAsync)FalsePredicateAsync, UseNotImplemented);
|
|
||||||
map1.MapWhenAsync((PredicateAsync)TruePredicateAsync, map2 => map2.MapWhenAsync((PredicateAsync)TruePredicateAsync, UseSuccess));
|
|
||||||
map1.Run(NotImplemented);
|
|
||||||
});
|
|
||||||
var app = builder.Build();
|
|
||||||
|
|
||||||
HttpContext context = CreateRequest();
|
|
||||||
app.Invoke(context).Wait();
|
|
||||||
Assert.Equal(200, context.Response.StatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
private HttpContext CreateRequest()
|
private HttpContext CreateRequest()
|
||||||
{
|
{
|
||||||
HttpContext context = new DefaultHttpContext();
|
HttpContext context = new DefaultHttpContext();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue