Search in sources :

Example 36 with TemplateEngine

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());
}
Also used : TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) TemplateHandlerImpl(io.vertx.ext.web.handler.impl.TemplateHandlerImpl) RoutingContext(io.vertx.ext.web.RoutingContext) JsonObject(io.vertx.core.json.JsonObject) TemplateHandler(io.vertx.ext.web.handler.TemplateHandler) Route(io.vertx.ext.web.Route) Test(org.junit.Test)

Example 37 with TemplateEngine

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);
}
Also used : TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Test(org.junit.Test)

Example 38 with TemplateEngine

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);
}
Also used : TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Route(io.vertx.ext.web.Route)

Example 39 with TemplateEngine

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();
}
Also used : Route(io.vertx.ext.web.Route) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) HttpHeaders(io.vertx.core.http.HttpHeaders) Router(io.vertx.ext.web.Router) Test(org.junit.Test) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) TemplateHandler(io.vertx.ext.web.handler.TemplateHandler) Utils(io.vertx.ext.web.impl.Utils) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) Map(java.util.Map) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) WebTestBase(io.vertx.ext.web.WebTestBase) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Test(org.junit.Test)

Example 40 with TemplateEngine

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()));
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) PrintWriter(java.io.PrintWriter) BeforeClass(org.junit.BeforeClass) FileSystemOptions(io.vertx.core.file.FileSystemOptions) MVELTemplateEngine(io.vertx.ext.web.templ.mvel.MVELTemplateEngine) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) IOException(java.io.IOException) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Utils(io.vertx.core.impl.Utils) File(java.io.File) JsonObject(io.vertx.core.json.JsonObject) Assume(org.junit.Assume) MVELTemplateEngine(io.vertx.ext.web.templ.mvel.MVELTemplateEngine) TemplateEngine(io.vertx.ext.web.common.template.TemplateEngine) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

TemplateEngine (io.vertx.ext.web.common.template.TemplateEngine)75 Test (org.junit.Test)74 JsonObject (io.vertx.core.json.JsonObject)70 TestContext (io.vertx.ext.unit.TestContext)57 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)57 RunWith (org.junit.runner.RunWith)57 File (java.io.File)53 IOException (java.io.IOException)53 PrintWriter (java.io.PrintWriter)53 Vertx (io.vertx.core.Vertx)52 VertxOptions (io.vertx.core.VertxOptions)52 FileSystemOptions (io.vertx.core.file.FileSystemOptions)52 BeforeClass (org.junit.BeforeClass)52 JsonArray (io.vertx.core.json.JsonArray)16 PebbleTemplateEngine (io.vertx.ext.web.templ.pebble.PebbleTemplateEngine)13 PebbleEngine (com.mitchellbosecke.pebble.PebbleEngine)11 TestExtension (io.vertx.ext.web.templ.extension.TestExtension)11 PebbleVertxLoader (io.vertx.ext.web.templ.pebble.impl.PebbleVertxLoader)11 ArrayList (java.util.ArrayList)11 Assert.assertNotNull (org.junit.Assert.assertNotNull)10