From acc18753735dd06b45eaf173d0f33a483654b391 Mon Sep 17 00:00:00 2001 From: Santiago Pericasgeertsen Date: Tue, 16 May 2023 08:48:30 -0400 Subject: [PATCH] Convenient method status(int) in ServerResponse. New test. --- .../integration/server/StatusCodeTest.java | 53 +++++++++++++++++++ .../nima/webserver/http/ServerResponse.java | 10 ++++ 2 files changed, 63 insertions(+) create mode 100644 nima/tests/integration/webserver/webserver/src/test/java/io/helidon/nima/tests/integration/server/StatusCodeTest.java diff --git a/nima/tests/integration/webserver/webserver/src/test/java/io/helidon/nima/tests/integration/server/StatusCodeTest.java b/nima/tests/integration/webserver/webserver/src/test/java/io/helidon/nima/tests/integration/server/StatusCodeTest.java new file mode 100644 index 00000000000..f74d203611f --- /dev/null +++ b/nima/tests/integration/webserver/webserver/src/test/java/io/helidon/nima/tests/integration/server/StatusCodeTest.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.nima.tests.integration.server; + +import io.helidon.common.http.Http; +import io.helidon.nima.testing.junit5.webserver.ServerTest; +import io.helidon.nima.testing.junit5.webserver.SetUpRoute; +import io.helidon.nima.webclient.ClientResponse; +import io.helidon.nima.webclient.http1.Http1Client; +import io.helidon.nima.webserver.http.HttpRules; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Tests status codes as integers. + */ +@ServerTest +class StatusCodeTest { + + private final Http1Client client; + + StatusCodeTest(Http1Client client) { + this.client = client; + } + + @SetUpRoute + static void routing(HttpRules rules) { + rules.get("/", (req, res) -> res.status(204).send()); + } + + @Test + void testCode() { + try (ClientResponse response = client.method(Http.Method.GET).request()) { + assertThat(response.status(), is(Http.Status.NO_CONTENT_204)); + } + } +} 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 d791d098dfa..ae562e8cf56 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 @@ -39,6 +39,16 @@ public interface ServerResponse { */ ServerResponse status(Http.Status status); + /** + * Status of the response. + * + * @param status HTTP status as integer + * @return this instance + */ + default ServerResponse status(int status) { + return status(Http.Status.create(status)); + } + /** * Configured HTTP status, if not configured, returns {@link Http.Status#OK_200}. *