Exemple #1
0
        public void EmptyResponsePageTest()
        {
            Action <IAppBuilder> config = (builder) =>
            {
                builder.Use(typeof(EmptyResponseApplication));
            };

            using (var server = new FosSelfHost(config))
            {
                server.Bind(ListenOn, ListenPort);
                server.Start(true);

                System.Threading.Thread.Sleep(100);

                // Make the request and expect the empty response page with 500 status code
                var browser = new Browser(ListenOn, ListenPort);
                using (var response = browser.ExecuteRequest("http://localhost/", "GET"))
                {
                    var emptyResponsePage = new EmptyResponsePage();

                    //Assert.AreNotEqual(0, response.AppStatusCode);
                    Assert.AreEqual(500, response.HttpStatusCode);
                    Assert.AreEqual(emptyResponsePage.Contents, ReadStream(response.ResponseBody));
                }
            }
        }
Exemple #2
0
 public static void Main(string[] args)
 {
     using (FosServer = new FosSelfHost(ConfigureOwin))
     {
         StaticConfiguration.DisableErrorTraces = false;
         FosServer.Bind(IPAddress.Loopback, Int32.Parse(ConfigurationManager.AppSettings["port"])); // Loopback is forced, as it is not mean to be front-facing server
         FosServer.Start(false);
     }
 }
Exemple #3
0
        public static void FastCgi(int port)
        {
            var startup = new Startup();

            using (var fosServer = new FosSelfHost(startup.Configuration))
            {
                fosServer.Bind(System.Net.IPAddress.Loopback, port);
                fosServer.Start(false);
            }
        }
Exemple #4
0
        protected FosSelfHost GetHelloWorldBoundServer()
        {
            Action <IAppBuilder> config = (builder) =>
            {
                builder.Use(typeof(Fos.Tests.Middleware.HelloWorldApplication));
            };

            var server = new FosSelfHost(config);

            server.Bind(ListenOn, ListenPort);

            return(server);
        }
