#20 - Port Map and MapWhen. Move Run.
This commit is contained in:
parent
19d49b06a6
commit
5557b959c4
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using Microsoft.AspNet.Abstractions;
|
||||
using Microsoft.AspNet.Abstractions.Extensions;
|
||||
|
||||
namespace Microsoft.AspNet
|
||||
{
|
||||
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 IBuilder Map([NotNull] this IBuilder app, [NotNull] string pathMatch, [NotNull] Action<IBuilder> configuration)
|
||||
{
|
||||
return Map(app, new PathString(pathMatch), configuration);
|
||||
}
|
||||
|
||||
/// <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 IBuilder Map([NotNull] this IBuilder app, PathString pathMatch, [NotNull] Action<IBuilder> configuration)
|
||||
{
|
||||
if (pathMatch.HasValue && pathMatch.Value.EndsWith("/", StringComparison.Ordinal))
|
||||
{
|
||||
throw new ArgumentException("The path must not end with a '/'", "pathMatch");
|
||||
}
|
||||
|
||||
// create branch
|
||||
IBuilder branchBuilder = app.New();
|
||||
configuration(branchBuilder);
|
||||
var branch = branchBuilder.Build();
|
||||
|
||||
var options = new MapOptions()
|
||||
{
|
||||
Branch = branch,
|
||||
PathMatch = pathMatch,
|
||||
};
|
||||
return app.Use(next => new MapMiddleware(next, options).Invoke);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.Abstractions.Extensions
|
||||
{
|
||||
public class MapMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly MapOptions _options;
|
||||
|
||||
public MapMiddleware([NotNull] RequestDelegate next, [NotNull] MapOptions options)
|
||||
{
|
||||
_next = next;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public async Task Invoke([NotNull] HttpContext context)
|
||||
{
|
||||
PathString path = context.Request.Path;
|
||||
PathString remainingPath;
|
||||
if (path.StartsWithSegments(_options.PathMatch, out remainingPath))
|
||||
{
|
||||
// Update the path
|
||||
PathString pathBase = context.Request.PathBase;
|
||||
context.Request.PathBase = pathBase + _options.PathMatch;
|
||||
context.Request.Path = remainingPath;
|
||||
|
||||
await _options.Branch(context);
|
||||
|
||||
context.Request.PathBase = pathBase;
|
||||
context.Request.Path = path;
|
||||
}
|
||||
else
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
namespace Microsoft.AspNet.Abstractions.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Options for the Map middleware
|
||||
/// </summary>
|
||||
public class MapOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// The path to match
|
||||
/// </summary>
|
||||
public PathString PathMatch { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The branch taken for a positive match
|
||||
/// </summary>
|
||||
public RequestDelegate Branch { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Abstractions;
|
||||
using Microsoft.AspNet.Abstractions.Extensions;
|
||||
|
||||
namespace Microsoft.AspNet
|
||||
{
|
||||
using Predicate = Func<HttpContext, bool>;
|
||||
using PredicateAsync = Func<HttpContext, Task<bool>>;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for the MapWhenMiddleware
|
||||
/// </summary>
|
||||
public static class MapWhenExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Branches the request pipeline based on the result of the given predicate.
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="predicate">Invoked 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 IBuilder MapWhen([NotNull] this IBuilder app, [NotNull] Predicate predicate, [NotNull] Action<IBuilder> configuration)
|
||||
{
|
||||
// create branch
|
||||
IBuilder branchBuilder = app.New();
|
||||
configuration(branchBuilder);
|
||||
var branch = branchBuilder.Build();
|
||||
|
||||
// put middleware in pipeline
|
||||
var options = new MapWhenOptions
|
||||
{
|
||||
Predicate = predicate,
|
||||
Branch = branch,
|
||||
};
|
||||
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 IBuilder MapWhenAsync([NotNull] this IBuilder app, [NotNull] PredicateAsync predicate, [NotNull] Action<IBuilder> configuration)
|
||||
{
|
||||
// create branch
|
||||
IBuilder 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.Abstractions.Extensions
|
||||
{
|
||||
public class MapWhenMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly MapWhenOptions _options;
|
||||
|
||||
public MapWhenMiddleware([NotNull] RequestDelegate next, [NotNull] MapWhenOptions options)
|
||||
{
|
||||
_next = next;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public async Task Invoke([NotNull] HttpContext context)
|
||||
{
|
||||
if (_options.Predicate != null)
|
||||
{
|
||||
if (_options.Predicate(context))
|
||||
{
|
||||
await _options.Branch(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (await _options.PredicateAsync(context))
|
||||
{
|
||||
await _options.Branch(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.Abstractions.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Options for the MapWhen middleware
|
||||
/// </summary>
|
||||
public class MapWhenOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// The user callback that determines if the branch should be taken
|
||||
/// </summary>
|
||||
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>
|
||||
/// The branch taken for a positive match
|
||||
/// </summary>
|
||||
public RequestDelegate Branch { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using Microsoft.AspNet.Abstractions;
|
||||
|
||||
namespace Microsoft.AspNet
|
||||
{
|
||||
public static class RunExtensions
|
||||
{
|
||||
public static void Run([NotNull] this IBuilder app, [NotNull] RequestDelegate handler)
|
||||
{
|
||||
app.Use(_ => handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,6 @@ namespace Microsoft.AspNet.Abstractions
|
|||
IServerInformation Server { get; set; }
|
||||
|
||||
IBuilder Use(Func<RequestDelegate, RequestDelegate> middleware);
|
||||
IBuilder Run(RequestDelegate handler);
|
||||
|
||||
IBuilder New();
|
||||
RequestDelegate Build();
|
||||
|
|
|
|||
|
|
@ -21,6 +21,13 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CookieOptions.cs" />
|
||||
<Compile Include="Extensions\MapExtensions.cs" />
|
||||
<Compile Include="Extensions\MapMiddleware.cs" />
|
||||
<Compile Include="Extensions\MapOptions.cs" />
|
||||
<Compile Include="Extensions\MapWhenExtensions.cs" />
|
||||
<Compile Include="Extensions\MapWhenMiddleware.cs" />
|
||||
<Compile Include="Extensions\MapWhenOptions.cs" />
|
||||
<Compile Include="Extensions\RunExtensions.cs" />
|
||||
<Compile Include="HostString.cs" />
|
||||
<Compile Include="HttpContext.cs" />
|
||||
<Compile Include="HttpRequest.cs" />
|
||||
|
|
@ -31,6 +38,7 @@
|
|||
<Compile Include="IReadableStringCollection.cs" />
|
||||
<Compile Include="IResponseCookies.cs" />
|
||||
<Compile Include="IServerInformation.cs" />
|
||||
<Compile Include="NotNullAttribute.cs" />
|
||||
<Compile Include="PathString.cs" />
|
||||
<Compile Include="QueryString.cs" />
|
||||
<Compile Include="RequestDelegate.cs" />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
using System;
|
||||
|
||||
namespace Microsoft.AspNet.Abstractions
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
|
||||
internal sealed class NotNullAttribute : Attribute
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Abstractions;
|
||||
|
||||
namespace Microsoft.AspNet.PipelineCore
|
||||
|
|
@ -29,11 +30,6 @@ namespace Microsoft.AspNet.PipelineCore
|
|||
return this;
|
||||
}
|
||||
|
||||
public IBuilder Run(RequestDelegate handler)
|
||||
{
|
||||
return Use(next => handler);
|
||||
}
|
||||
|
||||
public IBuilder New()
|
||||
{
|
||||
return new Builder(this);
|
||||
|
|
@ -41,7 +37,11 @@ namespace Microsoft.AspNet.PipelineCore
|
|||
|
||||
public RequestDelegate Build()
|
||||
{
|
||||
RequestDelegate app = async context => context.Response.StatusCode = 404;
|
||||
RequestDelegate app = context =>
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
|
||||
foreach (var component in _components.Reverse())
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.AspNet.HttpFeature;
|
||||
|
||||
namespace Microsoft.AspNet.Abstractions.Extensions
|
||||
{
|
||||
public class FakeHttpRequestInfo : IHttpRequestInformation
|
||||
{
|
||||
public string Protocol { get; set; }
|
||||
public string Scheme { get; set; }
|
||||
public string Method { get; set; }
|
||||
public string PathBase { get; set; }
|
||||
public string Path { get; set; }
|
||||
public string QueryString { get; set; }
|
||||
public IDictionary<string, string[]> Headers { get; set; }
|
||||
public Stream Body { get; set; }
|
||||
}
|
||||
|
||||
public class FakeHttpResponseInfo : IHttpResponseInformation
|
||||
{
|
||||
public int StatusCode { get; set; }
|
||||
public string ReasonPhrase { get; set; }
|
||||
public IDictionary<string, string[]> Headers { get; set; }
|
||||
public Stream Body { get; set; }
|
||||
public void OnSendingHeaders(Action<object> callback, object state)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,196 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.HttpFeature;
|
||||
using Microsoft.AspNet.PipelineCore;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Abstractions.Extensions
|
||||
{
|
||||
public class MapPathMiddlewareTests
|
||||
{
|
||||
private static readonly Action<IBuilder> ActionNotImplemented = new Action<IBuilder>(_ => { throw new NotImplementedException(); });
|
||||
|
||||
private static Task Success(HttpContext context)
|
||||
{
|
||||
context.Response.StatusCode = 200;
|
||||
context.Items["test.PathBase"] = context.Request.PathBase.Value;
|
||||
context.Items["test.Path"] = context.Request.Path.Value;
|
||||
return Task.FromResult<object>(null);
|
||||
}
|
||||
|
||||
private static void UseSuccess(IBuilder app)
|
||||
{
|
||||
app.Run(Success);
|
||||
}
|
||||
|
||||
private static Task NotImplemented(HttpContext context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private static void UseNotImplemented(IBuilder app)
|
||||
{
|
||||
app.Run(NotImplemented);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NullArguments_ArgumentNullException()
|
||||
{
|
||||
var builder = new Builder(serviceProvider: null);
|
||||
var noMiddleware = new Builder(serviceProvider: null).Build();
|
||||
var noOptions = new MapOptions();
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => builder.Map(null, ActionNotImplemented));
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => builder.Map("/foo", (Action<IBuilder>)null));
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => new MapMiddleware(null, noOptions));
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => new MapMiddleware(noMiddleware, null));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/foo", "", "/foo")]
|
||||
[InlineData("/foo", "", "/foo/")]
|
||||
[InlineData("/foo", "/Bar", "/foo")]
|
||||
[InlineData("/foo", "/Bar", "/foo/cho")]
|
||||
[InlineData("/foo", "/Bar", "/foo/cho/")]
|
||||
[InlineData("/foo/cho", "/Bar", "/foo/cho")]
|
||||
[InlineData("/foo/cho", "/Bar", "/foo/cho/do")]
|
||||
public void PathMatchFunc_BranchTaken(string matchPath, string basePath, string requestPath)
|
||||
{
|
||||
HttpContext context = CreateRequest(basePath, requestPath);
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.Map(matchPath, UseSuccess);
|
||||
var app = builder.Build();
|
||||
app.Invoke(context).Wait();
|
||||
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
Assert.Equal(basePath, context.Request.PathBase.Value);
|
||||
Assert.Equal(requestPath, context.Request.Path.Value);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/foo", "", "/foo")]
|
||||
[InlineData("/foo", "", "/foo/")]
|
||||
[InlineData("/foo", "/Bar", "/foo")]
|
||||
[InlineData("/foo", "/Bar", "/foo/cho")]
|
||||
[InlineData("/foo", "/Bar", "/foo/cho/")]
|
||||
[InlineData("/foo/cho", "/Bar", "/foo/cho")]
|
||||
[InlineData("/foo/cho", "/Bar", "/foo/cho/do")]
|
||||
public void PathMatchAction_BranchTaken(string matchPath, string basePath, string requestPath)
|
||||
{
|
||||
HttpContext context = CreateRequest(basePath, requestPath);
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.Map(matchPath, subBuilder => subBuilder.Run(Success));
|
||||
var app = builder.Build();
|
||||
app.Invoke(context).Wait();
|
||||
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
Assert.Equal(basePath + matchPath, context.Items["test.PathBase"]);
|
||||
Assert.Equal(requestPath.Substring(matchPath.Length), context.Items["test.Path"]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/")]
|
||||
[InlineData("/foo/")]
|
||||
[InlineData("/foo/cho/")]
|
||||
public void MatchPathWithTrailingSlashThrowsException(string matchPath)
|
||||
{
|
||||
Should.Throw<ArgumentException>(() => new Builder(serviceProvider: null).Map(matchPath, map => { }).Build());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/foo", "", "")]
|
||||
[InlineData("/foo", "/bar", "")]
|
||||
[InlineData("/foo", "", "/bar")]
|
||||
[InlineData("/foo", "/foo", "")]
|
||||
[InlineData("/foo", "/foo", "/bar")]
|
||||
[InlineData("/foo", "", "/bar/foo")]
|
||||
[InlineData("/foo/bar", "/foo", "/bar")]
|
||||
public void PathMismatchFunc_PassedThrough(string matchPath, string basePath, string requestPath)
|
||||
{
|
||||
HttpContext context = CreateRequest(basePath, requestPath);
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.Map(matchPath, UseNotImplemented);
|
||||
builder.Run(Success);
|
||||
var app = builder.Build();
|
||||
app.Invoke(context).Wait();
|
||||
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
Assert.Equal(basePath, context.Request.PathBase.Value);
|
||||
Assert.Equal(requestPath, context.Request.Path.Value);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/foo", "", "")]
|
||||
[InlineData("/foo", "/bar", "")]
|
||||
[InlineData("/foo", "", "/bar")]
|
||||
[InlineData("/foo", "/foo", "")]
|
||||
[InlineData("/foo", "/foo", "/bar")]
|
||||
[InlineData("/foo", "", "/bar/foo")]
|
||||
[InlineData("/foo/bar", "/foo", "/bar")]
|
||||
public void PathMismatchAction_PassedThrough(string matchPath, string basePath, string requestPath)
|
||||
{
|
||||
HttpContext context = CreateRequest(basePath, requestPath);
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.Map(matchPath, UseNotImplemented);
|
||||
builder.Run(Success);
|
||||
var app = builder.Build();
|
||||
app.Invoke(context).Wait();
|
||||
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
Assert.Equal(basePath, context.Request.PathBase.Value);
|
||||
Assert.Equal(requestPath, context.Request.Path.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ChainedRoutes_Success()
|
||||
{
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.Map("/route1", map =>
|
||||
{
|
||||
map.Map((string)"/subroute1", UseSuccess);
|
||||
map.Run(NotImplemented);
|
||||
});
|
||||
builder.Map("/route2/subroute2", UseSuccess);
|
||||
var app = builder.Build();
|
||||
|
||||
HttpContext context = CreateRequest(string.Empty, "/route1");
|
||||
Assert.Throws<AggregateException>(() => app.Invoke(context).Wait());
|
||||
|
||||
context = CreateRequest(string.Empty, "/route1/subroute1");
|
||||
app.Invoke(context).Wait();
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
Assert.Equal(string.Empty, context.Request.PathBase.Value);
|
||||
Assert.Equal("/route1/subroute1", context.Request.Path.Value);
|
||||
|
||||
context = CreateRequest(string.Empty, "/route2");
|
||||
app.Invoke(context).Wait();
|
||||
Assert.Equal(404, context.Response.StatusCode);
|
||||
Assert.Equal(string.Empty, context.Request.PathBase.Value);
|
||||
Assert.Equal("/route2", context.Request.Path.Value);
|
||||
|
||||
context = CreateRequest(string.Empty, "/route2/subroute2");
|
||||
app.Invoke(context).Wait();
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
Assert.Equal(string.Empty, context.Request.PathBase.Value);
|
||||
Assert.Equal("/route2/subroute2", context.Request.Path.Value);
|
||||
|
||||
context = CreateRequest(string.Empty, "/route2/subroute2/subsub2");
|
||||
app.Invoke(context).Wait();
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
Assert.Equal(string.Empty, context.Request.PathBase.Value);
|
||||
Assert.Equal("/route2/subroute2/subsub2", context.Request.Path.Value);
|
||||
}
|
||||
|
||||
private HttpContext CreateRequest(string basePath, string requestPath)
|
||||
{
|
||||
HttpContext context = new DefaultHttpContext(new FeatureModel.FeatureCollection());
|
||||
context.SetFeature<IHttpRequestInformation>(new FakeHttpRequestInfo());
|
||||
context.SetFeature<IHttpResponseInformation>(new FakeHttpResponseInfo());
|
||||
context.Request.PathBase = new PathString(basePath);
|
||||
context.Request.Path = new PathString(requestPath);
|
||||
return context;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,183 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.HttpFeature;
|
||||
using Microsoft.AspNet.PipelineCore;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Abstractions.Extensions
|
||||
{
|
||||
using AppFunc = Func<IDictionary<string, object>, Task>;
|
||||
using Predicate = Func<HttpContext, bool>;
|
||||
using PredicateAsync = Func<HttpContext, Task<bool>>;
|
||||
|
||||
public class MapPredicateMiddlewareTests
|
||||
{
|
||||
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)
|
||||
{
|
||||
context.Response.StatusCode = 200;
|
||||
return Task.FromResult<object>(null);
|
||||
}
|
||||
|
||||
private static void UseSuccess(IBuilder app)
|
||||
{
|
||||
app.Run(Success);
|
||||
}
|
||||
|
||||
private static Task NotImplemented(HttpContext context)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private static void UseNotImplemented(IBuilder app)
|
||||
{
|
||||
app.Run(NotImplemented);
|
||||
}
|
||||
|
||||
private bool TruePredicate(HttpContext context)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool FalsePredicate(HttpContext context)
|
||||
{
|
||||
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]
|
||||
public void NullArguments_ArgumentNullException()
|
||||
{
|
||||
var builder = new Builder(serviceProvider: null);
|
||||
var noMiddleware = new Builder(serviceProvider: null).Build();
|
||||
var noOptions = new MapWhenOptions();
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => builder.MapWhen(null, UseNotImplemented));
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => builder.MapWhen(NotImplementedPredicate, (Action<IBuilder>)null));
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(null, noOptions));
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null));
|
||||
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => builder.MapWhenAsync(null, UseNotImplemented));
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => builder.MapWhenAsync(NotImplementedPredicateAsync, (Action<IBuilder>)null));
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(null, noOptions));
|
||||
// TODO: [NotNull] Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PredicateTrue_BranchTaken()
|
||||
{
|
||||
HttpContext context = CreateRequest();
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.MapWhen(TruePredicate, UseSuccess);
|
||||
var app = builder.Build();
|
||||
app.Invoke(context).Wait();
|
||||
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PredicateTrueAction_BranchTaken()
|
||||
{
|
||||
HttpContext context = CreateRequest();
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.MapWhen(TruePredicate, UseSuccess);
|
||||
var app = builder.Build();
|
||||
app.Invoke(context).Wait();
|
||||
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PredicateFalseAction_PassThrough()
|
||||
{
|
||||
HttpContext context = CreateRequest();
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.MapWhen(FalsePredicate, UseNotImplemented);
|
||||
builder.Run(Success);
|
||||
var app = builder.Build();
|
||||
app.Invoke(context).Wait();
|
||||
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PredicateAsyncTrueAction_BranchTaken()
|
||||
{
|
||||
HttpContext context = CreateRequest();
|
||||
IBuilder builder = new Builder(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();
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.MapWhenAsync(FalsePredicateAsync, UseNotImplemented);
|
||||
builder.Run(Success);
|
||||
var app = builder.Build();
|
||||
app.Invoke(context).Wait();
|
||||
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ChainedPredicates_Success()
|
||||
{
|
||||
IBuilder builder = new Builder(serviceProvider: null);
|
||||
builder.MapWhen(TruePredicate, map1 =>
|
||||
{
|
||||
map1.MapWhen((Predicate)FalsePredicate, UseNotImplemented);
|
||||
map1.MapWhen((Predicate)TruePredicate, map2 => map2.MapWhen((Predicate)TruePredicate, UseSuccess));
|
||||
map1.Run(NotImplemented);
|
||||
});
|
||||
var app = builder.Build();
|
||||
|
||||
HttpContext context = CreateRequest();
|
||||
app.Invoke(context).Wait();
|
||||
Assert.Equal(200, context.Response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ChainedPredicatesAsync_Success()
|
||||
{
|
||||
IBuilder builder = new Builder(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()
|
||||
{
|
||||
HttpContext context = new DefaultHttpContext(new FeatureModel.FeatureCollection());
|
||||
context.SetFeature<IHttpRequestInformation>(new FakeHttpRequestInfo());
|
||||
context.SetFeature<IHttpResponseInformation>(new FakeHttpResponseInfo());
|
||||
return context;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,9 @@
|
|||
<Content Include="Project.json" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Fakes.cs" />
|
||||
<Compile Include="MapPathMiddlewareTests.cs" />
|
||||
<Compile Include="MapPredicateMiddlewareTests.cs" />
|
||||
<Compile Include="PathStringTests.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\ProjectK\Microsoft.Web.ProjectK.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
"version": "0.1-alpha-*",
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.Abstractions": "",
|
||||
"Microsoft.AspNet.PipelineCore": "",
|
||||
"Microsoft.AspNet.HttpFeature": "",
|
||||
"Microsoft.AspNet.Testing": "0.1-alpha-*",
|
||||
"Xunit.KRunner": "0.1-alpha-*",
|
||||
"xunit.abstractions": "2.0.0-aspnet-*",
|
||||
|
|
|
|||
Loading…
Reference in New Issue