|
| 1 | +package io.prometheus.client.exporter; |
| 2 | + |
| 3 | +import io.prometheus.client.Gauge; |
| 4 | +import io.prometheus.client.CollectorRegistry; |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.InetSocketAddress; |
| 7 | +import java.net.URL; |
| 8 | +import java.net.URLConnection; |
| 9 | +import java.util.Scanner; |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +public class TestHTTPServer { |
| 17 | + |
| 18 | + HTTPServer s; |
| 19 | + |
| 20 | + @Before |
| 21 | + public void init() throws IOException { |
| 22 | + CollectorRegistry registry = new CollectorRegistry(); |
| 23 | + Gauge.build("a", "a help").register(registry); |
| 24 | + Gauge.build("b", "a help").register(registry); |
| 25 | + Gauge.build("c", "a help").register(registry); |
| 26 | + s = new HTTPServer(new InetSocketAddress(0), registry); |
| 27 | + } |
| 28 | + |
| 29 | + @After |
| 30 | + public void cleanup() { |
| 31 | + s.stop(); |
| 32 | + } |
| 33 | + |
| 34 | + String request(String suffix) throws IOException { |
| 35 | + String url = "http://localhost:" + s.server.getAddress().getPort() + "/metrics" + suffix; |
| 36 | + URLConnection connection = new URL(url).openConnection(); |
| 37 | + connection.setDoOutput(true); |
| 38 | + connection.connect(); |
| 39 | + Scanner s = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\\A"); |
| 40 | + return s.hasNext() ? s.next() : ""; |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testSimpleRequest() throws IOException { |
| 45 | + String response = request(""); |
| 46 | + assertThat(response).contains("a 0.0"); |
| 47 | + assertThat(response).contains("b 0.0"); |
| 48 | + assertThat(response).contains("c 0.0"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testBadParams() throws IOException { |
| 53 | + String response = request("?x"); |
| 54 | + assertThat(response).contains("a 0.0"); |
| 55 | + assertThat(response).contains("b 0.0"); |
| 56 | + assertThat(response).contains("c 0.0"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testSingleName() throws IOException { |
| 61 | + String response = request("?name[]=a"); |
| 62 | + assertThat(response).contains("a 0.0"); |
| 63 | + assertThat(response).doesNotContain("b 0.0"); |
| 64 | + assertThat(response).doesNotContain("c 0.0"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testMultiName() throws IOException { |
| 69 | + String response = request("?name[]=a&name[]=b"); |
| 70 | + assertThat(response).contains("a 0.0"); |
| 71 | + assertThat(response).contains("b 0.0"); |
| 72 | + assertThat(response).doesNotContain("c 0.0"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testDecoding() throws IOException { |
| 77 | + String response = request("?n%61me[]=%61"); |
| 78 | + assertThat(response).contains("a 0.0"); |
| 79 | + assertThat(response).doesNotContain("b 0.0"); |
| 80 | + assertThat(response).doesNotContain("c 0.0"); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments