Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x: Support for optional entity in Níma. #5200

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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