diff --git a/simpleclient_spring_boot/src/main/java/io/prometheus/client/spring/boot/PrometheusMvcEndpoint.java b/simpleclient_spring_boot/src/main/java/io/prometheus/client/spring/boot/PrometheusMvcEndpoint.java index cde701ad4..35ec57bdc 100644 --- a/simpleclient_spring_boot/src/main/java/io/prometheus/client/spring/boot/PrometheusMvcEndpoint.java +++ b/simpleclient_spring_boot/src/main/java/io/prometheus/client/spring/boot/PrometheusMvcEndpoint.java @@ -28,7 +28,7 @@ public PrometheusMvcEndpoint(PrometheusEndpoint delegate) { ) @ResponseBody public ResponseEntity value( - @RequestParam(value = "name[]", required = false) Set name) { + @RequestParam(value = "name[]", required = false, defaultValue = "") Set name) { if (!getDelegate().isEnabled()) { // Shouldn't happen - MVC endpoint shouldn't be registered when delegate's // disabled diff --git a/simpleclient_spring_boot/src/test/java/io/prometheus/client/spring/boot/PrometheusMvcEndpointTest.java b/simpleclient_spring_boot/src/test/java/io/prometheus/client/spring/boot/PrometheusMvcEndpointTest.java new file mode 100644 index 000000000..34765f0b7 --- /dev/null +++ b/simpleclient_spring_boot/src/test/java/io/prometheus/client/spring/boot/PrometheusMvcEndpointTest.java @@ -0,0 +1,57 @@ +package io.prometheus.client.spring.boot; + +import io.prometheus.client.exporter.common.TextFormat; +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.web.client.RestTemplate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(DummyBootApplication.class) +@WebIntegrationTest(randomPort = true) +public class PrometheusMvcEndpointTest { + + @Value("${local.server.port}") + int localServerPort; + + RestTemplate template = new TestRestTemplate(); + + @Test + public void testNameParamIsNull() throws Exception { + ResponseEntity metricsResponse = template.exchange(getBaseUrl() + "/prometheus", HttpMethod.GET, getEntity(), String.class); + + assertEquals(HttpStatus.OK, metricsResponse.getStatusCode()); + assertTrue(StringUtils.deleteWhitespace(TextFormat.CONTENT_TYPE_004).equals(metricsResponse.getHeaders().getContentType().toString())); + } + + @Test + public void testNameParamIsNotNull() { + ResponseEntity metricsResponse = template.exchange(getBaseUrl() + "/prometheus?name[]=foo_bar", HttpMethod.GET, getEntity(), String.class); + + assertEquals(HttpStatus.OK, metricsResponse.getStatusCode()); + assertTrue(StringUtils.deleteWhitespace(TextFormat.CONTENT_TYPE_004).equals(metricsResponse.getHeaders().getContentType().toString())); + } + + public HttpEntity getEntity() { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", "text/plain; version=0.0.4; charset=utf-8"); + return new HttpEntity(headers); + } + + private String getBaseUrl() { + return "http://localhost:" + localServerPort; + } +} \ No newline at end of file