summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonmv <venstad@gmail.com>2023-10-05 17:56:12 +0200
committerjonmv <venstad@gmail.com>2023-10-05 17:56:12 +0200
commit8b2aba4edd1b555a46369c0979cbbf923777fe8c (patch)
tree3775c7243b970a24a5fbe083f5b48dbf95f9985b
parent9dfadb0aeb9c5925b14c7f4c63f04ddb3001f80f (diff)
Reply to HEAD and OPTIONS in badge handler
-rw-r--r--controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java24
-rw-r--r--controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiTest.java14
2 files changed, 27 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..bc69ada8e34 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,8 @@ 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"); }};
+ case HEAD, GET -> get(request);
default -> ErrorResponse.methodNotAllowed("Method '" + method + "' is unsupported");
};
} catch (IllegalArgumentException|IllegalStateException e) {
@@ -98,20 +100,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..e928fec8318 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,16 @@ 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("Allow", "GET, HEAD, OPTIONS"),
+ Map.entry("Content-Type", "image/svg+xml; charset=UTF-8")),
+ response.getHeaders().entries()),
+ 200);
}
}