Skip to content

Commit

Permalink
Issue #5539 - Cleanup, adding javadoc, etc.
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Nov 2, 2020
1 parent 86bbe96 commit 0962eac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Expand Up @@ -54,6 +54,19 @@

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Collect and report statistics about requests / responses / connections and more.
* <p>
* You can use normal HTTP content negotiation to ask for the statistics.
* Specify a request <code>Accept</code> header for one of the following formats:
* <ul>
* <li><code>application/json</code></li>
* <li><code>text/xml</code></li>
* <li><code>text/html</code></li>
* <li><code>text/plain</code> - default if no <code>Accept</code> header specified</li>
* </ul>
* </p>
*/
public class StatisticsServlet extends HttpServlet
{
private static final Logger LOG = Log.getLogger(StatisticsServlet.class);
Expand Down
Expand Up @@ -90,7 +90,7 @@ private void addStatisticsHandler()
}

@Test
public void getStats()
public void testGetStats()
throws Exception
{
addStatisticsHandler();
Expand All @@ -103,7 +103,7 @@ public void getStats()
assertEquals(response.getStatus(), 200);

// Look for 200 response that was tracked
response = getResponse("/stats?xml=true");
response = getResponse("/stats");
assertEquals(response.getStatus(), 200);
Stats stats = parseStats(response.getContent());

Expand All @@ -114,7 +114,7 @@ public void getStats()
assertEquals(response.getStatus(), 200);

// Request stats again
response = getResponse("/stats?xml=true");
response = getResponse("/stats");
assertEquals(response.getStatus(), 200);
stats = parseStats(response.getContent());

Expand All @@ -128,7 +128,7 @@ public void getStats()
assertEquals(response.getStatus(), 404);

// Request stats again
response = getResponse("/stats?xml=true");
response = getResponse("/stats");
assertEquals(response.getStatus(), 200);
stats = parseStats(response.getContent());

Expand All @@ -142,7 +142,7 @@ public void getStats()
}

@Test
public void getXmlResponse()
public void testGetXmlResponse()
throws Exception
{
addStatisticsHandler();
Expand All @@ -166,6 +166,7 @@ public void getXmlResponse()

// Parse it, make sure it's well formed.
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setValidating(false);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
try (ByteArrayInputStream input = new ByteArrayInputStream(response.getContentBytes()))
{
Expand All @@ -176,7 +177,7 @@ public void getXmlResponse()
}

@Test
public void getJsonResponse()
public void testGetJsonResponse()
throws Exception
{
addStatisticsHandler();
Expand Down Expand Up @@ -213,7 +214,7 @@ public void getJsonResponse()
}

@Test
public void getTextResponse()
public void testGetTextResponse()
throws Exception
{
addStatisticsHandler();
Expand Down Expand Up @@ -243,7 +244,7 @@ public void getTextResponse()
}

@Test
public void getHtmlResponse()
public void testGetHtmlResponse()
throws Exception
{
addStatisticsHandler();
Expand All @@ -263,7 +264,7 @@ public void getHtmlResponse()

assertThat("Response.contentType", response.get(HttpHeader.CONTENT_TYPE), containsString("text/html"));

System.out.println(response.getContent());
// System.out.println(response.getContent());

// Look for things that indicate it's a well formed HTML output
assertThat(response.getContent(), containsString("<html>"));
Expand All @@ -281,6 +282,7 @@ public HttpTester.Response getResponse(String path)
{
HttpTester.Request request = new HttpTester.Request();
request.setMethod("GET");
request.setHeader("Accept", "text/xml");
request.setURI(path);
request.setVersion(HttpVersion.HTTP_1_1);
request.setHeader("Host", "test");
Expand Down

0 comments on commit 0962eac

Please sign in to comment.