Example #1
0
        /// <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 IAppBuilder MapWhenAsync(this IAppBuilder app, PredicateAsync predicate, Action <IAppBuilder> configuration)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            // put middleware in pipeline before creating branch
            var options = new MapWhenOptions {
                PredicateAsync = predicate
            };
            IAppBuilder result = app.Use <MapWhenMiddleware>(options);

            // create branch and assign to options
            IAppBuilder branch = app.New();

            configuration(branch);
            options.Branch = (OwinMiddleware)branch.Build(typeof(OwinMiddleware));

            return(result);
        }
Example #2
0
        public static IApplicationBuilder <TContext> MapWhen <TContext>(this IApplicationBuilder <TContext> app, Predicate <TContext> predicate, Action <ApplicationBuilder <TContext> > configuration)
            where TContext : IRequestContext
        {
            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 branch
            var branchBuilder = app.New();

            configuration(branchBuilder);
            var branch = branchBuilder.Build();

            // put middleware in pipeline
            var options = new MapWhenOptions <TContext>
            {
                Predicate = predicate,
                Branch    = branch,
            };

            return(app.Use(next => new MapWhenMiddleware <TContext>(next, options).Invoke));
        }
        /// <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 IApplicationBuilder MapWhen(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 branch
            var 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));
        }
        public void Configure(IApplicationBuilder app)
        {
            //Use MapMiddleWare and MapWhenMiddlware directly
            var whenOption = new MapWhenOptions
            {
                Branch =
                    context =>
                    context.Response.WriteAsync($"Path: {context.Request.Path} - Path Base: {context.Request.PathBase}"),
                Predicate = context => context.Request.Path.Value.Contains("hello")
            };

            app.UseMiddleware <MapWhenMiddleware>(whenOption);

            var mapOption = new MapOptions
            {
                Branch =
                    context =>
                    context.Response.WriteAsync($"Path: {context.Request.Path} - Path Base: {context.Request.PathBase}"),
                PathMatch = "/greetings"
            };

            app.UseMiddleware <MapMiddleware>(mapOption);

            app.Run(context =>
            {
                context.Response.Headers.Add("content-type", "text/html");
                return(context.Response.WriteAsync(@"
                   <a href=""/hello"">/hello</a> <a href=""/greetings"">/greetings</a>
                "));
            });
        }
Example #5
0
    /// <summary>
    /// Creates a new instance of <see cref="MapWhenMiddleware"/>.
    /// </summary>
    /// <param name="next">The delegate representing the next middleware in the request pipeline.</param>
    /// <param name="options">The middleware options.</param>
    public MapWhenMiddleware(RequestDelegate next, MapWhenOptions options)
    {
        if (next == null)
        {
            throw new ArgumentNullException(nameof(next));
        }

        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        if (options.Predicate == null)
        {
            throw new ArgumentException("Predicate not set on options.", nameof(options));
        }

        if (options.Branch == null)
        {
            throw new ArgumentException("Branch not set on options.", nameof(options));
        }

        _next    = next;
        _options = options;
    }
Example #6
0
        public MapWhenMiddleware(RequestDelegate <TContext> next, MapWhenOptions <TContext> options)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _next    = next;
            _options = options;
        }
        public void NullArguments_ArgumentNullException()
        {
            var builder = new AppBuilder();
            var noMiddleware = new AppBuilder().Build<AppFunc>();
            var noOptions = new MapWhenOptions();
            Assert.Throws<ArgumentNullException>(() => builder.MapWhen(null, UseNotImplemented));
            Assert.Throws<ArgumentNullException>(() => builder.MapWhen(NotImplementedPredicate, (Action<IAppBuilder>)null));
            Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(null, noOptions));
            Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null));

            Assert.Throws<ArgumentNullException>(() => builder.MapWhenAsync(null, UseNotImplemented));
            Assert.Throws<ArgumentNullException>(() => builder.MapWhenAsync(NotImplementedPredicateAsync, (Action<IAppBuilder>)null));
            Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(null, noOptions));
            Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null));
        }
Example #8
0
        public void NullArguments_ArgumentNullException()
        {
            var builder      = new AppBuilder();
            var noMiddleware = new AppBuilder().Build <OwinMiddleware>();
            var noOptions    = new MapWhenOptions();

            Assert.Throws <ArgumentNullException>(() => builder.MapWhen(null, UseNotImplemented));
            Assert.Throws <ArgumentNullException>(() => builder.MapWhen(NotImplementedPredicate, (Action <IAppBuilder>)null));
            Assert.Throws <ArgumentNullException>(() => new MapWhenMiddleware(null, noOptions));
            Assert.Throws <ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null));

            Assert.Throws <ArgumentNullException>(() => builder.MapWhenAsync(null, UseNotImplemented));
            Assert.Throws <ArgumentNullException>(() => builder.MapWhenAsync(NotImplementedPredicateAsync, (Action <IAppBuilder>)null));
            Assert.Throws <ArgumentNullException>(() => new MapWhenMiddleware(null, noOptions));
            Assert.Throws <ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null));
        }
        /// <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 IApplicationBuilder MapWhenAsync([NotNull] this IApplicationBuilder app, [NotNull] PredicateAsync predicate, [NotNull] Action <IApplicationBuilder> 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));
        }
Example #10
0
        public CallBuilder MapWhen(Predicate <CallContext> predicate, Action <CallBuilder> configuration)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            var branchBuilder = New();

            configuration(branchBuilder);
            var branch  = branchBuilder.Build();
            var options = new MapWhenOptions
            {
                Predicate = predicate,
                Branch    = branch
            };

            return(Use(next => new MapWhenMiddleware(next, options).Invoke));
        }
Example #11
0
 internal void MapWhenOptions(IApplicationBuilder app, MapWhenOptions options)
 {
     app.UseMiddleware <MapWhenMiddleware>(options);
 }