use of io.helidon.webserver.Routing in project helidon by oracle.
the class CommentServiceTest method testRouting.
@Test
public void testRouting() throws Exception {
Routing routing = Routing.builder().register(new CommentService()).build();
TestResponse response = TestClient.create(routing).path("one").get();
assertEquals(Http.Status.OK_200, response.status());
// Add first comment
response = TestClient.create(routing).path("one").post(MediaPublisher.create(MediaType.TEXT_PLAIN, "aaa"));
assertEquals(Http.Status.OK_200, response.status());
response = TestClient.create(routing).path("one").get();
assertEquals(Http.Status.OK_200, response.status());
byte[] data = response.asBytes().toCompletableFuture().get();
assertEquals("anonymous: aaa\n", new String(data, StandardCharsets.UTF_8));
// Add second comment
response = TestClient.create(routing).path("one").post(MediaPublisher.create(MediaType.TEXT_PLAIN, "bbb"));
assertEquals(Http.Status.OK_200, response.status());
response = TestClient.create(routing).path("one").get();
assertEquals(Http.Status.OK_200, response.status());
data = response.asBytes().toCompletableFuture().get();
assertEquals("anonymous: aaa\nanonymous: bbb\n", new String(data, StandardCharsets.UTF_8));
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class UserFilterTest method filter.
@Test
public void filter() throws Exception {
AtomicReference<User> userReference = new AtomicReference<>();
Routing routing = Routing.builder().any(new UserFilter()).any((req, res) -> {
userReference.set(req.context().get(User.class).orElse(null));
res.send();
}).build();
TestResponse response = TestClient.create(routing).path("/").get();
assertEquals(User.ANONYMOUS, userReference.get());
response = TestClient.create(routing).path("/").header("Cookie", "Unauthenticated-User-Alias=Foo").get();
assertEquals("Foo", userReference.get().getAlias());
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class Main method firstRouting.
// ---------------- EXAMPLES
/**
* True heart of WebServer API is {@link Routing}. It provides fluent way how to assign custom {@link Handler} to the routing
* rule. The rule consists from two main factors - <i>HTTP method</i> and <i>path pattern</i>.
* <p>
* The (route) {@link Handler} is a functional interface which process HTTP {@link io.helidon.webserver.ServerRequest request} and
* writes to the {@link io.helidon.webserver.ServerResponse response}.
*/
public void firstRouting() {
Routing routing = Routing.builder().post("/post-endpoint", (req, res) -> res.status(Http.Status.CREATED_201).send()).get("/get-endpoint", (req, res) -> res.status(Http.Status.NO_CONTENT_204).send("Hello World!")).build();
startServer(routing);
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class Main method readContentEntity.
/**
* Request payload (body/entity) is represented by {@link java.util.concurrent.Flow.Publisher Flow.Publisher}
* of {@link DataChunk RequestChunks} to enable reactive processing of the content of any size.
* But it is more convenient to process entity in some type specific form. WebServer supports few types which can be
* used te read the whole entity:
* <ul>
* <li>{@code byte[]}</li>
* <li>{@code String}</li>
* <li>{@code InputStream}</li>
* </ul>
* <p>
* Similar approach is used for the response entity.
*/
public void readContentEntity() {
Routing routing = Routing.builder().post("/foo", (req, res) -> {
req.content().as(String.class).whenComplete((data, thr) -> {
if (thr == null) {
System.out.println("/foo DATA: " + data);
res.send(data);
} else {
res.status(Http.Status.BAD_REQUEST_400);
}
});
}).post("/bar", Handler.create(String.class, (req, res, data) -> {
System.out.println("/foo DATA: " + data);
res.send(data);
})).build();
startServer(routing);
}
use of io.helidon.webserver.Routing in project helidon by oracle.
the class Main method organiseCode.
/**
* Larger applications with many routing rules can cause complicated readability (maintainability) if all rules are
* defined in a single fluent code. It is possible to register {@link io.helidon.webserver.Service Service} and organise
* the code into services and resources. {@code Service} is an interface which can register more routing rules (routes).
*/
public void organiseCode() {
Routing routing = Routing.builder().register("/catalog-context-path", new Catalog()).build();
startServer(routing);
}
Aggregations