Search in sources :

Example 71 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class PokemonMain method startServer.

/**
 * Start the server.
 *
 * @return the created {@link io.helidon.webserver.WebServer} instance
 */
static WebServer startServer() {
    // load logging configuration
    LogConfig.configureRuntime();
    // By default this will pick up application.yaml from the classpath
    Config config = isMongo() ? Config.create(ConfigSources.classpath(MONGO_CFG)) : Config.create();
    // Prepare routing for the server
    Routing routing = createRouting(config);
    WebServer server = WebServer.builder(routing).addMediaSupport(JsonpSupport.create()).addMediaSupport(JsonbSupport.create()).config(config.get("server")).tracer(TracerBuilder.create(config.get("tracing")).build()).build();
    // Start the server and print some info.
    server.start().thenAccept(ws -> System.out.println("WEB server is up! http://localhost:" + ws.port() + "/"));
    // Server threads are not daemon. NO need to block. Just react.
    server.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
    return server;
}
Also used : WebServer(io.helidon.webserver.WebServer) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing)

Example 72 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class TestJsonBindingSupport method genericType.

@Test
public void genericType() throws Exception {
    GenericType<List<Person>> personsType = new GenericType<>() {
    };
    final Routing routing = Routing.builder().post("/foo", (req, res) -> {
        req.content().as(personsType).thenAccept(res::send);
    }).build();
    final String personsJson = "[{\"name\":\"Frank\"},{\"name\":\"John\"}]";
    final TestResponse response = TestClient.create(routing, JsonbSupport.create()).path("/foo").post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), personsJson));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(MediaType.APPLICATION_JSON.toString()));
    final String json = response.asString().get(10, TimeUnit.SECONDS);
    assertThat(json, is(personsJson));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TestResponse(io.helidon.webserver.testsupport.TestResponse) TestClient(io.helidon.webserver.testsupport.TestClient) GenericType(io.helidon.common.GenericType) MediaType(io.helidon.common.http.MediaType) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) MediaPublisher(io.helidon.webserver.testsupport.MediaPublisher) Handler(io.helidon.webserver.Handler) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) GenericType(io.helidon.common.GenericType) TestResponse(io.helidon.webserver.testsupport.TestResponse) Routing(io.helidon.webserver.Routing) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 73 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class TestClientTest method nextInErrorWhenHeadersSent.

@Test
public void nextInErrorWhenHeadersSent() throws Exception {
    StringBuffer sb = new StringBuffer();
    Routing routing = Routing.builder().any((req, res) -> {
        sb.append("any-");
        res.status(300);
        try {
            res.send().toCompletableFuture().get();
        } catch (Exception e) {
            fail("Should not have gotten an exception.");
        }
        throw new HttpException("test-exception", Http.ResponseStatus.create(400));
    }).error(Throwable.class, (req, res, ex) -> {
        sb.append("throwableHandler");
        if (!res.headers().whenSent().toCompletableFuture().isDone()) {
            fail("Headers weren't send as expected!");
        }
        req.next();
    }).build();
    TestResponse response = TestClient.create(routing).path("/anything/anywhere").get();
    assertThat(sb.toString(), is("any-throwableHandler"));
    assertThat(response.status().code(), is(300));
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CountDownLatch(java.util.concurrent.CountDownLatch) NotFoundException(io.helidon.webserver.NotFoundException) HttpException(io.helidon.webserver.HttpException) SERVICE_UNAVAILABLE_503(io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) Routing(io.helidon.webserver.Routing) HttpException(io.helidon.webserver.HttpException) NotFoundException(io.helidon.webserver.NotFoundException) HttpException(io.helidon.webserver.HttpException) Test(org.junit.jupiter.api.Test)

Example 74 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class TestClientTest method filtering.

@Test
public void filtering() throws Exception {
    StringBuffer sb = new StringBuffer();
    Routing routing = Routing.builder().any((req, res) -> {
        sb.append("A-");
        req.next();
    }).get("/foo", (req, res) -> {
        sb.append("foo-get");
        res.send();
    }).register(config -> {
        config.get("/bar", (req, res) -> {
            sb.append("B-");
            req.next();
        });
    }).get("/bar", (req, res) -> {
        sb.append("bar-get");
        res.send();
    }).build();
    TestResponse response = TestClient.create(routing).path("/foo").get();
    assertThat(sb.toString(), is("A-foo-get"));
    sb.setLength(0);
    response = TestClient.create(routing).path("/bar").get();
    assertThat(sb.toString(), is("A-B-bar-get"));
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CountDownLatch(java.util.concurrent.CountDownLatch) NotFoundException(io.helidon.webserver.NotFoundException) HttpException(io.helidon.webserver.HttpException) SERVICE_UNAVAILABLE_503(io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 75 with Routing

use of io.helidon.webserver.Routing in project helidon by oracle.

the class TestClientTest method errorHandling.

void errorHandling(RuntimeException exception, Http.Status status, boolean doThrow) throws Exception {
    Routing routing = Routing.builder().any((req, res) -> {
        if (doThrow) {
            throw exception;
        } else {
            req.next(exception);
        }
    }).build();
    TestResponse response = TestClient.create(routing).path("/anything/anywhere").get();
    assertThat(response.status(), is(status));
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) CountDownLatch(java.util.concurrent.CountDownLatch) NotFoundException(io.helidon.webserver.NotFoundException) HttpException(io.helidon.webserver.HttpException) SERVICE_UNAVAILABLE_503(io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) Routing(io.helidon.webserver.Routing) Routing(io.helidon.webserver.Routing)

Aggregations

Routing (io.helidon.webserver.Routing)86 WebServer (io.helidon.webserver.WebServer)38 Config (io.helidon.config.Config)33 Test (org.junit.jupiter.api.Test)32 Http (io.helidon.common.http.Http)23 TimeUnit (java.util.concurrent.TimeUnit)21 LogConfig (io.helidon.common.LogConfig)19 MediaType (io.helidon.common.http.MediaType)19 CoreMatchers.is (org.hamcrest.CoreMatchers.is)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)17 SecurityContext (io.helidon.security.SecurityContext)16 HttpException (io.helidon.webserver.HttpException)15 Optional (java.util.Optional)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)13 SERVICE_UNAVAILABLE_503 (io.helidon.common.http.Http.Status.SERVICE_UNAVAILABLE_503)11 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)11 Security (io.helidon.security.Security)11 StaticContentSupport (io.helidon.webserver.staticcontent.StaticContentSupport)10 TestResponse (io.helidon.webserver.testsupport.TestResponse)10