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:
Sam Brannen
2020-08-10 16:05:18 +02:00
parent c5bb5d6c03
commit 5de549d7d4
6 changed files with 44 additions and 60 deletions

View File

@@ -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();
}
}

View File

@@ -144,7 +144,7 @@ class MockHttpServletResponseTests {
void setContentTypeThenCharacterEncoding() {
response.setContentType("test/plain");
response.setCharacterEncoding("UTF-8");
assertThat(response.getContentType()).isEqualTo("test/plain");
assertThat(response.getContentType()).isEqualTo("test/plain;charset=UTF-8");
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo("test/plain;charset=UTF-8");
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
}
@@ -153,7 +153,7 @@ class MockHttpServletResponseTests {
void setCharacterEncodingThenContentType() {
response.setCharacterEncoding("UTF-8");
response.setContentType("test/plain");
assertThat(response.getContentType()).isEqualTo("test/plain");
assertThat(response.getContentType()).isEqualTo("test/plain;charset=UTF-8");
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo("test/plain;charset=UTF-8");
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
}
@@ -488,7 +488,7 @@ class MockHttpServletResponseTests {
assertThat(response.isCharset()).isTrue();
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
response.setContentType("text/plain");
assertThat(response.getContentType()).isEqualTo("text/plain");
assertThat(response.getContentType()).isEqualTo("text/plain;charset=UTF-8");
String contentTypeHeader = response.getHeader(CONTENT_TYPE);
assertThat(contentTypeHeader).isEqualTo("text/plain;charset=UTF-8");

View File

@@ -73,7 +73,7 @@ class ViewResolutionTests {
standaloneSetup(new PersonController()).setSingleView(new MappingJackson2JsonView()).build()
.perform(get("/person/Corea"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.person.name").value("Corea"));
}
@@ -119,7 +119,7 @@ class ViewResolutionTests {
mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.person.name").value("Corea"));
mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
@@ -150,4 +150,3 @@ class ViewResolutionTests {
}
}