Skip to content

Commit

Permalink
Support for optional entity. (#5200)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
  • Loading branch information
tomas-langer authored Oct 21, 2022
1 parent e1ead71 commit 90554ba
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 90554ba

Please sign in to comment.