public void NonNullCorsRequestContext_CallsPolicyProvider() { IAppBuilder builder = new AppBuilder(); bool wasCalled = false; builder.UseCors(new CorsOptions { PolicyProvider = new CorsPolicyProvider { PolicyResolver = ctx => { wasCalled = true; return Task.FromResult<CorsPolicy>(null); } } }); builder.Run(context => { context.Response.StatusCode = 200; return Task.FromResult(0); }); var app = (AppFunc)builder.Build(typeof(AppFunc)); IOwinRequest request = CreateRequest("http://localhost/sample"); request.Headers.Set(CorsConstants.Origin, "http://test"); app(request.Environment).Wait(); var response = new OwinResponse(request.Environment); Assert.Equal(true, wasCalled); }
/// <summary> /// Initializes a new instance of the <see cref="IdentityServerBearerTokenValidationMiddleware" /> class. /// </summary> /// <param name="next">The next middleware.</param> /// <param name="options">The options.</param> /// <param name="loggerFactory">The logger factory.</param> public IdentityServerBearerTokenValidationMiddleware(AppFunc next, IdentityServerOAuthBearerAuthenticationOptions options, ILoggerFactory loggerFactory) { _next = next; _options = options; _logger = loggerFactory.Create(this.GetType().FullName); if (options.LocalValidationOptions != null) { var localBuilder = new AppBuilder(); options.OnValidationAppBuilderCreated?.Invoke(localBuilder); localBuilder.UseOAuthBearerAuthentication(options.LocalValidationOptions); localBuilder.Run(ctx => next(ctx.Environment)); _localValidationFunc = localBuilder.Build(); } if (options.EndpointValidationOptions != null) { var endpointBuilder = new AppBuilder(); options.OnValidationAppBuilderCreated?.Invoke(endpointBuilder); endpointBuilder.Properties["host.AppName"] = "foobar"; endpointBuilder.UseOAuthBearerAuthentication(options.EndpointValidationOptions); endpointBuilder.Run(ctx => next(ctx.Environment)); _endpointValidationFunc = endpointBuilder.Build(); } }
public object Configuration() { var builder = new AppBuilder(); builder.Run(context => { return context.Response.WriteAsync("SUCCESS"); }); return builder.Build<AppFunc>(); }
public object Configuration(IDictionary<string, object> properties) { var builder = new AppBuilder(); builder.Run(context => { return context.Response.WriteAsync("SUCCESS"); }); return builder.Build<AppFunc>(); }
/// <summary> /// Initializes a new instance of the <see cref="TheaterMiddleware" /> class. /// </summary> /// <param name="theaterSettings">The histrio settings used to configure the middelware</param> internal TheaterMiddleware(TheaterSettings theaterSettings) { var controllerActivator = new TheaterControllerActivator(theaterSettings.Theater); MidFunc = next => { var app = new AppBuilder(); app.UseWebApi(GetWebApiConfiguration(controllerActivator)); app.Run(ctx => next(ctx.Environment)); return app.Build(); }; }
private void ConfigurePipeline(AppFunc next, string fullPath) { Console.WriteLine("Configuring pipeline..."); try { string index = string.Format( "<HTML><HEAD><TITLE>Dynamic Middleware</TITLE></HEAD><BODY><p>Edit {0} and refresh to see below links change.</p>", fullPath); var allLines = File.ReadAllLines(fullPath); var app = new AppBuilder(); foreach(var line in allLines) { var paths = line.Split(';'); app.UseDirectoryBrowser(new DirectoryBrowserOptions { RequestPath = new PathString(paths[0]), FileSystem = new PhysicalFileSystem(paths[1]) }); index += string.Format("<a href='{0}'>{0}</a><br>", paths[0]); Console.WriteLine("Directory {0} on {1}", paths[1], paths[0]); } app.Use(async (ctx, next2) => { if(ctx.Request.Path.Value == "/") { await ctx.Response.WriteAsync(index); } await next2(); }); app.Run(ctx => next(ctx.Environment)); _dynamicAppFunc = app.Build(); } catch(Exception ex) { Console.WriteLine(ex); _dynamicAppFunc = async env => { var context = new OwinContext(env); context.Response.StatusCode = 500; context.Response.ReasonPhrase = "Internal server error"; await context.Response.WriteAsync(ex.Message); }; } }
public void NullPolicyProvider_CallsNext() { IAppBuilder builder = new AppBuilder(); builder.UseCors(new CorsOptions { }); builder.Run(context => { context.Response.StatusCode = 200; return Task.FromResult(0); }); var app = (AppFunc)builder.Build(typeof(AppFunc)); IOwinRequest request = CreateRequest("http://localhost/sample"); app(request.Environment).Wait(); var response = new OwinResponse(request.Environment); Assert.Equal(200, response.StatusCode); Assert.Empty(response.Headers); }
/// <summary> /// Initializes a new instance of the <see cref="IdentityServerBearerTokenValidationMiddleware"/> class. /// </summary> /// <param name="next">The next middleware.</param> /// <param name="options">The options.</param> public IdentityServerBearerTokenValidationMiddleware(AppFunc next, IdentityServerOAuthBearerAuthenticationOptions options) { _next = next; _options = options; if (options.LocalValidationOptions != null) { var localBuilder = new AppBuilder(); localBuilder.UseOAuthBearerAuthentication(options.LocalValidationOptions); localBuilder.Run(ctx => next(ctx.Environment)); _localValidationFunc = localBuilder.Build(); } if (options.EndpointValidationOptions != null) { var endpointBuilder = new AppBuilder(); endpointBuilder.Properties["host.AppName"] = "foobar"; endpointBuilder.UseOAuthBearerAuthentication(options.EndpointValidationOptions); endpointBuilder.Run(ctx => next(ctx.Environment)); _endpointValidationFunc = endpointBuilder.Build(); } }
public void PredicateAsyncFalseAction_PassThrough() { IOwinContext context = new OwinContext(); IAppBuilder builder = new AppBuilder(); builder.MapWhenAsync(FalsePredicateAsync, UseNotImplemented); builder.Run(Success); var app = builder.Build<OwinMiddleware>(); app.Invoke(context).Wait(); Assert.Equal(200, context.Response.StatusCode); }
public void PathMismatchAction_PassedThrough(string matchPath, string basePath, string requestPath) { IOwinContext context = CreateRequest(basePath, requestPath); IAppBuilder builder = new AppBuilder(); builder.Map(matchPath, UseNotImplemented); builder.Run(Success); var app = builder.Build<OwinMiddleware>(); app.Invoke(context); Assert.Equal(200, context.Response.StatusCode); Assert.Equal(basePath, context.Request.PathBase.Value); Assert.Equal(requestPath, context.Request.Path.Value); }