Support setCharacterEncoding(null) in MockHttpServletResponse
Closes gh-30341
This commit is contained in:
parent
b408cee29f
commit
54853ee239
|
|
@ -33,6 +33,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
|
@ -201,15 +202,42 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setCharacterEncoding(String characterEncoding) {
|
||||
public void setCharacterEncoding(@Nullable String characterEncoding) {
|
||||
setExplicitCharacterEncoding(characterEncoding);
|
||||
updateContentTypePropertyAndHeader();
|
||||
}
|
||||
|
||||
private void setExplicitCharacterEncoding(String characterEncoding) {
|
||||
Assert.notNull(characterEncoding, "'characterEncoding' must not be null");
|
||||
this.characterEncoding = characterEncoding;
|
||||
this.characterEncodingSet = true;
|
||||
private void setExplicitCharacterEncoding(@Nullable String characterEncoding) {
|
||||
if (characterEncoding == null) {
|
||||
this.characterEncoding = this.defaultCharacterEncoding;
|
||||
this.characterEncodingSet = false;
|
||||
if (this.contentType != null) {
|
||||
try {
|
||||
MediaType mediaType = MediaType.parseMediaType(this.contentType);
|
||||
if (mediaType.getCharset() != null) {
|
||||
Map<String, String> parameters = new LinkedHashMap<>(mediaType.getParameters());
|
||||
parameters.remove("charset");
|
||||
mediaType = new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters);
|
||||
this.contentType = mediaType.toString();
|
||||
}
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
String value = this.contentType;
|
||||
int charsetIndex = value.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
value = value.substring(0, charsetIndex).trim();
|
||||
if (value.endsWith(";")) {
|
||||
value = value.substring(0, value.length() - 1);
|
||||
}
|
||||
this.contentType = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.characterEncoding = characterEncoding;
|
||||
this.characterEncodingSet = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateContentTypePropertyAndHeader() {
|
||||
|
|
|
|||
|
|
@ -195,6 +195,18 @@ class MockHttpServletResponseTests {
|
|||
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
|
||||
}
|
||||
|
||||
@Test
|
||||
void setCharacterEncodingNull() {
|
||||
response.setContentType("test/plain");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
assertThat(response.getContentType()).isEqualTo("test/plain;charset=UTF-8");
|
||||
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo("test/plain;charset=UTF-8");
|
||||
response.setCharacterEncoding(null);
|
||||
assertThat(response.getContentType()).isEqualTo("test/plain");
|
||||
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo("test/plain");
|
||||
assertThat(response.getCharacterEncoding()).isEqualTo(WebUtils.DEFAULT_CHARACTER_ENCODING);
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultCharacterEncoding() {
|
||||
assertThat(response.isCharset()).isFalse();
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
|
@ -201,15 +202,42 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setCharacterEncoding(String characterEncoding) {
|
||||
public void setCharacterEncoding(@Nullable String characterEncoding) {
|
||||
setExplicitCharacterEncoding(characterEncoding);
|
||||
updateContentTypePropertyAndHeader();
|
||||
}
|
||||
|
||||
private void setExplicitCharacterEncoding(String characterEncoding) {
|
||||
Assert.notNull(characterEncoding, "'characterEncoding' must not be null");
|
||||
this.characterEncoding = characterEncoding;
|
||||
this.characterEncodingSet = true;
|
||||
private void setExplicitCharacterEncoding(@Nullable String characterEncoding) {
|
||||
if (characterEncoding == null) {
|
||||
this.characterEncoding = this.defaultCharacterEncoding;
|
||||
this.characterEncodingSet = false;
|
||||
if (this.contentType != null) {
|
||||
try {
|
||||
MediaType mediaType = MediaType.parseMediaType(this.contentType);
|
||||
if (mediaType.getCharset() != null) {
|
||||
Map<String, String> parameters = new LinkedHashMap<>(mediaType.getParameters());
|
||||
parameters.remove("charset");
|
||||
mediaType = new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters);
|
||||
this.contentType = mediaType.toString();
|
||||
}
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
String value = this.contentType;
|
||||
int charsetIndex = value.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
value = value.substring(0, charsetIndex).trim();
|
||||
if (value.endsWith(";")) {
|
||||
value = value.substring(0, value.length() - 1);
|
||||
}
|
||||
this.contentType = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.characterEncoding = characterEncoding;
|
||||
this.characterEncodingSet = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateContentTypePropertyAndHeader() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue