Browse Source

Polish contribution

See gh-23219
pull/23305/head
Sam Brannen 7 years ago
parent
commit
b2b79ae1e6
  1. 4
      spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java
  2. 24
      spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

4
spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

@ -56,6 +56,7 @@ import org.springframework.web.util.WebUtils; @@ -56,6 +56,7 @@ import org.springframework.web.util.WebUtils;
* @author Rod Johnson
* @author Brian Clozel
* @author Vedran Pavic
* @author Sebastien Deleuze
* @since 1.0.2
*/
public class MockHttpServletResponse implements HttpServletResponse {
@ -219,10 +220,11 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -219,10 +220,11 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Get the content of the response body as a {@code String}, using the provided
* {@code fallbackCharset} if no charset has been explicitly defined, else using
* {@code fallbackCharset} if no charset has been explicitly defined and otherwise
* using the configured {@linkplain #getCharacterEncoding character encoding}.
* @return the content as a {@code String}
* @throws UnsupportedEncodingException if the character encoding is not supported
* @since 5.2
* @see #getContentAsString()
*/
public String getContentAsString(Charset fallbackCharset) throws UnsupportedEncodingException {

24
spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

@ -23,6 +23,7 @@ import java.io.OutputStreamWriter; @@ -23,6 +23,7 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@ -55,6 +56,7 @@ import org.springframework.web.util.WebUtils; @@ -55,6 +56,7 @@ import org.springframework.web.util.WebUtils;
* @author Rod Johnson
* @author Brian Clozel
* @author Vedran Pavic
* @author Sebastien Deleuze
* @since 1.0.2
*/
public class MockHttpServletResponse implements HttpServletResponse {
@ -204,11 +206,33 @@ public class MockHttpServletResponse implements HttpServletResponse { @@ -204,11 +206,33 @@ public class MockHttpServletResponse implements HttpServletResponse {
return this.content.toByteArray();
}
/**
* Get the content of the response body as a {@code String}, using the configured
* {@linkplain #getCharacterEncoding character encoding}.
* @return the content as a {@code String}
* @throws UnsupportedEncodingException if the character encoding is not supported
* @see #getContentAsString(Charset)
*/
public String getContentAsString() throws UnsupportedEncodingException {
return (this.characterEncoding != null ?
this.content.toString(this.characterEncoding) : this.content.toString());
}
/**
* Get the content of the response body as a {@code String}, using the provided
* {@code fallbackCharset} if no charset has been explicitly defined and otherwise
* using the configured {@linkplain #getCharacterEncoding character encoding}.
* @return the content as a {@code String}
* @throws UnsupportedEncodingException if the character encoding is not supported
* @since 5.2
* @see #getContentAsString()
*/
public String getContentAsString(Charset fallbackCharset) throws UnsupportedEncodingException {
return isCharset() ?
this.content.toString(this.characterEncoding) :
this.content.toString(fallbackCharset.name());
}
@Override
public void setContentLength(int contentLength) {
this.contentLength = contentLength;

Loading…
Cancel
Save