// Copyright (c) Microsoft Open Technologies, Inc. // All Rights Reserved // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR // CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING // WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF // TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR // NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing // permissions and limitations under the License. using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.HttpFeature; using Microsoft.AspNet.PipelineCore; using Xunit; namespace Microsoft.AspNet.Builder.Extensions { using AppFunc = Func, Task>; using Predicate = Func; using PredicateAsync = Func>; 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(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 TruePredicateAsync(HttpContext context) { return Task.FromResult(true); } private Task FalsePredicateAsync(HttpContext context) { return Task.FromResult(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(() => builder.MapWhen(null, UseNotImplemented)); // TODO: [NotNull] Assert.Throws(() => builder.MapWhen(NotImplementedPredicate, (Action)null)); // TODO: [NotNull] Assert.Throws(() => new MapWhenMiddleware(null, noOptions)); // TODO: [NotNull] Assert.Throws(() => new MapWhenMiddleware(noMiddleware, null)); // TODO: [NotNull] Assert.Throws(() => builder.MapWhenAsync(null, UseNotImplemented)); // TODO: [NotNull] Assert.Throws(() => builder.MapWhenAsync(NotImplementedPredicateAsync, (Action)null)); // TODO: [NotNull] Assert.Throws(() => new MapWhenMiddleware(null, noOptions)); // TODO: [NotNull] Assert.Throws(() => 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(new FakeHttpRequestFeature()); context.SetFeature(new FakeHttpResponseFeature()); return context; } } }