Esempio n. 1
0
        /// <remarks>
        /// If you start any asynchronous methods here, prevent the task
        /// from closing prematurely by using BackgroundTaskDeferral as
        /// described in http://aka.ms/backgroundtaskdeferral
        /// </remarks>
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // This deferral should have an instance reference, if it doesn't... the GC will
            // come some day, see that this method is not active anymore and the local variable
            // should be removed. Which results in the application being closed.
            _deferral = taskInstance.GetDeferral();
            
            var httpServer = new HttpServer(8800);
            _httpServer = httpServer;

            var restRouteHandler = new RestRouteHandler();

            restRouteHandler.RegisterController<AsyncControllerSample>();
            restRouteHandler.RegisterController<FromContentControllerSample>();
            restRouteHandler.RegisterController<PerCallControllerSample>();
            restRouteHandler.RegisterController<SimpleParameterControllerSample>();
            restRouteHandler.RegisterController<SingletonControllerSample>();
            restRouteHandler.RegisterController<ThrowExceptionControllerSample>();
            restRouteHandler.RegisterController<WithResponseContentControllerSample>();

            httpServer.RegisterRoute("api", restRouteHandler);
            httpServer.RegisterRoute(new StaticFileRouteHandler(@"DemoStaticFiles\Web"));

            await httpServer.StartServerAsync();

            // Dont release deferral, otherwise app will stop
        }
Esempio n. 2
0
        public void HandleRequest_OnNonRegisteredRoute_ReturnsBadRequest()
        {
            var httpServer = new HttpServer(80);

            var uri = new Uri("/api/Get", UriKind.Relative);
            var response = httpServer.HandleRequestAsync(Utils.CreateHttpRequest(uri: uri)).Result;

            Assert.IsNotNull(response);
            Assert.AreEqual(HttpResponseStatus.BadRequest, response.ResponseStatus);
        }
Esempio n. 3
0
        public void HandleRequest_RegisteredOnPrefixedRoute_RoutesSuccesfully(string registeredPrefix)
        {
            var httpServer = new HttpServer(80);
            var routeHandler = new TestRouteHandler();
            httpServer.RegisterRoute(registeredPrefix, routeHandler);

            var uri = new Uri("/api/Get", UriKind.Relative);
            var response = httpServer.HandleRequestAsync(Utils.CreateHttpRequest(uri: uri)).Result;

            Assert.IsNotNull(response);
            Assert.AreEqual(HttpResponseStatus.OK, response.ResponseStatus);

            Assert.AreEqual(routeHandler.Requests.Count(), 1);
            Assert.AreEqual(new Uri("/Get", UriKind.Relative), routeHandler.Requests.First().Uri);
        }
Esempio n. 4
0
        public void GivenMultipleRouteHandlersAreAddedInSequentialOrder_WhenRequestIsReceivedOnApiRoute_ThenRequestIsSuccesfullyReceived()
        {
            var httpServer = new HttpServer(80);
            var anyRouteHandler = new TestRouteHandler();
            var apiRouteHandler = new TestRouteHandler();

            httpServer.RegisterRoute(anyRouteHandler);
            httpServer.RegisterRoute("api", apiRouteHandler);

            var apiUri = new Uri("/api/Get", UriKind.Relative);
            var response = httpServer.HandleRequestAsync(Utils.CreateHttpRequest(uri: apiUri)).Result;

            Assert.IsNotNull(response);
            Assert.AreEqual(HttpResponseStatus.OK, response.ResponseStatus);

            Assert.AreEqual(apiRouteHandler.Requests.Count(), 1);
            Assert.AreEqual(anyRouteHandler.Requests.Count(), 0);
        }
Esempio n. 5
0
        private async Task InitializeWebServer()
        {
            var httpServer = new HttpServer(8800);
            _httpServer = httpServer;

            var restRouteHandler = new RestRouteHandler();

            restRouteHandler.RegisterController<AsyncControllerSample>();
            restRouteHandler.RegisterController<FromContentControllerSample>();
            restRouteHandler.RegisterController<PerCallControllerSample>();
            restRouteHandler.RegisterController<SimpleParameterControllerSample>();
            restRouteHandler.RegisterController<SingletonControllerSample>();
            restRouteHandler.RegisterController<ThrowExceptionControllerSample>();
            restRouteHandler.RegisterController<WithResponseContentControllerSample>();

            httpServer.RegisterRoute("api", restRouteHandler);

            httpServer.RegisterRoute(new StaticFileRouteHandler(@"DemoStaticFiles\Web"));
            await httpServer.StartServerAsync();

            // Dont release deferral, otherwise app will stop
        }
Esempio n. 6
0
        public void GivenMultipleRouteHandlersAreBeingAddedOnTheCatchAllRoute_ThenAnExceptionShouldBeThrown()
        {
            var httpServer = new HttpServer(80);
            httpServer.RegisterRoute(new TestRouteHandler());

            Assert.ThrowsException<Exception>(() => httpServer.RegisterRoute(new TestRouteHandler()));
        }
Esempio n. 7
0
        public void GivenMultipleRouteHandlersAreBeingAddedWithTheSamePrefix_ThenAnExceptionShouldBeThrown()
        {
            var httpServer = new HttpServer(80);
            httpServer.RegisterRoute("api", new TestRouteHandler());

            Assert.ThrowsException<Exception>(() => httpServer.RegisterRoute("api", new TestRouteHandler()));
        }