/// <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);
        }
Beispiel #2
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;
        }
 public void NullArguments_ArgumentNullException()
 {
     var builder = new ApplicationBuilder(serviceProvider: null);
     var noMiddleware = new ApplicationBuilder(serviceProvider: null).Build();
     var noOptions = new MapWhenOptions();
     Assert.Throws<ArgumentNullException>(() => builder.MapWhen(null, UseNotImplemented));
     Assert.Throws<ArgumentNullException>(() => builder.MapWhen(NotImplementedPredicate, configuration: null));
     Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(null, noOptions));
     Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null));
     Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(null, noOptions));
     Assert.Throws<ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null));
 }
Beispiel #4
0
        public void NullArguments_ArgumentNullException()
        {
            var builder      = new ApplicationBuilder(serviceProvider: null);
            var noMiddleware = new ApplicationBuilder(serviceProvider: null).Build();
            var noOptions    = new MapWhenOptions();

            Assert.Throws <ArgumentNullException>(() => builder.MapWhen(null !, UseNotImplemented));
            Assert.Throws <ArgumentNullException>(() => builder.MapWhen(NotImplementedPredicate, configuration: null !));
            Assert.Throws <ArgumentNullException>(() => new MapWhenMiddleware(null !, noOptions));
            Assert.Throws <ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null !));
            Assert.Throws <ArgumentNullException>(() => new MapWhenMiddleware(null !, noOptions));
            Assert.Throws <ArgumentNullException>(() => new MapWhenMiddleware(noMiddleware, null !));
        }
Beispiel #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));
            }

            _next    = next;
            _options = options;
        }