From 04b8ae921ee1a91987ea264e9d6967a5f04e1918 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 3 Oct 2016 17:33:31 +0200 Subject: [PATCH] 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 --- .../mock/web/MockHttpServletRequest.java | 32 +++++++++++++++++ .../mock/web/MockHttpServletRequestTests.java | 34 ++++++++++++++++++- .../mock/web/test/MockHttpServletRequest.java | 32 +++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index 8fa1a191ce..9117ead65d 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -371,10 +371,42 @@ public class MockHttpServletRequest implements HttpServletRequest { } } + /** + * 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); diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java index afa37457be..6ba9394b0a 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java @@ -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 diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java index b97ab429d5..527c1a704c 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java @@ -371,10 +371,42 @@ public class MockHttpServletRequest implements HttpServletRequest { } } + /** + * 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);