public void RazorEngineService_TestNestedBaseClass() { RunTestHelper(service => { var model = new Person() { Forename = "forname", Surname = "surname" }; var template = @"@TestProperty"; string result = service.RunCompile(template, Guid.NewGuid().ToString(), typeof(Person), model); Assert.AreEqual("mytest", result); }, config => { config.BaseTemplateType = typeof(HostingClass.NestedBaseClass<>); }); }
public void RazorEngineService_CustomLocalizerHelper_ViewBag() { RunTestHelper(service => { var model = new Person() { Forename = "forname", Surname = "surname" }; var template = @"@Model.Forename @Include(""test"") @Localizer.Language"; service.Compile("@Model.Surname", "test", typeof(Person)); dynamic viewbag = new DynamicViewBag(); viewbag.Language = "lang"; string result = service.RunCompile(template, Guid.NewGuid().ToString(), typeof(Person), model, (DynamicViewBag) viewbag); Assert.AreEqual("forname surname lang", result); }, config => { config.BaseTemplateType = typeof(AddLanguageInfo_Viewbag<>); }); }
public void IsolatedRazorEngineService_StaticSecurityCriticalModel_InSandbox() { using (var service = IsolatedRazorEngineService.Create(SandboxCreator)) { const string template = "<h1>Hello World @Model.Forename</h1>"; const string expected = "<h1>Hello World TestForename</h1>"; var model = new Person { Forename = "TestForename" }; Assert.Throws<MethodAccessException>(() => { // Because we cannot access the template constructor (as there is a SecurityCritical type argument) string result = service.RunCompile(template, "test", typeof(Person), model); Assert.AreEqual(expected, result); }); } }
public void IsolatedRazorEngineService_StaticSecurityCriticalModelWrapped_InSandbox() { using (var service = IsolatedRazorEngineService.Create(SandboxCreator)) { const string template = "<h1>Hello World @Model.Forename</h1>"; const string expected = "<h1>Hello World TestForename</h1>"; var model = new Person { Forename = "TestForename" }; string result = service.RunCompile(template, "test", null, new RazorDynamicObject(model)); Assert.AreEqual(expected, result); } }
public void IsolatedRazorEngineService_StaticSecurityCriticalModelDynamicType_InSandBox() { using (var service = IsolatedRazorEngineService.Create(SandboxCreator)) { const string template = "<h1>Hello @Model.Forename</h1>"; const string expected = "<h1>Hello Matt</h1>"; var model = new Person { Forename = "Matt" }; Assert.Throws<SecurityException>(() => { // this time we have no security critical type argument but we still should not be able to access it... string result = service.RunCompile(template, "test", null, model); Assert.AreEqual(expected, result); }); } }
public void IsolatedRazorEngineService_CanParseSimpleTemplate_WithComplexSerializableModel() { using (var service = IsolatedRazorEngineService.Create()) { const string template = "<h1>Hello @Model.Forename</h1>"; const string expected = "<h1>Hello Matt</h1>"; var model = new Person { Forename = "Matt" }; string result = service.RunCompile(template, "test", typeof(Person), model); Assert.That(result == expected, "Result does not match expected: " + result); } }