Update contentType property via MockHttpServletResponse::setCharacterEncoding()
Prior to this commit, MockHttpServletResponse's setCharacterEncoding() method did not update the contentType property, which violates the Servlet 2.4 Javadoc for getContentType() and setCharacterEncoding(). This commit addresses this issue; however, some existing tests may have to be updated as a result of this change. For example, note how some of the tests in this commit have been refactored to use MediaType##isCompatibleWith() instead of asserting exact matches for the value returned by MockHttpServletResponse's getContentType() method. Closes gh-25536
This commit is contained in:
@@ -169,14 +169,15 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
public void setCharacterEncoding(String characterEncoding) {
|
||||
this.characterEncoding = characterEncoding;
|
||||
this.charset = true;
|
||||
updateContentTypeHeader();
|
||||
updateContentTypePropertyAndHeader();
|
||||
}
|
||||
|
||||
private void updateContentTypeHeader() {
|
||||
private void updateContentTypePropertyAndHeader() {
|
||||
if (this.contentType != null) {
|
||||
String value = this.contentType;
|
||||
if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) {
|
||||
value = value + ';' + CHARSET_PREFIX + this.characterEncoding;
|
||||
this.contentType = value;
|
||||
}
|
||||
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
|
||||
}
|
||||
@@ -280,7 +281,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
this.charset = true;
|
||||
}
|
||||
}
|
||||
updateContentTypeHeader();
|
||||
updateContentTypePropertyAndHeader();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user