Skip to content

Commit 422e3b3

Browse files
sunghyukbrian-brazil
authored andcommitted
fix NPE in PrometheusMvcEndpoint.value() (#256)
1 parent 3d9a330 commit 422e3b3

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

simpleclient_spring_boot/src/main/java/io/prometheus/client/spring/boot/PrometheusMvcEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public PrometheusMvcEndpoint(PrometheusEndpoint delegate) {
2828
)
2929
@ResponseBody
3030
public ResponseEntity value(
31-
@RequestParam(value = "name[]", required = false) Set<String> name) {
31+
@RequestParam(value = "name[]", required = false, defaultValue = "") Set<String> name) {
3232
if (!getDelegate().isEnabled()) {
3333
// Shouldn't happen - MVC endpoint shouldn't be registered when delegate's
3434
// disabled
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.prometheus.client.spring.boot;
2+
3+
import io.prometheus.client.exporter.common.TextFormat;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.boot.test.SpringApplicationConfiguration;
9+
import org.springframework.boot.test.TestRestTemplate;
10+
import org.springframework.boot.test.WebIntegrationTest;
11+
import org.springframework.http.HttpEntity;
12+
import org.springframework.http.HttpHeaders;
13+
import org.springframework.http.HttpMethod;
14+
import org.springframework.http.HttpStatus;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17+
import org.springframework.web.client.RestTemplate;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertTrue;
21+
22+
@RunWith(SpringJUnit4ClassRunner.class)
23+
@SpringApplicationConfiguration(DummyBootApplication.class)
24+
@WebIntegrationTest(randomPort = true)
25+
public class PrometheusMvcEndpointTest {
26+
27+
@Value("${local.server.port}")
28+
int localServerPort;
29+
30+
RestTemplate template = new TestRestTemplate();
31+
32+
@Test
33+
public void testNameParamIsNull() throws Exception {
34+
ResponseEntity metricsResponse = template.exchange(getBaseUrl() + "/prometheus", HttpMethod.GET, getEntity(), String.class);
35+
36+
assertEquals(HttpStatus.OK, metricsResponse.getStatusCode());
37+
assertTrue(StringUtils.deleteWhitespace(TextFormat.CONTENT_TYPE_004).equals(metricsResponse.getHeaders().getContentType().toString()));
38+
}
39+
40+
@Test
41+
public void testNameParamIsNotNull() {
42+
ResponseEntity metricsResponse = template.exchange(getBaseUrl() + "/prometheus?name[]=foo_bar", HttpMethod.GET, getEntity(), String.class);
43+
44+
assertEquals(HttpStatus.OK, metricsResponse.getStatusCode());
45+
assertTrue(StringUtils.deleteWhitespace(TextFormat.CONTENT_TYPE_004).equals(metricsResponse.getHeaders().getContentType().toString()));
46+
}
47+
48+
public HttpEntity getEntity() {
49+
HttpHeaders headers = new HttpHeaders();
50+
headers.set("Accept", "text/plain; version=0.0.4; charset=utf-8");
51+
return new HttpEntity(headers);
52+
}
53+
54+
private String getBaseUrl() {
55+
return "http://localhost:" + localServerPort;
56+
}
57+
}

0 commit comments

Comments
 (0)