A standard implementation of IAppBuilder
Inheritance: IAppBuilder
 public static void AddOwinRoute(this RouteCollection routes, string path, Action<IAppBuilder> configuration)
 {
     var builder = new AppBuilder();
     configuration(builder);
     var app = (AppDelegate)builder.Build(typeof(AppDelegate));
     routes.Add(new OwinRoute(path, app));
 }
Exemple #2
0
 private ResultParameters Call(Action<IAppBuilder> pipe, string path)
 {
     var builder = new AppBuilder();
     pipe(builder);
     var app = builder.Build<AppDelegate>();
     return app(new Request() { Path = path }.Call).Result;
 }
 private void Call(Action<IAppBuilder> pipe, Request request)
 {
     var builder = new AppBuilder();
     pipe(builder);
     var app = (AppFunc)builder.Build(typeof(AppFunc));
     app(request.Environment).Wait();
 }
Exemple #4
0
 private ResultParameters Call(Action<IAppBuilder> pipe)
 {
     var builder = new AppBuilder();
     pipe(builder);
     var app = (AppDelegate)builder.Build(typeof(AppDelegate));
     return app(new Request().Call).Result;
 }
 public void LoadWithAssemblyAndFullTypeName_Success(string data, string expected)
 {
     ServerFactoryLoader loader = new ServerFactoryLoader(new ServerFactoryActivator(ServicesFactory.Create()));
     IServerFactoryAdapter serverFactory = loader.Load(data);
     Assert.NotNull(serverFactory);
     IAppBuilder builder = new AppBuilder();
     serverFactory.Create(builder);
     Assert.Equal(expected, builder.Properties["create.server"]);
 }
 public void LoadWithAssemblyName_DiscoverDefaultFactoryName()
 {
     ServerFactoryLoader loader = new ServerFactoryLoader(new ServerFactoryActivator(ServicesFactory.Create()));
     IServerFactoryAdapter serverFactory = loader.Load("Microsoft.Owin.Hosting.Tests");
     Assert.NotNull(serverFactory);
     IAppBuilder builder = new AppBuilder();
     serverFactory.Create(builder);
     Assert.Equal("Microsoft.Owin.Hosting.Tests.ServerFactory", builder.Properties["create.server"]);
 }
 private IDictionary<string, string[]> Call(Action<IAppBuilder> pipe)
 {
     var builder = new AppBuilder();
     pipe(builder);
     var app = (AppFunc)builder.Build(typeof(AppFunc));
     var env = Request.Create().Environment;
     var resp = new Response(env);
     resp.OutputStream = new MemoryStream();
     app(env).Wait();
     return resp.Headers;
 }
        public void NoMatch_PassesThrough(string baseUrl, string baseDir, string requestUrl)
        {
            IAppBuilder builder = new AppBuilder();
            builder.UseStaticFiles(baseUrl, baseDir);
            var app = (AppFunc)builder.Build(typeof(AppFunc));

            IDictionary<string, object> env = CreateEmptyRequest(requestUrl);
            app(env).Wait();

            Assert.Equal(404, env["owin.ResponseStatusCode"]);
        }
 public void NullArguments_ArgumentNullException()
 {
     var builder = new AppBuilder();
     Assert.Throws<ArgumentNullException>(() => builder.MapPath(null, FuncNotImplemented));
     Assert.Throws<ArgumentNullException>(() => builder.MapPath("/foo", (AppFunc)null));
     Assert.Throws<ArgumentNullException>(() => builder.MapPath(null, ActionNotImplemented));
     Assert.Throws<ArgumentNullException>(() => builder.MapPath("/foo", (Action<IAppBuilder>)null));
     Assert.Throws<ArgumentNullException>(() => new MapPathMiddleware(null, FuncNotImplemented, "/foo"));
     Assert.Throws<ArgumentNullException>(() => new MapPathMiddleware(FuncNotImplemented, null, "/foo"));
     Assert.Throws<ArgumentNullException>(() => new MapPathMiddleware(FuncNotImplemented, FuncNotImplemented, null));
 }
        public void PostDirectory_PassesThrough(string baseUrl, string baseDir, string requestUrl)
        {
            IAppBuilder builder = new AppBuilder();
            builder.UseDirectoryBrowser(baseUrl, baseDir);
            var app = (AppFunc)builder.Build(typeof(AppFunc));

            IDictionary<string, object> env = CreateEmptyRequest(requestUrl);
            env["owin.RequestMethod"] = "POST";
            app(env).Wait();

            Assert.Equal(404, env["owin.ResponseStatusCode"]);
        }
        public void PathMatchAction_BranchTaken(string matchPath, string basePath, string requestPath)
        {
            IDictionary<string, object> environment = CreateEmptyRequest(basePath, requestPath);
            IAppBuilder builder = new AppBuilder();
            builder.MapPath(matchPath, subBuilder => subBuilder.Run(Success));
            var app = builder.Build<AppFunc>();
            app(environment);

            Assert.Equal(200, environment["owin.ResponseStatusCode"]);
            Assert.Equal(basePath + matchPath, environment["owin.RequestPathBase"]);
            Assert.Equal(requestPath.Substring(matchPath.Length), environment["owin.RequestPath"]);
        }
        public void FoundDirectoryWithDefaultFile_PathModified(string baseUrl, string baseDir, string requestUrl)
        {
            IAppBuilder builder = new AppBuilder();
            builder.UseDefaultFiles(baseUrl, baseDir);
            var app = (AppFunc)builder.Build(typeof(AppFunc));

            IDictionary<string, object> env = CreateEmptyRequest(requestUrl);
            app(env).Wait();

            Assert.Equal(404, env["owin.ResponseStatusCode"]); // Passed through
            Assert.Equal(requestUrl + "default.html", env["owin.RequestPath"]); // Should be modified
        }
Exemple #13
0
 private string Call(Action<IAppBuilder> pipe, Request request)
 {
     var builder = new AppBuilder();
     pipe(builder);
     var app = (AppFunc)builder.Build(typeof(AppFunc));
     var env = request.Environment;
     var resp = new Response(env);
     MemoryStream buffer = new MemoryStream();
     resp.OutputStream = buffer;
     app(env).Wait();
     return Encoding.UTF8.GetString(buffer.GetBuffer(), 0, (int)buffer.Length);
 }
Exemple #14
0
        public static AppDelegate DefaultFactory()
        {
            var loader = new DefaultLoader();
            var builder = new AppBuilder();

            var startupName = ConfigurationManager.AppSettings["owin:Startup"];
            var startupMethod = loader.Load(startupName);
            if (startupMethod != null)
            {
                startupMethod.Invoke(builder);
            }
            return (AppDelegate)builder.Build(typeof(AppDelegate));
        }
Exemple #15
0
        private int Call(Action<IAppBuilder> pipe, string path)
        {
            var builder = new AppBuilder();
            pipe(builder);
            var app = builder.Build<AppFunc>();

            Request request = Request.Create();
            request.Path = path;
            request.Method = "GET";
            Response response = new Response(request.Environment);
            response.OutputStream = new MemoryStream();
            app(request.Environment).Wait();
            return response.StatusCode;
        }
        public void NearMatch_RedirectAddSlash(string baseUrl, string baseDir, string requestUrl)
        {
            IAppBuilder builder = new AppBuilder();
            builder.UseDirectoryBrowser(baseUrl, baseDir);
            var app = (AppFunc)builder.Build(typeof(AppFunc));

            IDictionary<string, object> env = CreateEmptyRequest(requestUrl);
            app(env).Wait();

            Assert.Equal(301, env["owin.ResponseStatusCode"]);
            var responseHeaders = (IDictionary<string, string[]>)env["owin.ResponseHeaders"];
            Assert.Equal(requestUrl + "/", responseHeaders["Location"][0]);
            Assert.Equal(0, ((Stream)env["owin.ResponseBody"]).Length);
        }
        public void FoundFile_Served(string baseUrl, string baseDir, string requestUrl)
        {
            IAppBuilder builder = new AppBuilder();
            builder.UseStaticFiles(baseUrl, baseDir);
            var app = (AppFunc)builder.Build(typeof(AppFunc));

            IDictionary<string, object> env = CreateEmptyRequest(requestUrl);
            app(env).Wait();

            var responseHeaders = (IDictionary<string, string[]>)env["owin.ResponseHeaders"];
            Assert.Equal("text/xml", responseHeaders["Content-Type"][0]);
            Assert.True(responseHeaders["Content-Length"][0].Length > 0);
            Assert.Equal(responseHeaders["Content-Length"][0], ((Stream)env["owin.ResponseBody"]).Length.ToString());
        }
        public OwinHttpClient(Action<IAppBuilder> build)
        {
            var app = new AppBuilder();
            _shutdownTokenSource = new CancellationTokenSource();

            // Some default properties (this is the instance name so make it unique per instance)
            app.Properties["server.Capabilities"] = new Dictionary<string, object>();
            app.Properties["host.AppName"] = "OwinHttpClient(" + GetHashCode() + ")";
            app.Properties["host.OnAppDisposing"] = _shutdownTokenSource.Token;

            build(app);

            _appFunc = (AppFunc)app.Build(typeof(AppFunc));
        }
        public async Task ScriptFileCanBeExecutedByName()
        {
            var factory = new RoslynAppLoaderFactory();
            Func<string, Action<IAppBuilder>> loader = factory.Create(_ => null);
            Action<IAppBuilder> startup = loader.Invoke("Simple.csx");
            var builder = new AppBuilder();
            startup.Invoke(builder);
            var app = (AppFunc)builder.Build(typeof(AppFunc));

            OwinRequest req = OwinRequest.Create();
            await app.Invoke(req.Dictionary);
            var res = new OwinResponse(req);
            res.StatusCode.ShouldBe(24601);
        }
