Fix #464 - Pretty print by default in ResponseHighlighterInterceptor

This commit is contained in:
James Agnew 2016-10-18 14:58:28 -04:00
parent 4c2841436f
commit 3d686e9383
4 changed files with 77 additions and 2 deletions

View File

@ -170,6 +170,7 @@ public class Constants {
public static final String URL_TOKEN_METADATA = "metadata"; public static final String URL_TOKEN_METADATA = "metadata";
public static final String CT_JSON_PATCH = "application/json-patch+json"; public static final String CT_JSON_PATCH = "application/json-patch+json";
public static final String CT_XML_PATCH = "application/xml-patch+xml"; public static final String CT_XML_PATCH = "application/xml-patch+xml";
public static final String PARAM_PRETTY_VALUE_FALSE = "false";
static { static {
CHARSET_UTF8 = Charset.forName(CHARSET_NAME_UTF8); CHARSET_UTF8 = Charset.forName(CHARSET_NAME_UTF8);

View File

@ -295,6 +295,20 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
RestfulServerUtils.configureResponseParser(theRequestDetails, p); RestfulServerUtils.configureResponseParser(theRequestDetails, p);
} }
// This interceptor defaults to pretty printing unless the user
// has specifically requested us not to
boolean prettyPrintResponse = true;
String[] prettyParams = parameters.get(Constants.PARAM_PRETTY);
if (prettyParams != null && prettyParams.length > 0) {
if (Constants.PARAM_PRETTY_VALUE_FALSE.equals(prettyParams[0])) {
prettyPrintResponse = false;
}
}
if (prettyPrintResponse) {
p.setPrettyPrint(prettyPrintResponse);
}
EncodingEnum encoding = p.getEncoding(); EncodingEnum encoding = p.getEncoding();
String encoded = p.encodeResourceToString(resource); String encoded = p.encodeResourceToString(resource);

View File

@ -88,6 +88,59 @@ public class ResponseHighlightingInterceptorTest {
TestUtil.clearAllStaticFieldsForUnitTest(); TestUtil.clearAllStaticFieldsForUnitTest();
} }
/**
* See #464
*/
@Test
public void testPrettyPrintDefaultsToTrue() throws Exception {
ourServlet.setDefaultPrettyPrint(false);
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1");
httpGet.addHeader("Accept", "text/html");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, (stringContainsInOrder("<body>", "<pre>", "\n", "</pre>")));
}
/**
* See #464
*/
@Test
public void testPrettyPrintDefaultsToTrueWithExplicitTrue() throws Exception {
ourServlet.setDefaultPrettyPrint(false);
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_pretty=true");
httpGet.addHeader("Accept", "text/html");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, (stringContainsInOrder("<body>", "<pre>", "\n", "</pre>")));
}
/**
* See #464
*/
@Test
public void testPrettyPrintDefaultsToTrueWithExplicitFalse() throws Exception {
ourServlet.setDefaultPrettyPrint(false);
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/1?_pretty=false");
httpGet.addHeader("Accept", "text/html");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(responseContent, not(stringContainsInOrder("<body>", "<pre>", "\n", "</pre>")));
}
@Test @Test
public void testForceResponseTime() throws Exception { public void testForceResponseTime() throws Exception {
@ -122,7 +175,7 @@ public class ResponseHighlightingInterceptorTest {
public void testGetInvalidResourceNoAcceptHeader() throws Exception { public void testGetInvalidResourceNoAcceptHeader() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Foobar/123"); HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Foobar/123");
CloseableHttpResponse status = ourClient.execute(httpGet); CloseableHttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent()); String responseContent = IOUtils.toString(status.getEntity().getContent(), StandardCharsets.UTF_8);
IOUtils.closeQuietly(status.getEntity().getContent()); IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info("Resp: {}", responseContent); ourLog.info("Resp: {}", responseContent);
@ -391,7 +444,7 @@ public class ResponseHighlightingInterceptorTest {
String output = sw.getBuffer().toString(); String output = sw.getBuffer().toString();
ourLog.info(output); ourLog.info(output);
assertThat(output, containsString("<span class='hlTagName'>Patient</span>")); assertThat(output, containsString("<span class='hlTagName'>Patient</span>"));
assertThat(output, not(stringContainsInOrder("<body>", "<pre>", "\n", "</pre>"))); assertThat(output, stringContainsInOrder("<body>", "<pre>", "\n", "</pre>"));
assertThat(output, containsString("<a href=\"?_format=json\">")); assertThat(output, containsString("<a href=\"?_format=json\">"));
} }

View File

@ -201,6 +201,13 @@
contained resource, the reference value (e.g. "#1") did not contained resource, the reference value (e.g. "#1") did not
get serialized. Thanks to GitHub user @fw060 for reporting! get serialized. Thanks to GitHub user @fw060 for reporting!
</action> </action>
<action type="fix" issue="464">
ResponseHighlighterInterceptor now pretty-prints responses
by default unless the user has explicitly requested
a non-pretty-printed response (ie.
using <![CDATA[<code>?_pretty=false</code>]]>. Thanks to
Allan Brohansen and Jens Villadsen for the suggestion!
</action>
</release> </release>
<release version="2.0" date="2016-08-30"> <release version="2.0" date="2016-08-30">
<action type="fix"> <action type="fix">