use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class FreeMarkerTemplateTest method testTemplateHandlerOnClasspath.
@Test
public void testTemplateHandlerOnClasspath(TestContext should) {
TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);
final JsonObject context = new JsonObject().put("foo", "badger").put("bar", "fox");
context.put("context", new JsonObject().put("path", "/test-freemarker-template2.ftl"));
engine.render(context, "somedir/test-freemarker-template2.ftl", should.asyncAssertSuccess(render -> {
should.assertEquals("Hello badger and fox\nRequest path is /test-freemarker-template2.ftl\n", normalizeCRLF(render.toString()));
}));
}
use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class JadeTemplateNoCacheTest method testCachingDisabled.
@Test
public void testCachingDisabled(TestContext should) throws IOException {
System.setProperty("vertxweb.environment", "development");
TemplateEngine engine = JadeTemplateEngine.create(vertx);
File temp = File.createTempFile("template", ".jade", new File("target/classes"));
temp.deleteOnExit();
try (PrintWriter out = new PrintWriter(temp)) {
out.print("before");
out.flush();
}
engine.render(new JsonObject(), temp.getParent() + "/" + temp.getName(), should.asyncAssertSuccess(render -> {
should.assertEquals("<before></before>", JadeTemplateTest.normalizeCRLF(render.toString()));
try (PrintWriter out2 = new PrintWriter(temp)) {
out2.print("after");
out2.flush();
} catch (IOException e) {
should.fail(e);
}
engine.render(new JsonObject(), temp.getParent() + "/" + temp.getName(), should.asyncAssertSuccess(render2 -> {
should.assertEquals("<after></after>", JadeTemplateTest.normalizeCRLF(render2.toString()));
}));
}));
}
use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class TemplateHandlerImplTest method testSetIndex.
@Test
public void testSetIndex() {
TemplateEngine templateEngine = mock(TemplateEngine.class);
RoutingContext routingContext = mock(RoutingContext.class);
when(routingContext.normalizedPath()).thenReturn("/");
Route currentRoute = mock(Route.class);
when(currentRoute.getPath()).thenReturn("/");
when(routingContext.currentRoute()).thenReturn(currentRoute);
TemplateHandler templateHandler = new TemplateHandlerImpl(templateEngine, "templates", "ext");
templateHandler.setIndexTemplate("home");
templateHandler.handle(routingContext);
verify(templateEngine).render(any(JsonObject.class), eq("templates/home"), any());
}
use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class TemplateTest method testTemplateEngineFail.
@Test
public void testTemplateEngineFail() throws Exception {
TemplateEngine engine = new TestEngine(true);
router.route().handler(TemplateHandler.create(engine, "somedir", "text/html"));
router.errorHandler(500, ctx -> {
Throwable t = ctx.failure();
assertEquals("eek", t.getMessage());
testComplete();
});
testRequest(HttpMethod.GET, "/foo.html", 500, "Internal Server Error");
await();
}
use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class TemplateTest method testRenderDirectly.
@Test
public void testRenderDirectly() throws Exception {
TemplateEngine engine = new TestEngine(false);
router.route().handler(context -> {
context.put("foo", "badger");
context.put("bar", "fox");
engine.render(context.data(), "somedir/test-template.html", res -> {
if (res.succeeded()) {
context.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/html").end(res.result());
} else {
context.fail(res.cause());
}
});
});
String expected = "<html>\n" + "<body>\n" + "<h1>Test template</h1>\n" + "foo is badger bar is fox<br>\n" + "</body>\n" + "</html>";
testRequest(HttpMethod.GET, "/", 200, "OK", expected);
}
Aggregations