Search in sources :

Example 76 with Routing

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

the class TestClientTest method implicitNotFound.

@Test
public void implicitNotFound() throws Exception {
    Routing routing = Routing.builder().any("/", (req, res) -> {
        fail("The handler for '/' is not expected to be matched");
    }).build();
    TestResponse response = TestClient.create(routing).path("/nonexisting").get();
    assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
}
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 77 with Routing

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

the class TestClientTest method throwingExceptionInErrorHandler.

@Test
public void throwingExceptionInErrorHandler() throws Exception {
    StringBuffer sb = new StringBuffer();
    Http.ResponseStatus expected = Http.ResponseStatus.create(888);
    Routing routing = Routing.builder().any((req, res) -> {
        sb.append("any-");
        throw new HttpException("original-exception", Http.ResponseStatus.create(777));
    }).error(HttpException.class, (req, res, ex) -> {
        sb.append("httpExceptionHandler-");
        throw new HttpException("unexpected-exception", expected);
    }).error(Throwable.class, (req, res, ex) -> {
        fail("The rest of the handlers were supposed to be skipped due to an unexpected exception being thrown " + "from the error handler.");
        sb.append("throwableHandler-");
        sb.append(ex.getMessage());
        req.next();
    }).build();
    TestResponse response = TestClient.create(routing).path("/anything/anywhere").get();
    assertThat(sb.toString(), is("any-httpExceptionHandler-"));
    assertThat(response.status(), is(expected));
}
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) Http(io.helidon.common.http.Http) HttpException(io.helidon.webserver.HttpException) Test(org.junit.jupiter.api.Test)

Example 78 with Routing

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

the class TestClientTest method singleHandlerTest.

@Test
public void singleHandlerTest() throws Exception {
    StringBuffer sb = new StringBuffer();
    Routing routing = Routing.builder().get("/foo", (req, res) -> {
        sb.append("a");
        res.send();
    }).build();
    TestResponse response = TestClient.create(routing).path("/foo").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(sb.toString(), is("a"));
}
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 79 with Routing

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

the class TestClientTest method userDefinedErrorHandling.

@Test
public void userDefinedErrorHandling() throws Exception {
    StringBuffer sb = new StringBuffer();
    MyTestException exception = new MyTestException("test-exception");
    CountDownLatch latch = new CountDownLatch(1);
    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(MyTestException.class, (req, res, ex) -> {
        sb.append(ex.getMessage());
        res.status(417);
        res.send().whenComplete((serverResponse, throwable) -> {
            sb.append("-complete");
            latch.countDown();
        });
    }).error(RuntimeException.class, (req, res, ex) -> {
        fail("Should not be called");
    }).build();
    TestResponse response = TestClient.create(routing).path("/anything/anywhere").get();
    latch.await(10, TimeUnit.SECONDS);
    assertThat(sb.toString(), is("any-test-exception-complete"));
    assertThat(response.status(), is(Http.Status.EXPECTATION_FAILED_417));
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 80 with Routing

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

the class ClassPathContentHandlerTest method serveFromJarWithWelcome.

@Test
public void serveFromJarWithWelcome() throws Exception {
    Routing routing = Routing.builder().register(StaticContentSupport.builder("/s-internal").welcomeFileName("example-a.txt")).build();
    // /
    TestResponse response = TestClient.create(routing).path("/").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    assertThat(filterResponse(response), is("Example A TXT"));
    assertThat(response.headers().first(Http.Header.CONTENT_TYPE), is(Optional.of(MediaType.TEXT_PLAIN.toString())));
    // /a
    response = TestClient.create(routing).path("/a/").get();
    assertThat(response.status(), is(Http.Status.OK_200));
    // redirect to /a/
    response = TestClient.create(routing).path("/a").get();
    assertThat(response.status(), is(Http.Status.MOVED_PERMANENTLY_301));
    assertThat(response.headers().first("Location"), is(Optional.of("/a/")));
    // another index
    routing = Routing.builder().register(StaticContentSupport.builder("/s-internal").welcomeFileName("example-b.txt")).build();
    // /a/
    response = TestClient.create(routing).path("/a/").get();
    assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
}
Also used : Routing(io.helidon.webserver.Routing) Test(org.junit.jupiter.api.Test)

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