|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.AspNetCore.Builder.Internal; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.AspNetCore.Builder.Extensions |
| 11 | +{ |
| 12 | + public class UseWhenExtensionsTests |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public void NullArguments_ArgumentNullException() |
| 16 | + { |
| 17 | + // Arrange |
| 18 | + var builder = CreateBuilder(); |
| 19 | + |
| 20 | + // Act |
| 21 | + Action nullPredicate = () => builder.UseWhen(null, app => { }); |
| 22 | + Action nullConfiguration = () => builder.UseWhen(TruePredicate, null); |
| 23 | + |
| 24 | + // Assert |
| 25 | + Assert.Throws<ArgumentNullException>(nullPredicate); |
| 26 | + Assert.Throws<ArgumentNullException>(nullConfiguration); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void PredicateTrue_BranchTaken_WillRejoin() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var context = CreateContext(); |
| 34 | + var parent = CreateBuilder(); |
| 35 | + |
| 36 | + parent.UseWhen(TruePredicate, child => |
| 37 | + { |
| 38 | + child.UseWhen(TruePredicate, grandchild => |
| 39 | + { |
| 40 | + grandchild.Use(Increment("grandchild")); |
| 41 | + }); |
| 42 | + |
| 43 | + child.Use(Increment("child")); |
| 44 | + }); |
| 45 | + |
| 46 | + parent.Use(Increment("parent")); |
| 47 | + |
| 48 | + // Act |
| 49 | + parent.Build().Invoke(context).Wait(); |
| 50 | + |
| 51 | + // Assert |
| 52 | + Assert.Equal(1, Count(context, "parent")); |
| 53 | + Assert.Equal(1, Count(context, "child")); |
| 54 | + Assert.Equal(1, Count(context, "grandchild")); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void PredicateTrue_BranchTaken_CanTerminate() |
| 59 | + { |
| 60 | + // Arrange |
| 61 | + var context = CreateContext(); |
| 62 | + var parent = CreateBuilder(); |
| 63 | + |
| 64 | + parent.UseWhen(TruePredicate, child => |
| 65 | + { |
| 66 | + child.UseWhen(TruePredicate, grandchild => |
| 67 | + { |
| 68 | + grandchild.Use(Increment("grandchild", terminate: true)); |
| 69 | + }); |
| 70 | + |
| 71 | + child.Use(Increment("child")); |
| 72 | + }); |
| 73 | + |
| 74 | + parent.Use(Increment("parent")); |
| 75 | + |
| 76 | + // Act |
| 77 | + parent.Build().Invoke(context).Wait(); |
| 78 | + |
| 79 | + // Assert |
| 80 | + Assert.Equal(0, Count(context, "parent")); |
| 81 | + Assert.Equal(0, Count(context, "child")); |
| 82 | + Assert.Equal(1, Count(context, "grandchild")); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void PredicateFalse_PassThrough() |
| 87 | + { |
| 88 | + // Arrange |
| 89 | + var context = CreateContext(); |
| 90 | + var parent = CreateBuilder(); |
| 91 | + |
| 92 | + parent.UseWhen(FalsePredicate, child => |
| 93 | + { |
| 94 | + child.Use(Increment("child")); |
| 95 | + }); |
| 96 | + |
| 97 | + parent.Use(Increment("parent")); |
| 98 | + |
| 99 | + // Act |
| 100 | + parent.Build().Invoke(context).Wait(); |
| 101 | + |
| 102 | + // Assert |
| 103 | + Assert.Equal(1, Count(context, "parent")); |
| 104 | + Assert.Equal(0, Count(context, "child")); |
| 105 | + } |
| 106 | + |
| 107 | + private static HttpContext CreateContext() |
| 108 | + { |
| 109 | + return new DefaultHttpContext(); |
| 110 | + } |
| 111 | + |
| 112 | + private static ApplicationBuilder CreateBuilder() |
| 113 | + { |
| 114 | + return new ApplicationBuilder(serviceProvider: null); |
| 115 | + } |
| 116 | + |
| 117 | + private static bool TruePredicate(HttpContext context) |
| 118 | + { |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + private static bool FalsePredicate(HttpContext context) |
| 123 | + { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + private static Func<HttpContext, Func<Task>, Task> Increment(string key, bool terminate = false) |
| 128 | + { |
| 129 | + return (context, next) => |
| 130 | + { |
| 131 | + if (!context.Items.ContainsKey(key)) |
| 132 | + { |
| 133 | + context.Items[key] = 1; |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + var item = context.Items[key]; |
| 138 | + |
| 139 | + if (item is int) |
| 140 | + { |
| 141 | + context.Items[key] = 1 + (int)item; |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + context.Items[key] = 1; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return terminate ? Task.FromResult<object>(null) : next(); |
| 150 | + }; |
| 151 | + } |
| 152 | + |
| 153 | + private static int Count(HttpContext context, string key) |
| 154 | + { |
| 155 | + if (!context.Items.ContainsKey(key)) |
| 156 | + { |
| 157 | + return 0; |
| 158 | + } |
| 159 | + |
| 160 | + var item = context.Items[key]; |
| 161 | + |
| 162 | + if (item is int) |
| 163 | + { |
| 164 | + return (int)item; |
| 165 | + } |
| 166 | + |
| 167 | + return 0; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments