diff options
author | Jon Marius Venstad <jonmv@users.noreply.github.com> | 2023-10-05 18:32:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-05 18:32:29 +0200 |
commit | d87ecbc6149325761d1e8e0f1e8c8c988150197b (patch) | |
tree | f088657bcf330ea4c32ae17594eee84baa6d8fa6 | |
parent | 1ddfd644fb6f0d1e67feca079a0b21284e12aff1 (diff) | |
parent | 652fff958ced79750b4f7471552691bd0ae99d91 (diff) |
Merge pull request #28810 from vespa-engine/jonmv/reply-to-head-and-options-in-badge-handler
Reply to HEAD and OPTIONS in badge handler
2 files changed, 33 insertions, 11 deletions
diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java index c6eaf5abef7..347f56efe58 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java @@ -2,6 +2,7 @@ package com.yahoo.vespa.hosted.controller.restapi.deployment; import com.yahoo.config.provision.ApplicationId; +import com.yahoo.container.jdisc.EmptyResponse; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.container.jdisc.ThreadedHttpRequestHandler; @@ -53,7 +54,12 @@ public class BadgeApiHandler extends ThreadedHttpRequestHandler { Method method = request.getMethod(); try { return switch (method) { - case GET -> get(request); + case OPTIONS -> new SvgHttpResponse("") {{ + headers().add("Allow", "GET, HEAD, OPTIONS"); + headers().add("Access-Control-Allow-Origin", "*"); + headers().add("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS"); + }}; + case HEAD, GET -> get(request); default -> ErrorResponse.methodNotAllowed("Method '" + method + "' is unsupported"); }; } catch (IllegalArgumentException|IllegalStateException e) { @@ -98,20 +104,20 @@ public class BadgeApiHandler extends ThreadedHttpRequestHandler { } private HttpResponse cachedResponse(Key key, Instant now, Supplier<String> badge) { - return svgResponse(badgeCache.compute(key, (__, value) -> { + return new SvgHttpResponse(badgeCache.compute(key, (__, value) -> { return value != null && value.expiry.isAfter(now) ? value : new Value(badge.get(), now); }).badgeSvg); } - private static HttpResponse svgResponse(String svg) { - return new HttpResponse(200) { - @Override public void render(OutputStream outputStream) throws IOException { - outputStream.write(svg.getBytes(UTF_8)); - } - @Override public String getContentType() { - return "image/svg+xml; charset=UTF-8"; - } - }; + private static class SvgHttpResponse extends HttpResponse { + private final String svg; + SvgHttpResponse(String svg) { super(200); this.svg = svg; } + @Override public void render(OutputStream outputStream) throws IOException { + outputStream.write(svg.getBytes(UTF_8)); + } + @Override public String getContentType() { + return "image/svg+xml"; + } } diff --git a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiTest.java b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiTest.java index 5dd57b09af4..f932584b4c0 100644 --- a/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiTest.java +++ b/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiTest.java @@ -1,6 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.controller.restapi.deployment; +import com.yahoo.application.container.handler.Request.Method; import com.yahoo.component.Version; import com.yahoo.vespa.hosted.controller.ControllerTester; import com.yahoo.vespa.hosted.controller.application.pkg.ApplicationPackage; @@ -9,12 +10,15 @@ import com.yahoo.vespa.hosted.controller.deployment.DeploymentContext; import com.yahoo.vespa.hosted.controller.deployment.DeploymentTester; import com.yahoo.vespa.hosted.controller.restapi.ContainerTester; import com.yahoo.vespa.hosted.controller.restapi.ControllerContainerTest; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.time.Duration; +import java.util.List; +import java.util.Map; /** * @author jonmv @@ -79,6 +83,18 @@ public class BadgeApiTest extends ControllerContainerTest { tester.assertResponse(authenticatedRequest("http://localhost:8080/badge/v1/tenant/application/default/production-us-west-1?historyLength=0"), Files.readString(Paths.get(responseFiles + "single-done.svg")), 200); + + tester.assertResponse(() -> authenticatedRequest("http://localhost:8080/badge/v1/tenant/application/default", "", Method.HEAD), + __ -> { }, + 200); + + tester.assertResponse(() -> authenticatedRequest("http://localhost:8080/badge/v1/tenant/application/default", "", Method.OPTIONS), + response -> Assertions.assertEquals(List.of(Map.entry("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS"), + Map.entry("Access-Control-Allow-Origin", "*"), + Map.entry("Allow", "GET, HEAD, OPTIONS"), + Map.entry("Content-Type", "image/svg+xml; charset=UTF-8")), + response.getHeaders().entries()), + 200); } } |