44import static org .assertj .core .api .Assertions .assertThat ;
55import static org .assertj .core .api .Assertions .fail ;
66
7+ import com .google .common .io .Files ;
8+ import com .google .common .io .Resources ;
79import io .prometheus .client .it .common .LogConsumer ;
810import io .prometheus .client .it .common .Volume ;
911import io .prometheus .metrics .expositionformats .generated .com_google_protobuf_4_28_3 .Metrics ;
2527import org .apache .commons .io .IOUtils ;
2628import org .junit .jupiter .api .AfterEach ;
2729import org .junit .jupiter .api .Test ;
30+ import org .junit .jupiter .params .ParameterizedTest ;
31+ import org .junit .jupiter .params .provider .CsvSource ;
32+ import org .junit .jupiter .params .provider .ValueSource ;
2833import org .testcontainers .containers .BindMode ;
2934import org .testcontainers .containers .GenericContainer ;
3035
@@ -68,8 +73,7 @@ public void testOpenMetricsTextFormat() throws IOException {
6873 assertThat (response .getHeader ("Transfer-Encoding" )).isNull ();
6974 assertThat (response .getHeader ("Content-Length" ))
7075 .isEqualTo (Integer .toString (response .body .length ));
71- String bodyString = new String (response .body , UTF_8 );
72- assertThat (bodyString )
76+ assertThat (response .stringBody ())
7377 .contains ("integration_test_info{test_name=\" " + sampleApp + "\" } 1" )
7478 .contains ("temperature_celsius{location=\" inside\" } 23.0" )
7579 .contains ("temperature_celsius{location=\" outside\" } 27.0" )
@@ -91,8 +95,7 @@ public void testPrometheusTextFormat() throws IOException {
9195 assertThat (response .getHeader ("Transfer-Encoding" )).isNull ();
9296 assertThat (response .getHeader ("Content-Length" ))
9397 .isEqualTo (Integer .toString (response .body .length ));
94- String bodyString = new String (response .body , UTF_8 );
95- assertThat (bodyString )
98+ assertThat (response .stringBody ())
9699 .contains ("integration_test_info{test_name=\" " + sampleApp + "\" } 1" )
97100 .contains ("temperature_celsius{location=\" inside\" } 23.0" )
98101 .contains ("temperature_celsius{location=\" outside\" } 27.0" )
@@ -125,13 +128,31 @@ public void testPrometheusProtobufFormat() throws IOException {
125128 while (in .available () > 0 ) {
126129 metrics .add (Metrics .MetricFamily .parseDelimitedFrom (in ));
127130 }
128- assertThat (metrics . size ()). isEqualTo (3 );
131+ assertThat (metrics ). hasSize (3 );
129132 // metrics are sorted by name
130133 assertThat (metrics .get (0 ).getName ()).isEqualTo ("integration_test_info" );
131134 assertThat (metrics .get (1 ).getName ()).isEqualTo ("temperature_celsius" );
132135 assertThat (metrics .get (2 ).getName ()).isEqualTo ("uptime_seconds_total" );
133136 }
134137
138+ @ ParameterizedTest
139+ @ CsvSource ({
140+ "openmetrics, debug-openmetrics.txt" ,
141+ "text, debug-text.txt" ,
142+ "prometheus-protobuf, debug-protobuf.txt" ,
143+ })
144+ public void testPrometheusProtobufDebugFormat (String format , String expected ) throws IOException {
145+ sampleAppContainer
146+ .withCommand ("java" , "-jar" , "/app/" + sampleApp + ".jar" , "9400" , "success" )
147+ .start ();
148+ Response response = scrape ("GET" , "debug=" + format );
149+ assertThat (response .status ).isEqualTo (200 );
150+ assertContentType (
151+ "text/plain;charset=utf-8" ,
152+ response .getHeader ("Content-Type" ));
153+ assertThat (response .stringBody ().trim ()).isEqualTo (Resources .toString (Resources .getResource (expected ), UTF_8 ).trim ());
154+ }
155+
135156 @ Test
136157 public void testCompression () throws IOException {
137158 sampleAppContainer
@@ -159,11 +180,7 @@ public void testCompression() throws IOException {
159180 assertContentType (
160181 "application/openmetrics-text; version=1.0.0; charset=utf-8" ,
161182 response .getHeader ("Content-Type" ));
162- String body =
163- new String (
164- IOUtils .toByteArray (new GZIPInputStream (new ByteArrayInputStream (response .body ))),
165- UTF_8 );
166- assertThat (body ).contains ("uptime_seconds_total 17.0" );
183+ assertThat (response .gzipBody ()).contains ("uptime_seconds_total 17.0" );
167184 }
168185
169186 @ Test
@@ -173,7 +190,7 @@ public void testErrorHandling() throws IOException {
173190 .start ();
174191 Response response = scrape ("GET" , "" );
175192 assertThat (response .status ).isEqualTo (500 );
176- assertThat (new String ( response .body , UTF_8 )).contains ("Simulating an error." );
193+ assertThat (response .stringBody ( )).contains ("Simulating an error." );
177194 }
178195
179196 protected boolean headReturnsContentLength () {
@@ -195,7 +212,7 @@ public void testHeadRequest() throws IOException {
195212 } else {
196213 assertThat (headResponse .getHeader ("Content-Length" )).isNull ();
197214 }
198- assertThat (headResponse .body . length ). isZero ();
215+ assertThat (headResponse .body ). isEmpty ();
199216 }
200217
201218 @ Test
@@ -206,8 +223,7 @@ public void testDebug() throws IOException {
206223 Response response = scrape ("GET" , "debug=openmetrics" );
207224 assertThat (response .status ).isEqualTo (200 );
208225 assertContentType ("text/plain; charset=utf-8" , response .getHeader ("Content-Type" ));
209- String bodyString = new String (response .body , UTF_8 );
210- assertThat (bodyString )
226+ assertThat (response .stringBody ())
211227 .contains ("uptime_seconds_total 17.0" )
212228 .contains ("# UNIT uptime_seconds seconds" );
213229 }
@@ -227,8 +243,7 @@ public void testNameFilter() throws IOException {
227243 assertContentType (
228244 "application/openmetrics-text; version=1.0.0; charset=utf-8" ,
229245 response .getHeader ("Content-Type" ));
230- String bodyString = new String (response .body , UTF_8 );
231- assertThat (bodyString )
246+ assertThat (response .stringBody ())
232247 .contains ("integration_test_info{test_name=\" " + sampleApp + "\" } 1" )
233248 .contains ("uptime_seconds_total 17.0" )
234249 .doesNotContain ("temperature_celsius" );
@@ -251,7 +266,7 @@ public void testEmptyResponseOpenMetrics() throws IOException {
251266 response .getHeader ("Content-Type" ));
252267 assertThat (response .getHeader ("Content-Length" ))
253268 .isEqualTo (Integer .toString (response .body .length ));
254- assertThat (new String ( response .body , UTF_8 )).isEqualTo ("# EOF\n " );
269+ assertThat (response .stringBody ( )).isEqualTo ("# EOF\n " );
255270 }
256271
257272 @ Test
@@ -267,7 +282,7 @@ public void testEmptyResponseText() throws IOException {
267282 != null ) { // HTTPServer does not send a zero content length, which is ok
268283 assertThat (response .getHeader ("Content-Length" )).isEqualTo ("0" );
269284 }
270- assertThat (response .body . length ). isZero ();
285+ assertThat (response .body ). isEmpty ();
271286 }
272287
273288 @ Test
@@ -285,7 +300,7 @@ public void testEmptyResponseProtobuf() throws IOException {
285300 assertContentType (
286301 "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited" ,
287302 response .getHeader ("Content-Type" ));
288- assertThat (response .body . length ). isZero ();
303+ assertThat (response .body ). isEmpty ();
289304 }
290305
291306 @ Test
@@ -303,11 +318,7 @@ public void testEmptyResponseGzipOpenMetrics() throws IOException {
303318 "gzip" );
304319 assertThat (response .status ).isEqualTo (200 );
305320 assertThat (response .getHeader ("Content-Encoding" )).isEqualTo ("gzip" );
306- String body =
307- new String (
308- IOUtils .toByteArray (new GZIPInputStream (new ByteArrayInputStream (response .body ))),
309- UTF_8 );
310- assertThat (body ).isEqualTo ("# EOF\n " );
321+ assertThat (response .gzipBody ()).isEqualTo ("# EOF\n " );
311322 }
312323
313324 @ Test
@@ -318,11 +329,7 @@ public void testEmptyResponseGzipText() throws IOException {
318329 Response response = scrape ("GET" , nameParam ("none_existing" ), "Accept-Encoding" , "gzip" );
319330 assertThat (response .status ).isEqualTo (200 );
320331 assertThat (response .getHeader ("Content-Encoding" )).isEqualTo ("gzip" );
321- String body =
322- new String (
323- IOUtils .toByteArray (new GZIPInputStream (new ByteArrayInputStream (response .body ))),
324- UTF_8 );
325- assertThat (body .length ()).isZero ();
332+ assertThat (response .gzipBody ()).isEmpty ();
326333 }
327334
328335 private String nameParam (String name ) throws UnsupportedEncodingException {
@@ -411,5 +418,15 @@ private String getHeader(String name) {
411418 // HTTP headers are case-insensitive
412419 return headers .get (name .toLowerCase (Locale .ROOT ));
413420 }
421+
422+ private String stringBody () {
423+ return new String (body , UTF_8 );
424+ }
425+
426+ private String gzipBody () throws IOException {
427+ return new String (
428+ IOUtils .toByteArray (new GZIPInputStream (new ByteArrayInputStream (body ))),
429+ UTF_8 );
430+ }
414431 }
415432}
0 commit comments