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));
}
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));
}
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"));
}
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));
}
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));
}
Aggregations