Skip to content

Commit

Permalink
Introduce getContentAsByteArray()/getContentAsString() in MockHtttpSv…
Browse files Browse the repository at this point in the history
…ltReq

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
  • Loading branch information
sbrannen committed Oct 3, 2016
1 parent dbc86ec commit 04b8ae9
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,42 @@ private void updateContentTypeHeader() {
}
}

/**
* Set the content of the request body as a byte array.
* @see #getContentAsByteArray()
* @see #getContentAsString()
*/
public void setContent(byte[] content) {
this.content = content;
}

/**
* Get the content of the request body as a byte array.
* @since 5.0
* @see #setContent(byte[])
* @see #getContentAsString()
*/
public byte[] getContentAsByteArray() {
return this.content;
}

/**
* Get the content of the request body as a {@code String}, using the configured
* {@linkplain #getCharacterEncoding character encoding} if present.
* @since 5.0
* @see #setContent(byte[])
* @see #getContentAsByteArray()
* @see #setCharacterEncoding(String)
*/
public String getContentAsString() throws UnsupportedEncodingException {
if (this.content == null) {
return null;
}

return (this.characterEncoding != null ?
new String(this.content, this.characterEncoding) : new String(this.content));
}

@Override
public int getContentLength() {
return (this.content != null ? this.content.length : -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,51 @@ 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());
assertNotNull(request.getInputStream());
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,42 @@ private void updateContentTypeHeader() {
}
}

/**
* Set the content of the request body as a byte array.
* @see #getContentAsByteArray()
* @see #getContentAsString()
*/
public void setContent(byte[] content) {
this.content = content;
}

/**
* Get the content of the request body as a byte array.
* @since 5.0
* @see #setContent(byte[])
* @see #getContentAsString()
*/
public byte[] getContentAsByteArray() {
return this.content;
}

/**
* Get the content of the request body as a {@code String}, using the configured
* {@linkplain #getCharacterEncoding character encoding} if present.
* @since 5.0
* @see #setContent(byte[])
* @see #getContentAsByteArray()
* @see #setCharacterEncoding(String)
*/
public String getContentAsString() throws UnsupportedEncodingException {
if (this.content == null) {
return null;
}

return (this.characterEncoding != null ?
new String(this.content, this.characterEncoding) : new String(this.content));
}

@Override
public int getContentLength() {
return (this.content != null ? this.content.length : -1);
Expand Down

0 comments on commit 04b8ae9

Please sign in to comment.