Skip to content

Commit

Permalink
Support setCharacterEncoding(null) in MockHttpServletResponse
Browse files Browse the repository at this point in the history
Closes gh-30341
  • Loading branch information
poutsma committed Apr 25, 2023
1 parent b408cee commit 54853ee
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -201,15 +202,42 @@ public boolean isCharset() {
}

@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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ void setCharacterEncodingThenContentType() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -201,15 +202,42 @@ public boolean isCharset() {
}

@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() {
Expand Down

0 comments on commit 54853ee

Please sign in to comment.