Exemplo n.º 1
0
        public void WriteResponse()
        {
            using (var layout = new TestDirectoryLayout())
            {
                layout.WriteAppDataFile("static/__test.txt", "test contents");
                var fileStorage  = Application.Ioc.Resolve <IFileStorage>();
                var fileEntry    = fileStorage.GetResourceFile("static/__test.txt");
                var lastModified = fileEntry.LastWriteTimeUtc.Truncate();
                Assert.Equals(fileEntry.ReadAllText(), "test contents");

                var ifModifiedSinces = new DateTime?[] { null, DateTime.UtcNow.AddDays(1), lastModified };
                foreach (var ifModifiedSince in ifModifiedSinces)
                {
                    var result      = new FileEntryResult(fileEntry, ifModifiedSince);
                    var contextMock = new HttpContextMock();
                    result.WriteResponse(contextMock.response);
                    if (ifModifiedSince == lastModified)
                    {
                        // 304
                        Assert.Equals(contextMock.response.StatusCode, 304);
                        Assert.Equals(contextMock.response.GetContentsFromBody(), "");
                    }
                    else
                    {
                        // 200
                        Assert.Equals(contextMock.response.StatusCode, 200);
                        Assert.Equals(contextMock.response.ContentType, "text/plain");
                        Assert.Equals(contextMock.response.GetContentsFromBody(), "test contents");
                    }
                    contextMock.request.headers["If-Modified-Since"] = (
                        contextMock.response.headers["Last-Modified"][0]);
                    Assert.Equals(contextMock.request.GetIfModifiedSince(), lastModified);
                }
            }
        }
Exemplo n.º 2
0
        public void Render()
        {
            using (var layout = new TestDirectoryLayout()) {
                Application.Ioc.Unregister <TemplateAreaManager>();
                Application.Ioc.RegisterMany <TemplateAreaManager>(ReuseType.Singleton);
                var areaManager = Application.Ioc.Resolve <TemplateAreaManager>();

                areaManager.GetArea("__test_area").DefaultWidgets.Add("__test_a");
                areaManager.GetArea("__test_area").DefaultWidgets.Add("__test_b", new { a = 1 });

                layout.WritePluginFile("PluginA", "templates/__test_a.widget", "{}");
                layout.WritePluginFile("PluginA", "templates/__test_a.html", "widget test_a");
                layout.WritePluginFile("PluginB", "templates/__test_b.widget", "{}");
                layout.WritePluginFile("PluginB", "templates/__test_b.html", "widget test_b {{ a }}");

                var result = Template.Parse("{% area __test_area %}").Render();
                Assert.Equals(result,
                              "<div class='template_area' area_id='__test_area'>" +
                              "<div class='template_widget' data-widget=''>widget test_a</div>" +
                              "<div class='template_widget' data-widget=''>widget test_b 1</div>" +
                              "</div>");

                result = Template.Parse("{% area __test_empty_area %}").Render();
                Assert.Equals(result,
                              "<div class='template_area' area_id='__test_empty_area'></div>");
            }
        }
Exemplo n.º 3
0
        public void All()
        {
            using (var layout = new TestDirectoryLayout())
            {
                Application.Ioc.Unregister <TemplateAreaManager>();
                Application.Ioc.RegisterMany <TemplateAreaManager>(ReuseType.Singleton);
                var areaManager = Application.Ioc.Resolve <TemplateAreaManager>();

                areaManager.GetArea("__test_area").DefaultWidgets.Add("__test");
                Assert.Equals(areaManager.GetArea("__test_area").DefaultWidgets[0].Path, "__test");

                layout.WritePluginFile("PluginA", "templates/__test.widget",
                                       "{ Name: 'TestWidget', CacheTime: 123, CacheBy: 'Url' }");
                layout.WritePluginFile("PluginA", "templates/__test.html", "test contents {{ name }}");
                var widgetInfo = areaManager.GetWidgetInfo("__test");
                Assert.Equals(widgetInfo.Name, "TestWidget");
                Assert.Equals(widgetInfo.CacheTime, 123);
                Assert.Equals(widgetInfo.CacheBy, "Url");

                Assert.Equals(areaManager.GetCustomWidgets("__test_area"), null);
                areaManager.SetCustomWidgets("__test_area", new[] {
                    new TemplateWidget("__custom_test")
                });
                var customWidgets = areaManager.GetCustomWidgets("__test_area");
                Assert.Equals(customWidgets.Count, 1);
                Assert.Equals(customWidgets[0].Path, "__custom_test");
                areaManager.SetCustomWidgets("__test_area", null);
                Assert.Equals(areaManager.GetCustomWidgets("__test_area"), null);

#pragma warning disable S1075 // URIs should not be hardcoded
                using (HttpManager.OverrideContext("/a", "GET"))
                {
#pragma warning restore S1075 // URIs should not be hardcoded
                    var context = new Context();
                    context["name"] = "a";
                    var contents = areaManager.RenderWidget(context, new TemplateWidget("__test"));
                    Assert.IsTrueWith(contents.Contains("test contents a"), contents);
                }

#pragma warning disable S1075 // URIs should not be hardcoded
                using (HttpManager.OverrideContext("/b", "GET"))
                {
#pragma warning restore S1075 // URIs should not be hardcoded
                    var context = new Context();
                    context["name"] = "b";
                    var contents = areaManager.RenderWidget(context, new TemplateWidget("__test"));
                    Assert.IsTrueWith(contents.Contains("test contents b"), contents);
                }
            }
        }
