use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class TemplateHandlerImplTest method testDefaultIndex.
@Test
public void testDefaultIndex() {
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.handle(routingContext);
verify(templateEngine).render(any(JsonObject.class), eq("templates/index"), any());
}
use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class TemplateTest method testTemplateEngineWithPathVariables.
@Test
public void testTemplateEngineWithPathVariables() throws Exception {
TemplateEngine engine = new TestEngine(false);
router.route().handler(context -> {
context.put("foo", "badger");
context.put("bar", "fox");
context.next();
});
router.route("/:project/*").handler(TemplateHandler.create(engine, "somedir", "text/html"));
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, "/1/test-template.html", 200, "OK", expected);
}
use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class TemplateTest method testRelativeToRoutePath.
private void testRelativeToRoutePath(String pathPrefix) throws Exception {
TemplateEngine engine = new TestEngine(false);
router.route().handler(context -> {
context.put("foo", "badger");
context.put("bar", "fox");
context.next();
});
Route route = router.route();
if (pathPrefix != null) {
route.path(pathPrefix + "*");
}
route.handler(TemplateHandler.create(engine, "somedir", "text/html"));
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, pathPrefix != null ? pathPrefix + "/test-template.html" : "/test-template.html", 200, "OK", expected);
}
use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class TemplateTest method testRenderToBuffer.
@Test
public void testRenderToBuffer() throws Exception {
TemplateEngine engine = new TestEngine(false);
String expected = "<html>\n" + "<body>\n" + "<h1>Test template</h1>\n" + "foo is badger bar is fox<br>\n" + "</body>\n" + "</html>";
router.route().handler(context -> {
context.put("foo", "badger");
context.put("bar", "fox");
engine.render(context.data(), "somedir/test-template.html", onSuccess(res -> {
String rendered = res.toString();
final String actual = normalizeLineEndingsFor(res).toString();
assertEquals(expected, actual);
context.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/html");
context.response().end(rendered);
testComplete();
}));
});
testRequestBuffer(HttpMethod.GET, "/", null, null, 200, "OK", Buffer.buffer(expected), true);
await();
}
use of io.vertx.ext.web.common.template.TemplateEngine in project vertx-web by vert-x3.
the class MVELTemplateTest method MVELTemplateTestMVELTemplateTestMVELTemplateTest.
@Test
public void MVELTemplateTestMVELTemplateTestMVELTemplateTest(TestContext should) {
TemplateEngine engine = MVELTemplateEngine.create(vertx);
final JsonObject context = new JsonObject().put("foo", "badger").put("bar", "fox");
context.put("context", new JsonObject().put("path", "/test-mvel-template3.templ"));
String tmplPath = "src/test/filesystemtemplates/test-mvel-template3.templ".replace('/', File.separatorChar);
engine.render(context, tmplPath, should.asyncAssertSuccess(render -> {
should.assertEquals("Hello badger and fox\nRequest path is /test-mvel-template3.templ\n", normalizeCRLF(render.toString()));
}));
}
Aggregations