Exemplo n.º 1
0
            public void ThrowsExceptionWhenRouteNotFound()
            {
                var listener = Substitute.For <IHttpListener>();

                listener.When(l => l.Start()).Do(i => listener.IsListening.Returns(true));
                listener.When(l => l.Stop()).Do(i => listener.IsListening.Returns(false));

                using (var server = new RestServer(listener))
                {
                    Should.Throw <RouteNotFoundException>(() => server.TestUnsafeRouteContext(Mocks.HttpContext()));
                }
            }
Exemplo n.º 2
0
            public void Sends404WhenFileNotFound()
            {
                const string prefix = "prefix";

                using (var server = new RestServer(Substitute.For <IHttpListener>()))
                {
                    var context = Mocks.HttpContext(new Dictionary <string, object> {
                        { "PathInfo", $"/{prefix}/some/folder/file.txt" }
                    });
                    context.Server.Returns(server);

                    server.PublicFolder.Prefix = prefix;
                    server.TestUnsafeRouteContext(context);

                    context.Response.Received().SendResponse(HttpStatusCode.NotFound);
                }
            }
Exemplo n.º 3
0
            public void RoutesWithoutException()
            {
                const string pathinfo = "/route/success";
                var          invoked  = false;

                using (var server = new RestServer(Substitute.For <IHttpListener>()))
                {
                    var context = Mocks.HttpContext(new Dictionary <string, object> {
                        { "PathInfo", pathinfo }
                    });
                    Func <IHttpContext, IHttpContext> func = ctx =>
                    {
                        invoked = true;
                        context.WasRespondedTo.Returns(true);
                        return(ctx);
                    };

                    context.Server.Returns(server);
                    server.Router.Register(func, pathinfo);
                    server.TestUnsafeRouteContext(context);
                }

                invoked.ShouldBeTrue();
            }
Exemplo n.º 4
0
            public void LogsMessageWhenFileReturned()
            {
                const string filepath = "/some/file/path";

                var context = Mocks.HttpContext();

                context.WasRespondedTo.Returns(true);
                context.Request.PathInfo.Returns(filepath);

                var logger = new InMemoryLogger();

                using (var server = new RestServer(Substitute.For <IHttpListener>())
                {
                    Logger = logger
                })
                {
                    context.Server.Returns(server);
                    server.TestUnsafeRouteContext(context);
                }

                logger.Logs.Count.ShouldBe(1);
                logger.Logs[0].Level.ShouldBe(LogLevel.Trace);
                logger.Logs[0].Message.ShouldBe($"Returned file {filepath}");
            }