Introduce getContentAsByteArray()/getContentAsString() in MockHtttpSvltReq

In order to improve debugging and logging within test suites, this
commit introduces getContentAsByteArray() and getContentAsString()
methods in MockHttpServletRequest, analogous to the existing methods in
MockHttpServletResponse.

Issue: SPR-14717
This commit is contained in:
Sam Brannen
2016-10-03 17:33:31 +02:00
parent dbc86ec043
commit 04b8ae921e
3 changed files with 97 additions and 1 deletions

View File

@@ -58,7 +58,7 @@ public class MockHttpServletRequestTests {
@Test
public void content() throws IOException {
public void setContentAndGetInputStream() throws IOException {
byte[] bytes = "body".getBytes(Charset.defaultCharset());
request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength());
@@ -66,11 +66,43 @@ public class MockHttpServletRequestTests {
assertEquals("body", StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset()));
}
@Test
public void setContentAndGetContentAsByteArray() throws IOException {
byte[] bytes = "request body".getBytes();
request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength());
assertNotNull(request.getContentAsByteArray());
assertEquals(bytes, request.getContentAsByteArray());
}
@Test
public void setContentAndGetContentAsStringWithDefaultCharacterEncoding() throws IOException {
String palindrome = "ablE was I ere I saw Elba";
byte[] bytes = palindrome.getBytes();
request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength());
assertNotNull(request.getContentAsString());
assertEquals(palindrome, request.getContentAsString());
}
@Test
public void setContentAndGetContentAsStringWithExplicitCharacterEncoding() throws IOException {
String palindrome = "ablE was I ere I saw Elba";
byte[] bytes = palindrome.getBytes("UTF-16");
request.setCharacterEncoding("UTF-16");
request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength());
assertNotNull(request.getContentAsString());
assertEquals(palindrome, request.getContentAsString());
}
@Test
public void noContent() throws IOException {
assertEquals(-1, request.getContentLength());
assertNotNull(request.getInputStream());
assertEquals(-1, request.getInputStream().read());
assertNull(request.getContentAsByteArray());
assertNull(request.getContentAsString());
}
@Test