Search in sources :

Example 1 with EmbeddedJettyFactory

use of io.javalin.embeddedserver.jetty.EmbeddedJettyFactory in project javalin by tipsy.

the class TestCustomJetty method test_embeddedServer_withCustomServer.

@Test
public void test_embeddedServer_withCustomServer() throws Exception {
    Javalin app = Javalin.create().port(0).embeddedServer(new EmbeddedJettyFactory(() -> {
        Server server = new Server();
        server.setAttribute("is-custom-server", true);
        return server;
    })).start();
    assertThat(app.embeddedServer().attribute("is-custom-server"), is(true));
    app.stop();
}
Also used : EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) Server(org.eclipse.jetty.server.Server) Test(org.junit.Test)

Example 2 with EmbeddedJettyFactory

use of io.javalin.embeddedserver.jetty.EmbeddedJettyFactory in project javalin by tipsy.

the class TestAsync method test_async.

@Test
@Ignore("Just for running manually")
public void test_async() throws Exception {
    Javalin app = Javalin.create().embeddedServer(new EmbeddedJettyFactory(() -> new Server(new QueuedThreadPool(16, 10, 60_000)))).port(5454).start();
    app.get("/test-async", ctx -> ctx.async(() -> {
        CompletableFuture<Void> future = new CompletableFuture<>();
        new Timer().schedule(new TimerTask() {

            public void run() {
                ctx.status(418);
                future.complete(null);
            }
        }, 1000);
        return future;
    }));
    long startTime = System.currentTimeMillis();
    ForkJoinPool forkJoinPool = new ForkJoinPool(200);
    forkJoinPool.submit(() -> IntStream.range(0, 50).parallel().forEach(i -> {
        try {
            assertThat(new SimpleHttpClient().http_GET("http://localhost:5454/test-async").getStatus(), is(418));
        } catch (IOException e) {
            e.printStackTrace();
        }
    })).get();
    log.info("took " + (System.currentTimeMillis() - startTime) + " milliseconds");
    app.stop();
}
Also used : EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Server(org.eclipse.jetty.server.Server) Timer(java.util.Timer) TimerTask(java.util.TimerTask) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) IOException(java.io.IOException) SimpleHttpClient(io.javalin.util.SimpleHttpClient) ForkJoinPool(java.util.concurrent.ForkJoinPool) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with EmbeddedJettyFactory

use of io.javalin.embeddedserver.jetty.EmbeddedJettyFactory in project javalin by tipsy.

the class TestCustomJetty method test_embeddedServer_withStatisticsHandler.

@Test
public void test_embeddedServer_withStatisticsHandler() throws Exception {
    StatisticsHandler statisticsHandler = new StatisticsHandler();
    Javalin app = Javalin.create().port(0).embeddedServer(new EmbeddedJettyFactory(() -> {
        Server server = new Server();
        server.setHandler(statisticsHandler);
        return server;
    })).get("/", ctx -> ctx.result("Hello World")).start();
    String origin = "http://localhost:" + app.port();
    int requests = 5;
    for (int i = 0; i < requests; i++) {
        assertThat(Unirest.get(origin + "/").asString().getBody(), is("Hello World"));
        assertThat(Unirest.get(origin + "/not_there").asString().getStatus(), is(404));
    }
    assertThat(statisticsHandler.getDispatched(), is(requests * 2));
    assertThat(statisticsHandler.getResponses2xx(), is(requests));
    assertThat(statisticsHandler.getResponses4xx(), is(requests));
    app.stop();
}
Also used : EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) CoreMatchers.is(org.hamcrest.CoreMatchers.is) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) Unirest(com.mashape.unirest.http.Unirest) RequestLog(org.eclipse.jetty.server.RequestLog) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) Test(org.junit.Test) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Server(org.eclipse.jetty.server.Server) Server(org.eclipse.jetty.server.Server) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Test(org.junit.Test)

Example 4 with EmbeddedJettyFactory

use of io.javalin.embeddedserver.jetty.EmbeddedJettyFactory in project javalin by tipsy.

the class TestCustomJetty method test_embeddedServer_withHandlerChain.

@Test
public void test_embeddedServer_withHandlerChain() throws Exception {
    AtomicLong logCount = new AtomicLong(0);
    RequestLog requestLog = (request, response) -> logCount.incrementAndGet();
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    requestLogHandler.setRequestLog(requestLog);
    StatisticsHandler handlerChain = new StatisticsHandler();
    handlerChain.setHandler(requestLogHandler);
    Javalin app = Javalin.create().port(0).embeddedServer(new EmbeddedJettyFactory(() -> {
        Server server = new Server();
        server.setHandler(handlerChain);
        return server;
    })).get("/", ctx -> ctx.result("Hello World")).start();
    String origin = "http://localhost:" + app.port();
    int requests = 10;
    for (int i = 0; i < requests; i++) {
        assertThat(Unirest.get(origin + "/").asString().getBody(), is("Hello World"));
        assertThat(Unirest.get(origin + "/not_there").asString().getStatus(), is(404));
    }
    assertThat(handlerChain.getDispatched(), is(requests * 2));
    assertThat(handlerChain.getResponses2xx(), is(requests));
    assertThat(handlerChain.getResponses4xx(), is(requests));
    assertThat(logCount.get(), is((long) (requests * 2)));
    app.stop();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) Unirest(com.mashape.unirest.http.Unirest) RequestLog(org.eclipse.jetty.server.RequestLog) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) Test(org.junit.Test) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Server(org.eclipse.jetty.server.Server) EmbeddedJettyFactory(io.javalin.embeddedserver.jetty.EmbeddedJettyFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) RequestLog(org.eclipse.jetty.server.RequestLog) Server(org.eclipse.jetty.server.Server) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Test(org.junit.Test)

Aggregations

EmbeddedJettyFactory (io.javalin.embeddedserver.jetty.EmbeddedJettyFactory)4 Server (org.eclipse.jetty.server.Server)4 Test (org.junit.Test)4 Unirest (com.mashape.unirest.http.Unirest)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 RequestLog (org.eclipse.jetty.server.RequestLog)2 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)2 StatisticsHandler (org.eclipse.jetty.server.handler.StatisticsHandler)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 SimpleHttpClient (io.javalin.util.SimpleHttpClient)1 IOException (java.io.IOException)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ForkJoinPool (java.util.concurrent.ForkJoinPool)1 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)1 Ignore (org.junit.Ignore)1