diff --git a/nima/tests/integration/webserver/webserver/src/test/java/io/helidon/nima/tests/integration/server/GetTest.java b/nima/tests/integration/webserver/webserver/src/test/java/io/helidon/nima/tests/integration/server/GetTest.java index cf8b7ab5cea..8849e06e260 100644 --- a/nima/tests/integration/webserver/webserver/src/test/java/io/helidon/nima/tests/integration/server/GetTest.java +++ b/nima/tests/integration/webserver/webserver/src/test/java/io/helidon/nima/tests/integration/server/GetTest.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.UncheckedIOException; +import java.util.Optional; import java.util.Random; import io.helidon.common.http.Headers; @@ -39,6 +40,7 @@ import org.junit.jupiter.api.Test; import static io.helidon.common.http.Http.Header.CONTENT_LENGTH; +import static io.helidon.common.http.Http.Method.GET; import static io.helidon.common.testing.http.junit5.HttpHeaderMatcher.hasHeader; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -68,11 +70,12 @@ class GetTest { @SetUpRoute static void routing(HttpRouting.Builder router) { - router.route(Http.Method.GET, "/string", Routes::string) - .route(Http.Method.GET, "/bytes", Routes::bytes) - .route(Http.Method.GET, "/chunked", Routes::chunked) - .route(Http.Method.GET, "/headers", Routes::headers) - .route(Http.Method.GET, "/close", Routes::close); + router.route(GET, "/string", Routes::string) + .route(GET, "/bytes", Routes::bytes) + .route(GET, "/chunked", Routes::chunked) + .route(GET, "/headers", Routes::headers) + .route(GET, "/close", Routes::close) + .route(GET, "/optional", Routes::optional); } @Test @@ -151,7 +154,39 @@ void testCloseRoute() { } } + @Test + void testOptionalResponseWithValue() { + try (Http1ClientResponse response = client.get("/optional") + .request()) { + + assertThat(response.status(), is(Http.Status.OK_200)); + String entity = response.entity().as(String.class); + assertThat(entity, is("return value")); + } + } + + @Test + void testOptionalResponseEmpty() { + try (Http1ClientResponse response = client.get("/optional") + .queryParam("empty", "true") + .request()) { + + assertThat(response.status(), is(Http.Status.NOT_FOUND_404)); + assertThat(response.headers(), hasHeader(HeaderValues.CONTENT_LENGTH_ZERO)); + } + } + private static class Routes { + public static void optional(ServerRequest req, ServerResponse res) { + String empty = req.query().first("empty").orElse("false"); + + if ("false".equals(empty)) { + res.send(Optional.of("return value")); + } else { + res.send(Optional.empty()); + } + } + private static void close(ServerRequest req, ServerResponse res) { res.header(HeaderValues.CONNECTION_CLOSE); res.send("Hello"); diff --git a/nima/webserver/webserver/src/main/java/io/helidon/nima/webserver/http/ServerResponse.java b/nima/webserver/webserver/src/main/java/io/helidon/nima/webserver/http/ServerResponse.java index be0232b997d..e9c749fc220 100644 --- a/nima/webserver/webserver/src/main/java/io/helidon/nima/webserver/http/ServerResponse.java +++ b/nima/webserver/webserver/src/main/java/io/helidon/nima/webserver/http/ServerResponse.java @@ -17,9 +17,11 @@ package io.helidon.nima.webserver.http; import java.io.OutputStream; +import java.util.Optional; import io.helidon.common.http.Http; import io.helidon.common.http.Http.HeaderName; +import io.helidon.common.http.NotFoundException; import io.helidon.common.http.ServerResponseHeaders; import io.helidon.common.uri.UriQuery; @@ -97,6 +99,15 @@ default ServerResponse header(String name, String... values) { */ void send(Object entity); + /** + * Send an entity if present, throw {@link io.helidon.common.http.NotFoundException} if empty. + * + * @param entity entity as an optional + */ + default void send(Optional entity) { + send(entity.orElseThrow(() -> new NotFoundException(""))); + } + /** * Whether this response has been sent. *