Search in sources :

Example 41 with Routing

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

the class FileSystemContentHandlerTest method serveIndex.

@Test
public void serveIndex() throws Exception {
    Routing routing = Routing.builder().register(StaticContentSupport.builder(folder.root().toPath()).welcomeFileName("index.html").contentType("css", MediaType.TEXT_PLAIN).build()).build();
    // /
    TestResponse response = TestClient.create(routing).path("/").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(responseToString(response), is("Index HTML"));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(MediaType.TEXT_HTML.toString()));
    // /other
    response = TestClient.create(routing).path("/other").get();
    assertThat(response.status(), is(Http.Status.MOVED_PERMANENTLY_301));
    assertThat(response.headers().first(Http.Header.LOCATION).orElse(null), is("/other/"));
    // /other/
    response = TestClient.create(routing).path("/other/").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(responseToString(response), is("Index HTML"));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(MediaType.TEXT_HTML.toString()));
    // /css/
    response = TestClient.create(routing).path("/css/").get();
    assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
    // /css/a.css
    response = TestClient.create(routing).path("/css/a.css").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(responseToString(response), is("A CSS"));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(MediaType.TEXT_PLAIN.toString()));
}
Also used : Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

Example 42 with Routing

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

the class TestClientTest method advancingToDefaultErrorHandler.

@Test
public void advancingToDefaultErrorHandler() throws Exception {
    StringBuffer sb = new StringBuffer();
    HttpException exception = new HttpException("test-exception", Http.ResponseStatus.create(777));
    Routing routing = Routing.builder().any((req, res) -> {
        sb.append("any-");
        req.next(exception);
    }).error(IllegalStateException.class, (req, res, ex) -> {
        fail("Should not be called");
    }).error(Exception.class, (req, res, ex) -> {
        sb.append("exceptionHandler-");
        req.next(ex);
    }).error(IllegalArgumentException.class, (req, res, ex) -> {
        fail("Should not be called");
    }).error(HttpException.class, (req, res, ex) -> {
        sb.append("httpExceptionHandler-");
        req.next();
    }).error(Throwable.class, (req, res, ex) -> {
        sb.append("throwableHandler");
        req.next();
    }).build();
    TestResponse response = TestClient.create(routing).path("/anything/anywhere").get();
    assertThat(sb.toString(), is("any-exceptionHandler-httpExceptionHandler-throwableHandler"));
    assertThat(response.status().code(), is(777));
}
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) Test(org.junit.jupiter.api.Test)

Example 43 with Routing

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

the class TestClientTest method subRoutes.

@Test
public void subRoutes() throws Exception {
    StringBuffer sb = new StringBuffer();
    Routing routing = Routing.builder().get("/foo/{name}", (req, res) -> {
        sb.append("foo-get:").append(req.path().param("name"));
        res.send();
    }).register("/bar/{name}", config -> {
        config.get("/baz", (req, res) -> {
            sb.append("baz-get:").append(req.path().absolute().param("name"));
            res.send();
        }).get("/{id}", (req, res) -> {
            sb.append("bar-get:").append(req.path().absolute().param("name")).append(':').append(req.path().param("id"));
            res.send();
        });
    }).post("/foo/{name}", (req, res) -> {
        sb.append("foo-post:").append(req.path().param("name"));
        res.send();
    }).build();
    TestResponse response = TestClient.create(routing).path("/foo/a").get();
    assertThat(sb.toString(), is("foo-get:a"));
    sb.setLength(0);
    response = TestClient.create(routing).path("/foo/a").post();
    assertThat(sb.toString(), is("foo-post:a"));
    sb.setLength(0);
    response = TestClient.create(routing).path("/bar/n/baz").get();
    assertThat(sb.toString(), is("baz-get:n"));
    sb.setLength(0);
    response = TestClient.create(routing).path("/bar/n/kuk").get();
    assertThat(sb.toString(), is("bar-get:n:kuk"));
}
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 44 with Routing

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

the class TestClientTest method multipleErrorEndingRequests.

@Test
public void multipleErrorEndingRequests() throws Exception {
    StringBuffer sb = new StringBuffer();
    Routing routing = Routing.builder().any((req, res) -> {
        sb.append("any-");
        switch(req.queryParams().first("a").orElse("")) {
            case "IllegalArgumentException":
                throw new IllegalArgumentException();
            case "IllegalStateException":
                throw new IllegalStateException();
        }
    }).error(Throwable.class, (req, res, ex) -> {
        sb.append("throwableHandler-");
        req.next();
    }).error(NumberFormatException.class, (req, res, ex) -> {
        fail("Should not be called!");
    }).error(IllegalArgumentException.class, (req, res, ex) -> {
        sb.append("IllegalArgumentExceptionHandler-");
        req.next();
    }).error(IllegalStateException.class, (req, res, ex) -> {
        sb.append("IllegalStateExceptionHandler-");
        req.next();
    }).build();
    TestResponse responseIse = TestClient.create(routing).path("/1").queryParameter("a", IllegalStateException.class.getSimpleName()).get();
    TestResponse responseIae = TestClient.create(routing).path("/2").queryParameter("a", IllegalArgumentException.class.getSimpleName()).get();
    assertThat(sb.toString(), is("any-throwableHandler-IllegalStateExceptionHandler-any-throwableHandler-" + "IllegalArgumentExceptionHandler-"));
    assertThat(responseIse.status(), is(Http.Status.INTERNAL_SERVER_ERROR_500));
    assertThat(responseIae.status(), is(Http.Status.INTERNAL_SERVER_ERROR_500));
}
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 45 with Routing

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

the class SecureServer method createWebServer.

/**
 * Create the web server.
 */
private static WebServer createWebServer(Config config, Security security) {
    Routing routing = Routing.builder().register(WebSecurity.create(security).securityDefaults(WebSecurity.authenticate())).register(new RestService()).build();
    WebServer webServer = WebServer.create(routing, config);
    webServer.start().thenAccept(s -> {
        System.out.println("Web server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Web server startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    return webServer;
}
Also used : Security(io.helidon.security.Security) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) Context(io.helidon.common.context.Context) ResponseHelper.complete(io.helidon.grpc.core.ResponseHelper.complete) Channel(io.grpc.Channel) StringService(io.helidon.grpc.examples.common.StringService) StreamObserver(io.grpc.stub.StreamObserver) GrpcHelper(io.helidon.grpc.core.GrpcHelper) ServerResponse(io.helidon.webserver.ServerResponse) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Service(io.helidon.webserver.Service) Status(io.grpc.Status) LogConfig(io.helidon.common.LogConfig) Http(io.helidon.common.http.Http) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) GrpcClientSecurity(io.helidon.security.integration.grpc.GrpcClientSecurity) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) SecurityContext(io.helidon.security.SecurityContext) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) Greet(io.helidon.grpc.examples.common.Greet) ServerRequest(io.helidon.webserver.ServerRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) Strings(io.helidon.grpc.examples.common.Strings) GrpcService(io.helidon.grpc.server.GrpcService) StringServiceGrpc(io.helidon.grpc.examples.common.StringServiceGrpc) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) GrpcServer(io.helidon.grpc.server.GrpcServer) Routing(io.helidon.webserver.Routing) WebServer(io.helidon.webserver.WebServer) GrpcRouting(io.helidon.grpc.server.GrpcRouting) 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