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