diff --git a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java index 41fdec76d..f0c536fe7 100644 --- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java +++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java @@ -66,11 +66,13 @@ private HTTPServer( this.server = httpServer; this.executorService = executorService; String metricsPath = getMetricsPath(metricsHandlerPath); - registerHandler( - "/", - defaultHandler == null ? new DefaultHandler(metricsPath) : defaultHandler, - authenticator, - authenticatedSubjectAttributeName); + if (!metricsPath.equals("/")) { + registerHandler( + "/", + defaultHandler == null ? new DefaultHandler(metricsPath) : defaultHandler, + authenticator, + authenticatedSubjectAttributeName); + } registerHandler( metricsPath, new MetricsHandler(config, registry), diff --git a/prometheus-metrics-exporter-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/HTTPServerTest.java b/prometheus-metrics-exporter-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/HTTPServerTest.java index 9b7f658de..df53793f8 100644 --- a/prometheus-metrics-exporter-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/HTTPServerTest.java +++ b/prometheus-metrics-exporter-httpserver/src/test/java/io/prometheus/metrics/exporter/httpserver/HTTPServerTest.java @@ -10,25 +10,41 @@ import com.sun.net.httpserver.HttpsConfigurator; import io.prometheus.metrics.model.registry.PrometheusRegistry; import io.prometheus.metrics.model.registry.PrometheusScrapeRequest; +import io.prometheus.metrics.model.snapshots.CounterSnapshot; +import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot; +import io.prometheus.metrics.model.snapshots.Labels; +import io.prometheus.metrics.model.snapshots.MetricMetadata; import io.prometheus.metrics.model.snapshots.MetricSnapshots; import java.io.IOException; import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.nio.charset.StandardCharsets; -import java.security.NoSuchAlgorithmException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.security.Principal; +import java.util.List; import java.util.concurrent.Executors; import javax.net.ssl.SSLContext; import javax.security.auth.Subject; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class HTTPServerTest { + private PrometheusRegistry registry; + + @BeforeEach + void setUp() { + final MetricMetadata metadata = new MetricMetadata("my-counter"); + final CounterDataPointSnapshot dataPointSnapshot = + new CounterDataPointSnapshot(1.0, Labels.EMPTY, null, System.currentTimeMillis()); + + registry = new PrometheusRegistry(); + registry.register(() -> new CounterSnapshot(metadata, List.of(dataPointSnapshot))); + } + @Test - @SuppressWarnings({"removal"}) public void testSubjectDoAs() throws Exception { - final String user = "joe"; final Subject subject = new Subject(); subject.getPrincipals().add(() -> user); @@ -65,61 +81,61 @@ public Result authenticate(HttpExchange exchange) { .authenticatedSubjectAttributeName("aa") .buildAndStart(); - run(server, "204", "/"); + run(server, "/", 204, ""); } - private static void run(HTTPServer server, String expected, String path) throws IOException { - try (Socket socket = new Socket()) { - socket.connect(new InetSocketAddress("localhost", server.getPort())); - - socket - .getOutputStream() - .write(("GET " + path + " HTTP/1.1 \r\n").getBytes(StandardCharsets.UTF_8)); - socket.getOutputStream().write("HOST: localhost \r\n\r\n".getBytes(StandardCharsets.UTF_8)); - socket.getOutputStream().flush(); - - String actualResponse = ""; - byte[] resp = new byte[500]; - int read = socket.getInputStream().read(resp, 0, resp.length); - if (read > 0) { - actualResponse = new String(resp, 0, read, StandardCharsets.UTF_8); - } - assertThat(actualResponse).contains(expected); - } + @Test + void defaultHandler() throws Exception { + run( + HTTPServer.builder().port(0).buildAndStart(), + "/", + 200, + "