This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Add UseWhenExtensions and UseWhenExtensionsTests #663
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/Microsoft.AspNetCore.Http.Abstractions/Extensions/UseWhenExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Builder | ||
{ | ||
using Predicate = Func<HttpContext, bool>; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IApplicationBuilder"/>. | ||
/// </summary> | ||
public static class UseWhenExtensions | ||
{ | ||
/// <summary> | ||
/// Conditionally creates a branch in the request pipeline that is rejoined to the main pipeline. | ||
/// </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 IApplicationBuilder UseWhen(this IApplicationBuilder app, Predicate predicate, Action<IApplicationBuilder> configuration) | ||
{ | ||
if (app == null) | ||
{ | ||
throw new ArgumentNullException(nameof(app)); | ||
} | ||
|
||
if (predicate == null) | ||
{ | ||
throw new ArgumentNullException(nameof(predicate)); | ||
} | ||
|
||
if (configuration == null) | ||
{ | ||
throw new ArgumentNullException(nameof(configuration)); | ||
} | ||
|
||
// Create and configure the branch builder right away; otherwise, | ||
// we would end up running our branch after all the components | ||
// that were subsequently added to the main builder. | ||
var branchBuilder = app.New(); | ||
configuration(branchBuilder); | ||
|
||
return app.Use(main => | ||
{ | ||
// This is called only when the main application builder | ||
// is built, not per request. | ||
branchBuilder.Run(main); | ||
var branch = branchBuilder.Build(); | ||
|
||
return async context => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the async/await could have been removed here to save a state machine allocation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😱 true, true |
||
{ | ||
if (predicate(context)) | ||
{ | ||
await branch(context); | ||
} | ||
else | ||
{ | ||
await main(context); | ||
} | ||
}; | ||
}); | ||
} | ||
} | ||
} |
170 changes: 170 additions & 0 deletions
170
test/Microsoft.AspNetCore.Http.Abstractions.Tests/UseWhenExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright (c) .NET Foundation. 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.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder.Internal; | ||
using Microsoft.AspNetCore.Http; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Builder.Extensions | ||
{ | ||
public class UseWhenExtensionsTests | ||
{ | ||
[Fact] | ||
public void NullArguments_ArgumentNullException() | ||
{ | ||
// Arrange | ||
var builder = CreateBuilder(); | ||
|
||
// Act | ||
Action nullPredicate = () => builder.UseWhen(null, app => { }); | ||
Action nullConfiguration = () => builder.UseWhen(TruePredicate, null); | ||
|
||
// Assert | ||
Assert.Throws<ArgumentNullException>(nullPredicate); | ||
Assert.Throws<ArgumentNullException>(nullConfiguration); | ||
} | ||
|
||
[Fact] | ||
public void PredicateTrue_BranchTaken_WillRejoin() | ||
{ | ||
// Arrange | ||
var context = CreateContext(); | ||
var parent = CreateBuilder(); | ||
|
||
parent.UseWhen(TruePredicate, child => | ||
{ | ||
child.UseWhen(TruePredicate, grandchild => | ||
{ | ||
grandchild.Use(Increment("grandchild")); | ||
}); | ||
|
||
child.Use(Increment("child")); | ||
}); | ||
|
||
parent.Use(Increment("parent")); | ||
|
||
// Act | ||
parent.Build().Invoke(context).Wait(); | ||
|
||
// Assert | ||
Assert.Equal(1, Count(context, "parent")); | ||
Assert.Equal(1, Count(context, "child")); | ||
Assert.Equal(1, Count(context, "grandchild")); | ||
} | ||
|
||
[Fact] | ||
public void PredicateTrue_BranchTaken_CanTerminate() | ||
{ | ||
// Arrange | ||
var context = CreateContext(); | ||
var parent = CreateBuilder(); | ||
|
||
parent.UseWhen(TruePredicate, child => | ||
{ | ||
child.UseWhen(TruePredicate, grandchild => | ||
{ | ||
grandchild.Use(Increment("grandchild", terminate: true)); | ||
}); | ||
|
||
child.Use(Increment("child")); | ||
}); | ||
|
||
parent.Use(Increment("parent")); | ||
|
||
// Act | ||
parent.Build().Invoke(context).Wait(); | ||
|
||
// Assert | ||
Assert.Equal(0, Count(context, "parent")); | ||
Assert.Equal(0, Count(context, "child")); | ||
Assert.Equal(1, Count(context, "grandchild")); | ||
} | ||
|
||
[Fact] | ||
public void PredicateFalse_PassThrough() | ||
{ | ||
// Arrange | ||
var context = CreateContext(); | ||
var parent = CreateBuilder(); | ||
|
||
parent.UseWhen(FalsePredicate, child => | ||
{ | ||
child.Use(Increment("child")); | ||
}); | ||
|
||
parent.Use(Increment("parent")); | ||
|
||
// Act | ||
parent.Build().Invoke(context).Wait(); | ||
|
||
// Assert | ||
Assert.Equal(1, Count(context, "parent")); | ||
Assert.Equal(0, Count(context, "child")); | ||
} | ||
|
||
private static HttpContext CreateContext() | ||
{ | ||
return new DefaultHttpContext(); | ||
} | ||
|
||
private static ApplicationBuilder CreateBuilder() | ||
{ | ||
return new ApplicationBuilder(serviceProvider: null); | ||
} | ||
|
||
private static bool TruePredicate(HttpContext context) | ||
{ | ||
return true; | ||
} | ||
|
||
private static bool FalsePredicate(HttpContext context) | ||
{ | ||
return false; | ||
} | ||
|
||
private static Func<HttpContext, Func<Task>, Task> Increment(string key, bool terminate = false) | ||
{ | ||
return (context, next) => | ||
{ | ||
if (!context.Items.ContainsKey(key)) | ||
{ | ||
context.Items[key] = 1; | ||
} | ||
else | ||
{ | ||
var item = context.Items[key]; | ||
|
||
if (item is int) | ||
{ | ||
context.Items[key] = 1 + (int)item; | ||
} | ||
else | ||
{ | ||
context.Items[key] = 1; | ||
} | ||
} | ||
|
||
return terminate ? Task.FromResult<object>(null) : next(); | ||
}; | ||
} | ||
|
||
private static int Count(HttpContext context, string key) | ||
{ | ||
if (!context.Items.ContainsKey(key)) | ||
{ | ||
return 0; | ||
} | ||
|
||
var item = context.Items[key]; | ||
|
||
if (item is int) | ||
{ | ||
return (int)item; | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This covers the
if (condition) { }
scenario, but is there also a need forif (condition) { } else { }
or evenif (condition) { } elseif (condition) { } elseif (condition) { } else { }
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (condition) { } else { }
can be done asif (condition) { } if (!condition) { }
, but it may be inefficient to evaluate the conditional twice.if (condition) { } elseif (condition) else { }
can be done asif (condition) { } else { if (condition) { } else { } }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tratcher as far as I see it this fills the need of the original issue, if the additional use case(s) ever become in demand they can be designed then...