Search in sources :

Example 26 with Javalin

use of io.javalin.Javalin in project javalin by tipsy.

the class TestUtil method test.

public static void test(Javalin javalin, ThrowingBiConsumer<Javalin, HttpUtil> test) {
    JavalinLogger.enabled = false;
    try (final Javalin closingJavalin = javalin.start(0)) {
        HttpUtil http = new HttpUtil(closingJavalin.port());
        test.accept(closingJavalin, http);
        closingJavalin.delete("/x-test-cookie-cleaner", ctx -> ctx.cookieMap().keySet().forEach(ctx::removeCookie));
        http.call(HttpMethod.DELETE, "/x-test-cookie-cleaner");
    }
    JavalinLogger.enabled = true;
}
Also used : Javalin(io.javalin.Javalin)

Example 27 with Javalin

use of io.javalin.Javalin in project javalin by tipsy.

the class TestOpenApiAnnotations_Java method testWithCrudHandler.

@Test
public void testWithCrudHandler() {
    OpenApiOptions openApiOptions = new OpenApiOptions(new Info().title("Example").version("1.0.0"));
    Javalin app = Javalin.create(config -> config.registerPlugin(new OpenApiPlugin(openApiOptions)));
    app.routes(() -> ApiBuilder.crud("users/{user-id}", new JavaCrudHandler()));
    OpenAPI actual = JavalinOpenApi.createSchema(app);
    assertThat(JsonKt.asJsonString(actual)).isEqualTo(JsonKt.getCrudExampleJson());
}
Also used : Javalin(io.javalin.Javalin) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.junit.jupiter.api.Test)

Example 28 with Javalin

use of io.javalin.Javalin in project javalin by tipsy.

the class HelloWorldWebSockets method main.

public static void main(String[] args) {
    Javalin app = Javalin.create(config -> config.enableDevLogging());
    app.ws("/websocket", ws -> {
        ws.onConnect(ctx -> {
            System.out.println("Connected");
            ctx.send("[MESSAGE FROM SERVER] Connection established");
        });
        ws.onMessage(ctx -> {
            System.out.println("Received: " + ctx.message());
            ctx.send("[MESSAGE FROM SERVER] Echo: " + ctx.message());
        });
        ws.onClose(ctx -> {
            System.out.println("Closed");
        });
        ws.onError(ctx -> {
            System.out.println("Errored");
        });
    });
    app.get("/", ctx -> {
        ctx.html("<h1>WebSocket example</h1>\n" + "<script>\n" + "   let ws = new WebSocket(\"ws://localhost:7070/websocket\");\n" + "   ws.onmessage = e => document.body.insertAdjacentHTML(\"beforeEnd\", \"<pre>\" + e.data + \"</pre>\");\n" + "   ws.onclose = () => alert(\"WebSocket connection closed\");\n" + "   setInterval(() => ws.send(\"Repeating request every 2 seconds\"), 2000);\n" + "</script>");
    });
    app.start(7070);
}
Also used : Javalin(io.javalin.Javalin)

Example 29 with Javalin

use of io.javalin.Javalin in project javalin by tipsy.

the class SimpleAsyncTest method test_async.

@Test
@Disabled("For running manually")
public void test_async() throws Exception {
    QueuedThreadPool threadPool = new QueuedThreadPool(10, 2, 60_000);
    Javalin app = Javalin.create(c -> c.server(() -> new Server(threadPool))).start(0);
    HttpUtil http = new HttpUtil(app.port());
    app.get("/test-async", ctx -> ctx.future(getFuture()));
    app.get("/test-sync", ctx -> ctx.result(getBlockingResult()));
    timeCallable("Async result", () -> {
        return new ForkJoinPool(100).submit(() -> range(0, 50).parallel().forEach(i -> {
            assertThat(http.getBody("/test-async")).isEqualTo("success");
        })).get();
    });
    timeCallable("Blocking result", () -> {
        return new ForkJoinPool(100).submit(() -> range(0, 50).parallel().forEach(i -> {
            assertThat(http.getBody("/test-sync")).isEqualTo("success");
        })).get();
    });
    app.stop();
}
Also used : Javalin(io.javalin.Javalin) Logger(org.slf4j.Logger) IntStream.range(java.util.stream.IntStream.range) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) Javalin(io.javalin.Javalin) Disabled(org.junit.jupiter.api.Disabled) Executors(java.util.concurrent.Executors) HttpUtil(io.javalin.testing.HttpUtil) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) ForkJoinPool(java.util.concurrent.ForkJoinPool) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Server(org.eclipse.jetty.server.Server) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpUtil(io.javalin.testing.HttpUtil) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 30 with Javalin

use of io.javalin.Javalin in project javalin by tipsy.

the class FileUploadExample method main.

public static void main(String[] args) {
    Javalin app = Javalin.create().start(7000);
    app.get("/", ctx -> ctx.html("" + "<form method='post' enctype='multipart/form-data'>" + "    <input type='file' name='files' multiple>" + "    <button>Upload</button>" + "</form>"));
    app.post("/", ctx -> {
        ctx.uploadedFiles("files").forEach(file -> {
            FileUtil.streamToFile(file.getContent(), "upload/" + file.getFilename());
        });
    });
}
Also used : Javalin(io.javalin.Javalin)

Aggregations

Javalin (io.javalin.Javalin)47 Test (org.junit.jupiter.api.Test)9 ApiBuilder.get (io.javalin.apibuilder.ApiBuilder.get)4 Context (io.javalin.http.Context)4 Config (org.devocative.artemis.cfg.Config)4 Server (org.eclipse.jetty.server.Server)4 OpenApiPlugin (io.javalin.plugin.openapi.OpenApiPlugin)3 Info (io.swagger.v3.oas.models.info.Info)3 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Context (io.javalin.Context)2 Handler (io.javalin.Handler)2 ApiBuilder.path (io.javalin.apibuilder.ApiBuilder.path)2 Plugin (io.javalin.core.plugin.Plugin)2 ExceptionHandler (io.javalin.http.ExceptionHandler)2 Handler (io.javalin.http.Handler)2 HandlerEntry (io.javalin.http.HandlerEntry)2 HandlerType (io.javalin.http.HandlerType)2