From c09a47afd5adf75d6d398ddf62e55ba7ecb37e63 Mon Sep 17 00:00:00 2001 From: Tim Quinn Date: Tue, 21 Jan 2025 09:59:55 -0600 Subject: [PATCH] Restore setting of no-cache header in health HTTP responses; add test (#9671) --- .../observe/health/HealthHandler.java | 4 +- .../observe/health/TestNoCacheHeaders.java | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 webserver/observe/health/src/test/java/io/helidon/webserver/observe/health/TestNoCacheHeaders.java diff --git a/webserver/observe/health/src/main/java/io/helidon/webserver/observe/health/HealthHandler.java b/webserver/observe/health/src/main/java/io/helidon/webserver/observe/health/HealthHandler.java index e248de26e85..bfc4960ccbf 100644 --- a/webserver/observe/health/src/main/java/io/helidon/webserver/observe/health/HealthHandler.java +++ b/webserver/observe/health/src/main/java/io/helidon/webserver/observe/health/HealthHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023 Oracle and/or its affiliates. + * Copyright (c) 2022, 2025 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. @@ -22,6 +22,7 @@ import io.helidon.health.HealthCheck; import io.helidon.health.HealthCheckResponse; +import io.helidon.http.HeaderValues; import io.helidon.http.HtmlEncoder; import io.helidon.http.Status; import io.helidon.http.media.EntityWriter; @@ -84,6 +85,7 @@ public void handle(ServerRequest req, ServerResponse res) { }; res.status(responseStatus); + res.header(HeaderValues.CACHE_NO_CACHE); if (details) { entityWriter.write(JsonpSupport.JSON_OBJECT_TYPE, diff --git a/webserver/observe/health/src/test/java/io/helidon/webserver/observe/health/TestNoCacheHeaders.java b/webserver/observe/health/src/test/java/io/helidon/webserver/observe/health/TestNoCacheHeaders.java new file mode 100644 index 00000000000..f411b6e3c0b --- /dev/null +++ b/webserver/observe/health/src/test/java/io/helidon/webserver/observe/health/TestNoCacheHeaders.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 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.webserver.observe.health; + +import io.helidon.common.testing.http.junit5.HttpHeaderMatcher; +import io.helidon.http.HeaderNames; +import io.helidon.webclient.http1.Http1Client; +import io.helidon.webclient.http1.Http1ClientResponse; +import io.helidon.webserver.testing.junit5.ServerTest; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; + +@ServerTest +class TestNoCacheHeaders { + + private final Http1Client client; + + TestNoCacheHeaders(Http1Client client) { + this.client = client; + } + + @Test + void testNoCacheHeaders() { + try (Http1ClientResponse response = client + .get("/observe/health") + .request()) { + + assertThat("No-cache headers", + response.headers(), + allOf(HttpHeaderMatcher.hasHeader(HeaderNames.CACHE_CONTROL, + "no-cache", + "no-store", + "must-revalidate", + "no-transform"))); + } + } +}