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