Exemple #5
0
        /// <summary>
        /// Log statistics and show a perty page at <paramref name="relativePath"/>, accessible only if <paramref name="configureAuthentication"/> allows so.
        /// This method is just a way to <see cref="Use"/> an internal middleware that serves a page with the logged statistics.
        /// </summary>
        /// <param name="server">The instance of <see cref="Fos.FosSelfHost"/> that represents your server.</param>
        /// <param name="aggregationInterval">The aggregation interval to show time aggregated statistics such as visit numbers. The logger will have lots of work to do if this is too small, so balance this carefully.</param>
        /// <remarks>You can use <see cref="Fos.Owin.ShuntMiddleware"/> to shunt requests to a certain path to a different <see cref="Owin.IAppBuilder"/> to serve the statistics page.</remarks>
        public static IAppBuilder UseStatisticsLogging(this IAppBuilder builder, FosSelfHost server, TimeSpan aggregationInterval)
        {
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }

            var logger = new Fos.Logging.StatsLogger(aggregationInterval);

            server.StatisticsLogger = logger;
            builder.Use <StatsPageMiddleware>(logger);

            return(builder);
        }
        public Task StartAsync <TContext>(IHttpApplication <TContext> application, CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                Func <IDictionary <string, object>, Task> appFunc = async env =>
                {
                    var owinFeatures = new OwinFeatureCollection(env);
                    var features = new FeatureCollection(owinFeatures);

                    var owinHttpResponse = features.Get <IHttpResponseFeature>();
                    features.Set <IHttpResponseFeature>(new NoOnStartingHttpResponseFeature(owinHttpResponse));

                    var context = application.CreateContext(features);
                    try
                    {
                        await application.ProcessRequestAsync(context);
                    }
                    catch (Exception ex)
                    {
                        application.DisposeContext(context, ex);
                        throw;
                    }

                    application.DisposeContext(context, null);
                };

                // Convert OWIN WebSockets to ASP.NET Core WebSockets
                appFunc = OwinWebSocketAcceptAdapter.AdaptWebSockets(appFunc);

                // Wrap this into a middleware handler that Fos can accept
                Func <IDictionary <string, object>, Func <IDictionary <string, object>, Task>, Task> middlewareHandler = async(env, next) =>
                {
                    await appFunc(env);

                    if (next != null)
                    {
                        await next(env);
                    }
                };

                cgiServer = new FosSelfHost(builder =>
                {
                    builder.Use(middlewareHandler);
                });

                cgiServer.Bind(IPAddress.Loopback, 9000);
                cgiServer.Start(true);
            }, cancellationToken));
        }
        public void StartAndDispose()
        {
            int port = 9007;             // Let's hope this is not being used..

            FosSelfHost server;

            using (server = new FosSelfHost(app => {}))
            {
                server.Bind(System.Net.IPAddress.Loopback, port);

                server.Start(true);
                Assert.AreEqual(true, server.IsRunning);
            }

            Assert.AreEqual(false, server.IsRunning);
        }
        /// <summary>
        /// Log statistics and show a perty page at <paramref name="relativePath"/>, accessible only if <paramref name="configureAuthentication"/> allows so.
        /// This method is just a way to <see cref="Use"/> an internal middleware that serves a page with the logged statistics.
        /// </summary>
        /// <param name="server">The instance of <see cref="Fos.FosSelfHost"/> that represents your server.</param>
        /// <param name="aggregationInterval">The aggregation interval to show time aggregated statistics such as visit numbers. The logger will have lots of work to do if this is too small, so balance this carefully.</param>
        /// <remarks>You can use <see cref="Fos.Owin.ShuntMiddleware"/> to shunt requests to a certain path to a different <see cref="Owin.IAppBuilder"/> to serve the statistics page.</remarks>
        public static IAppBuilder UseStatisticsLogging(this IAppBuilder builder, FosSelfHost server, TimeSpan aggregationInterval)
        {
            if (builder is FosAppBuilder == false)
            {
                throw new ArgumentException("The IAppBuilder must be the Fos's Application Builder implementation. Don't use this extension method with a different Owin Server implementation");
            }
            else if (server == null)
            {
                throw new ArgumentNullException("server");
            }

            var fosBuilder = (FosAppBuilder)builder;
            var logger     = new Fos.Logging.StatsLogger(aggregationInterval);

            server.StatisticsLogger = logger;
            fosBuilder.Use <Fos.Logging.StatsPageMiddleware>(logger);

            return(fosBuilder);
        }
Exemple #9
0
        public void ErrorPageTest()
        {
            Action <IAppBuilder> config = (builder) =>
            {
                builder.Use(typeof(ThrowsExceptionApplication));
            };

            using (var server = new FosSelfHost(config))
            {
                server.Bind(ListenOn, ListenPort);
                server.Start(true);

                //System.Threading.Thread.Sleep(1000000);
                // Make the request and expect the empty response page with 500 status code
                var browser  = new Browser(ListenOn, ListenPort);
                var response = browser.ExecuteRequest("http://localhost/", "GET");

                //Assert.AreNotEqual(0, response.AppStatusCode);
                Assert.AreEqual(500, response.HttpStatusCode);
                Assert.That(ReadStream(response.ResponseBody).Contains("An error occured in the application. On purpose."));
            }
        }
        public void StartAndDisposeUnixSocket()
        {
            FosSelfHost server;

            using (server = new FosSelfHost(app => {}))
            {
                server.Bind("./.FosTestSocket");

                server.Start(true);
                Assert.AreEqual(true, server.IsRunning);
            }

            using (server = new FosSelfHost(app => {}))
            {
                server.Bind("./.FosTestSocket");

                server.Start(true);
                Assert.AreEqual(true, server.IsRunning);
            }

            Assert.AreEqual(false, server.IsRunning);
        }