Exemplo n.º 4
0
 public void RenderTemplate()
 {
     using (var layout = new TestDirectoryLayout()) {
         Application.Ioc.Unregister <TemplateManager>();
         Application.Ioc.RegisterMany <TemplateManager>(ReuseType.Singleton);
         layout.WritePluginFile("PluginA", "templates/__test_a.html", "test a {{ name }}");
         layout.WritePluginFile("PluginB", "templates/__test_b.html", "test b {{ name }}");
         var templateManager = Application.Ioc.Resolve <TemplateManager>();
         var a = templateManager.RenderTemplate("__test_a.html", new { name = "asd" });
         var b = templateManager.RenderTemplate("__test_b.html", new { name = "asd" });
         Assert.Throws <FileNotFoundException>(() =>
                                               templateManager.RenderTemplate("__test_c.html", new { name = "asd" }));
         Assert.Equals(a, "test a asd");
         Assert.Equals(b, "test b asd");
     }
 }
Exemplo n.º 5
0
 public void ReadTemplateFile()
 {
     using (var layout = new TestDirectoryLayout()) {
         Application.Ioc.Unregister <TemplateFileSystem>();
         Application.Ioc.RegisterMany <TemplateFileSystem>(ReuseType.Singleton);
         layout.WritePluginFile("PluginA", "templates/__test_a.html", "test a");
         layout.WritePluginFile("PluginB", "templates/__test_b.html", "test b");
         var filesystem = Application.Ioc.Resolve <TemplateFileSystem>();
         var context    = new Context();
         var templateA  = (Template)filesystem.ReadTemplateFile(context, "__test_a.html");
         var templateB  = (Template)filesystem.ReadTemplateFile(context, "__test_b.html");
         Assert.Throws <FileNotFoundException>(() =>
                                               filesystem.ReadTemplateFile(context, "__test_c.html"));
         Assert.IsTrue(templateA != null);
         Assert.IsTrue(templateB != null);
         Assert.Equals(templateA.Render(), "test a");
         Assert.Equals(templateB.Render(), "test b");
     }
 }
Exemplo n.º 6
0
        public void WriteResponse()
        {
            using (var layout = new TestDirectoryLayout()) {
                Application.Ioc.Unregister <TemplateManager>();
                Application.Ioc.RegisterMany <TemplateManager>(ReuseType.Singleton);
                layout.WritePluginFile("PluginA", "templates/__test_a.html", "test a {{ name }}");
                layout.WritePluginFile("PluginB", "templates/__test_b.html", "test b {{ name }}");

                var result      = new TemplateResult("__test_a.html", new { name = "asd" });
                var contextMock = new HttpContextMock();
                result.WriteResponse(contextMock.response);
                Assert.Equals(contextMock.response.StatusCode, 200);
                Assert.Equals(contextMock.response.ContentType, "text/html");
                Assert.Equals(contextMock.response.GetContentsFromBody(), "test a asd");

                result      = new TemplateResult("__test_b.html", new { name = "asd" });
                contextMock = new HttpContextMock();
                result.WriteResponse(contextMock.response);
                Assert.Equals(contextMock.response.StatusCode, 200);
                Assert.Equals(contextMock.response.ContentType, "text/html");
                Assert.Equals(contextMock.response.GetContentsFromBody(), "test b asd");
            }
        }