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