From 43c3913b86e60f6fd5dbfa1e0a4d72541909f4ee Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Fri, 17 Apr 2015 12:18:09 -0700 Subject: [PATCH 1/4] #265 Remove setters for IApplcationBuilder.Properties and Server. --- .../IApplicationBuilder.cs | 4 ++-- src/Microsoft.AspNet.Http/ApplicationBuilder.cs | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.AspNet.Http.Core/IApplicationBuilder.cs b/src/Microsoft.AspNet.Http.Core/IApplicationBuilder.cs index cfecfd97..79e11d5a 100644 --- a/src/Microsoft.AspNet.Http.Core/IApplicationBuilder.cs +++ b/src/Microsoft.AspNet.Http.Core/IApplicationBuilder.cs @@ -10,9 +10,9 @@ public interface IApplicationBuilder { IServiceProvider ApplicationServices { get; set; } - object Server { get; set; } + object Server { get; } - IDictionary Properties { get; set; } + IDictionary Properties { get; } IApplicationBuilder Use(Func middleware); diff --git a/src/Microsoft.AspNet.Http/ApplicationBuilder.cs b/src/Microsoft.AspNet.Http/ApplicationBuilder.cs index e7a00106..3a284eb5 100644 --- a/src/Microsoft.AspNet.Http/ApplicationBuilder.cs +++ b/src/Microsoft.AspNet.Http/ApplicationBuilder.cs @@ -19,6 +19,12 @@ public ApplicationBuilder(IServiceProvider serviceProvider) ApplicationServices = serviceProvider; } + public ApplicationBuilder(IServiceProvider serviceProvider, object server) + : this(serviceProvider) + { + SetProperty(Constants.BuilderProperties.ServerInformation, server); + } + private ApplicationBuilder(ApplicationBuilder builder) { Properties = builder.Properties; @@ -42,13 +48,9 @@ public object Server { return GetProperty(Constants.BuilderProperties.ServerInformation); } - set - { - SetProperty(Constants.BuilderProperties.ServerInformation, value); - } } - public IDictionary Properties { get; set; } + public IDictionary Properties { get; } private T GetProperty(string key) { From 7d7cd5fde7a6679149e0197105c18fb9e9b1e1f4 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Fri, 17 Apr 2015 12:28:02 -0700 Subject: [PATCH 2/4] #265 Remove some overloads for Run, Map, and MapWhen. --- .../Extensions/MapExtensions.cs | 13 ----- .../Extensions/MapWhenExtensions.cs | 26 +-------- .../Extensions/MapWhenMiddleware.cs | 20 ++----- .../Extensions/MapWhenOptions.cs | 5 -- .../Extensions/RunExtensions.cs | 20 ------- .../MapPathMiddlewareTests.cs | 16 +++--- .../MapPredicateMiddlewareTests.cs | 53 ------------------- 7 files changed, 12 insertions(+), 141 deletions(-) diff --git a/src/Microsoft.AspNet.Http.Core/Extensions/MapExtensions.cs b/src/Microsoft.AspNet.Http.Core/Extensions/MapExtensions.cs index 5f71a1b7..3a53395f 100644 --- a/src/Microsoft.AspNet.Http.Core/Extensions/MapExtensions.cs +++ b/src/Microsoft.AspNet.Http.Core/Extensions/MapExtensions.cs @@ -10,19 +10,6 @@ namespace Microsoft.AspNet.Builder { public static class MapExtensions { - /// - /// 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. - /// - /// - /// The path to match - /// The branch to take for positive path matches - /// - public static IApplicationBuilder Map([NotNull] this IApplicationBuilder app, [NotNull] string pathMatch, [NotNull] Action configuration) - { - return Map(app, new PathString(pathMatch), configuration); - } - /// /// 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. diff --git a/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenExtensions.cs b/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenExtensions.cs index 117b8cac..30564a11 100644 --- a/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenExtensions.cs +++ b/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenExtensions.cs @@ -5,12 +5,11 @@ using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Builder.Extensions; +using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Builder { - using Microsoft.Framework.Internal; using Predicate = Func; - using PredicateAsync = Func>; /// /// Extension methods for the MapWhenMiddleware @@ -39,28 +38,5 @@ public static IApplicationBuilder MapWhen([NotNull] this IApplicationBuilder app }; return app.Use(next => new MapWhenMiddleware(next, options).Invoke); } - - /// - /// Branches the request pipeline based on the async result of the given predicate. - /// - /// - /// Invoked asynchronously with the request environment to determine if the branch should be taken - /// Configures a branch to take - /// - public static IApplicationBuilder MapWhenAsync([NotNull] this IApplicationBuilder app, [NotNull] PredicateAsync predicate, [NotNull] Action 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); - } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenMiddleware.cs b/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenMiddleware.cs index b4f5c78b..ab0e2d3d 100644 --- a/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenMiddleware.cs +++ b/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenMiddleware.cs @@ -20,27 +20,13 @@ public MapWhenMiddleware([NotNull] RequestDelegate next, [NotNull] MapWhenOption public async Task Invoke([NotNull] HttpContext context) { - if (_options.Predicate != null) + if (_options.Predicate(context)) { - if (_options.Predicate(context)) - { - await _options.Branch(context); - } - else - { - await _next(context); - } + await _options.Branch(context); } else { - if (await _options.PredicateAsync(context)) - { - await _options.Branch(context); - } - else - { - await _next(context); - } + await _next(context); } } } diff --git a/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenOptions.cs b/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenOptions.cs index c94b15f1..27a9d9b1 100644 --- a/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenOptions.cs +++ b/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenOptions.cs @@ -17,11 +17,6 @@ public class MapWhenOptions /// public Func Predicate { get; set; } - /// - /// The async user callback that determines if the branch should be taken - /// - public Func> PredicateAsync { get; set; } - /// /// The branch taken for a positive match /// diff --git a/src/Microsoft.AspNet.Http.Core/Extensions/RunExtensions.cs b/src/Microsoft.AspNet.Http.Core/Extensions/RunExtensions.cs index c0cb5af2..ad103c3d 100644 --- a/src/Microsoft.AspNet.Http.Core/Extensions/RunExtensions.cs +++ b/src/Microsoft.AspNet.Http.Core/Extensions/RunExtensions.cs @@ -14,25 +14,5 @@ public static void Run([NotNull] this IApplicationBuilder app, [NotNull] Request { app.Use(_ => handler); } - - public static void Run(this IApplicationBuilder app, Func handler) - { - app.Use((ctx, _, s1) => handler(ctx, s1)); - } - - public static void Run(this IApplicationBuilder app, Func handler) - { - app.Use((ctx, _, s1, s2) => handler(ctx, s1, s2)); - } - - public static void Run(this IApplicationBuilder app, Func handler) - { - app.Use((ctx, _, s1, s2, s3) => handler(ctx, s1, s2, s3)); - } - - public static void Run(this IApplicationBuilder app, Func handler) - { - app.Use((ctx, _, s1, s2, s3, s4) => handler(ctx, s1, s2, s3, s4)); - } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs b/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs index e0d7b306..071e8fbd 100644 --- a/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs @@ -60,7 +60,7 @@ public void PathMatchFunc_BranchTaken(string matchPath, string basePath, string { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(matchPath, UseSuccess); + builder.Map(new PathString(matchPath), UseSuccess); var app = builder.Build(); app.Invoke(context).Wait(); @@ -81,7 +81,7 @@ public void PathMatchAction_BranchTaken(string matchPath, string basePath, strin { HttpContext context = CreateRequest(basePath, requestPath); 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(); app.Invoke(context).Wait(); @@ -96,7 +96,7 @@ public void PathMatchAction_BranchTaken(string matchPath, string basePath, strin [InlineData("/foo/cho/")] public void MatchPathWithTrailingSlashThrowsException(string matchPath) { - Should.Throw(() => new ApplicationBuilder(serviceProvider: null).Map(matchPath, map => { }).Build()); + Should.Throw(() => new ApplicationBuilder(serviceProvider: null).Map(new PathString(matchPath), map => { }).Build()); } [Theory] @@ -111,7 +111,7 @@ public void PathMismatchFunc_PassedThrough(string matchPath, string basePath, st { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(matchPath, UseNotImplemented); + builder.Map(new PathString(matchPath), UseNotImplemented); builder.Run(Success); var app = builder.Build(); app.Invoke(context).Wait(); @@ -133,7 +133,7 @@ public void PathMismatchAction_PassedThrough(string matchPath, string basePath, { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(matchPath, UseNotImplemented); + builder.Map(new PathString(matchPath), UseNotImplemented); builder.Run(Success); var app = builder.Build(); app.Invoke(context).Wait(); @@ -147,12 +147,12 @@ public void PathMismatchAction_PassedThrough(string matchPath, string basePath, public void ChainedRoutes_Success() { 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); }); - builder.Map("/route2/subroute2", UseSuccess); + builder.Map(new PathString("/route2/subroute2"), UseSuccess); var app = builder.Build(); HttpContext context = CreateRequest(string.Empty, "/route1"); diff --git a/test/Microsoft.AspNet.Http.Core.Tests/MapPredicateMiddlewareTests.cs b/test/Microsoft.AspNet.Http.Core.Tests/MapPredicateMiddlewareTests.cs index fef12361..29b6eac3 100644 --- a/test/Microsoft.AspNet.Http.Core.Tests/MapPredicateMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Http.Core.Tests/MapPredicateMiddlewareTests.cs @@ -16,7 +16,6 @@ namespace Microsoft.AspNet.Builder.Extensions 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) { @@ -49,16 +48,6 @@ private bool FalsePredicate(HttpContext context) return false; } - private Task TruePredicateAsync(HttpContext context) - { - return Task.FromResult(true); - } - - private Task FalsePredicateAsync(HttpContext context) - { - return Task.FromResult(false); - } - [Fact] public void NullArguments_ArgumentNullException() { @@ -113,31 +102,6 @@ public void PredicateFalseAction_PassThrough() 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] public void ChainedPredicates_Success() { @@ -155,23 +119,6 @@ public void ChainedPredicates_Success() 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() { HttpContext context = new DefaultHttpContext(); From 4030be585db1620a59e4105cbcae214351b3a6fb Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Mon, 20 Apr 2015 10:54:33 -0700 Subject: [PATCH 3/4] #265 Add implicit converters between string and PathString. --- src/Microsoft.AspNet.Http.Core/PathString.cs | 18 ++++++++++++++++++ .../MapPathMiddlewareTests.cs | 16 ++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.AspNet.Http.Core/PathString.cs b/src/Microsoft.AspNet.Http.Core/PathString.cs index 5e6f03ec..11249afb 100644 --- a/src/Microsoft.AspNet.Http.Core/PathString.cs +++ b/src/Microsoft.AspNet.Http.Core/PathString.cs @@ -236,5 +236,23 @@ public override int GetHashCode() { return left.Add(right); } + + /// + /// Implicitly creates a new PathString from the given string. + /// + /// + public static implicit operator PathString(string s) + { + return new PathString(s); + } + + /// + /// Implicitly calls ToString(). + /// + /// + public static implicit operator string(PathString path) + { + return path.ToString(); + } } } diff --git a/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs b/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs index 071e8fbd..a54a89d0 100644 --- a/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Http.Core.Tests/MapPathMiddlewareTests.cs @@ -60,7 +60,7 @@ public void PathMatchFunc_BranchTaken(string matchPath, string basePath, string { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString(matchPath), UseSuccess); + builder.Map(matchPath, UseSuccess); var app = builder.Build(); app.Invoke(context).Wait(); @@ -81,7 +81,7 @@ public void PathMatchAction_BranchTaken(string matchPath, string basePath, strin { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString(matchPath), subBuilder => subBuilder.Run(Success)); + builder.Map(matchPath, subBuilder => subBuilder.Run(Success)); var app = builder.Build(); app.Invoke(context).Wait(); @@ -96,7 +96,7 @@ public void PathMatchAction_BranchTaken(string matchPath, string basePath, strin [InlineData("/foo/cho/")] public void MatchPathWithTrailingSlashThrowsException(string matchPath) { - Should.Throw(() => new ApplicationBuilder(serviceProvider: null).Map(new PathString(matchPath), map => { }).Build()); + Should.Throw(() => new ApplicationBuilder(serviceProvider: null).Map(matchPath, map => { }).Build()); } [Theory] @@ -111,7 +111,7 @@ public void PathMismatchFunc_PassedThrough(string matchPath, string basePath, st { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString(matchPath), UseNotImplemented); + builder.Map(matchPath, UseNotImplemented); builder.Run(Success); var app = builder.Build(); app.Invoke(context).Wait(); @@ -133,7 +133,7 @@ public void PathMismatchAction_PassedThrough(string matchPath, string basePath, { HttpContext context = CreateRequest(basePath, requestPath); var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString(matchPath), UseNotImplemented); + builder.Map(matchPath, UseNotImplemented); builder.Run(Success); var app = builder.Build(); app.Invoke(context).Wait(); @@ -147,12 +147,12 @@ public void PathMismatchAction_PassedThrough(string matchPath, string basePath, public void ChainedRoutes_Success() { var builder = new ApplicationBuilder(serviceProvider: null); - builder.Map(new PathString("/route1"), map => + builder.Map("/route1", map => { - map.Map(new PathString("/subroute1"), UseSuccess); + map.Map("/subroute1", UseSuccess); map.Run(NotImplemented); }); - builder.Map(new PathString("/route2/subroute2"), UseSuccess); + builder.Map("/route2/subroute2", UseSuccess); var app = builder.Build(); HttpContext context = CreateRequest(string.Empty, "/route1"); From 0737ea392f9747f80ebc0854cb29b544958994a1 Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Mon, 20 Apr 2015 11:41:43 -0700 Subject: [PATCH 4/4] Add NotNull to Predicate setter. --- .../Extensions/MapWhenOptions.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenOptions.cs b/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenOptions.cs index 27a9d9b1..86967a2c 100644 --- a/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenOptions.cs +++ b/src/Microsoft.AspNet.Http.Core/Extensions/MapWhenOptions.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNet.Http; +using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Builder.Extensions { @@ -15,7 +16,12 @@ public class MapWhenOptions /// /// The user callback that determines if the branch should be taken /// - public Func Predicate { get; set; } + public Func Predicate + { + get; + [param: NotNull] + set; + } /// /// The branch taken for a positive match