Exemple #20
0
        static void Main(string[] args)
        {
            var app = new AppBuilder();
            app.Properties["host.Addresses"] = HostAddresses;

            ServerFactory.Initialize(app);
            Startup.Configuration(app);
            var server = ServerFactory.Create(app.Build(), app.Properties);

            System.Console.WriteLine("Running on http://localhost:8080/");
            System.Console.WriteLine("Press enter to exit");
            System.Console.ReadLine();

            server.Dispose();
        }
Exemple #21
0
        public void Configure(Action<IAppBuilder> startup)
        {
            if (startup == null)
            {
                throw new ArgumentNullException("startup");
            }

            var builder = new AppBuilder();

            builder.Properties[OwinConstants.ServerCapabilities] = new Dictionary<string, object>();
            builder.Properties[OwinConstants.HostOnAppDisposing] = _shutDownToken;
            builder.Properties[OwinConstants.HostAppNameKey] = InstanceName ?? Process.GetCurrentProcess().GetUniqueInstanceName(_shutDownToken);

            startup(builder);

            _appFunc = Build(builder);
        }
        public void NullArguments_ArgumentNullException()
        {
            var builder = new AppBuilder();
            Assert.Throws<ArgumentNullException>(() => builder.MapPredicate(null, FuncNotImplemented));
            Assert.Throws<ArgumentNullException>(() => builder.MapPredicate(NotImplementedPredicate, (AppFunc)null));
            Assert.Throws<ArgumentNullException>(() => builder.MapPredicate(null, ActionNotImplemented));
            Assert.Throws<ArgumentNullException>(() => builder.MapPredicate(NotImplementedPredicate, (Action<IAppBuilder>)null));
            Assert.Throws<ArgumentNullException>(() => new MapPredicateMiddleware(null, FuncNotImplemented, NotImplementedPredicate));
            Assert.Throws<ArgumentNullException>(() => new MapPredicateMiddleware(FuncNotImplemented, null, NotImplementedPredicate));
            Assert.Throws<ArgumentNullException>(() => new MapPredicateMiddleware(FuncNotImplemented, FuncNotImplemented, (Predicate)null));

            Assert.Throws<ArgumentNullException>(() => builder.MapPredicateAsync(null, FuncNotImplemented));
            Assert.Throws<ArgumentNullException>(() => builder.MapPredicateAsync(NotImplementedPredicateAsync, (AppFunc)null));
            Assert.Throws<ArgumentNullException>(() => builder.MapPredicateAsync(null, ActionNotImplemented));
            Assert.Throws<ArgumentNullException>(() => builder.MapPredicateAsync(NotImplementedPredicateAsync, (Action<IAppBuilder>)null));
            Assert.Throws<ArgumentNullException>(() => new MapPredicateMiddleware(null, FuncNotImplemented, NotImplementedPredicateAsync));
            Assert.Throws<ArgumentNullException>(() => new MapPredicateMiddleware(FuncNotImplemented, null, NotImplementedPredicateAsync));
            Assert.Throws<ArgumentNullException>(() => new MapPredicateMiddleware(FuncNotImplemented, FuncNotImplemented, (PredicateAsync)null));
        }
Exemple #23
0
        static void Main(string[] args)
        {
            var server = new ServerFactory(new ConsoleTrace());

            var builder = new AppBuilder();
            builder
                .UseFunc(ShowFormValues)
                .UseFunc(UrlRewrite("/", "/index.html"))
                .UseStatic()
                .Run(new Program());

            var app = builder.Build<AppFunc>();

            using (server.Create(app, 8080))
            {
                Console.WriteLine("Running server on http://localhost:8080/");
                Console.WriteLine("Press enter to exit");
                Console.ReadLine();
            }
        }
Exemple #24
0
        static void Main(string[] args)
        {
            var startup = new Startup(); // this program's Startup.cs class
            var app = new AppBuilder(); // the app builder, from Owin.Builder package

            // the server adapter, from Microsoft.HttpListener.Owin package, gets first chance
            ServerFactory.Initialize(app.Properties);

            // this method adds all middleware and frameworks
            startup.Configuration(app);

            // ext method to set server address in app.Properties
            app.SetHostAddress("http", "+", 8080);

            // build pipeline, and give to server adapter from Microsoft.HttpListener.Owin package
            using (ServerFactory.Create(app.Build(), app.Properties))
            {
                Console.WriteLine("Running on http://+:8080/");
                Console.WriteLine("Press enter to exit");
                Console.ReadLine();
            }
        }
Exemple #25
0
 AppDelegate Build(Action<IAppBuilder> b)
 {
     var builder = new AppBuilder();
     b(builder);
     return (AppDelegate)builder.Build(typeof(AppDelegate));
 }
 AppFunc Build(Action<IAppBuilder> b)
 {
     var builder = new AppBuilder();
     b(builder);
     return (AppFunc)builder.Build(typeof(AppFunc));
 }
 private Response Call(Action<IAppBuilder> pipe)
 {
     var builder = new AppBuilder();
     pipe(builder);
     var app = (AppFunc)builder.Build(typeof(AppFunc));
     Request request = Request.Create();
     request.Method = "GET";
     Response response = new Response(request.Environment);
     MemoryStream buffer = new MemoryStream();
     response.OutputStream = buffer;
     app(request.Environment).Wait();
     response.OutputStream = buffer; // Replace the buffer so it can be read.
     return response;
 }
        public void Method_that_returns_app_action_may_also_be_called()
        {
            var loader = new DefaultLoader();
            var configuration = loader.Load("Owin.Loader.Tests.DefaultConfigurationLoaderTests.Alpha");

            var builder = new AppBuilder();
            configuration(builder);
            var app = builder.Build<AppFunc>();

            _alphaCalls = 0;
            app(new Dictionary<string, object>());
            Assert.Equal(1, _alphaCalls);
        }
Exemple #29
0
 public object Build(Type signature, Action<object> configuration)
 {
     var nestedBuilder = new AppBuilder(_conversions, _properties);
     configuration.Invoke(nestedBuilder);
     return nestedBuilder.BuildInternal(signature);
 }
 private IDictionary<string, string[]> CallPipe(Action<IAppBuilder> pipe)
 {
     var builder = new AppBuilder();
     pipe(builder);
     var app = (AppFunc)builder.Build(typeof(AppFunc));
     Request request = Request.Create();
     request.Method = "GET";
     Response response = new Response(request.Environment);
     response.OutputStream = new MemoryStream();
     app(request.Environment).Wait();
     return response.Headers;
 }