Affects: 5.2.7.RELEASE

When calling the reset() method of MockHttpServletResponse, the boolean charset is not reset to false. If the response object had the charset set before being reset, charset=null is appended to the mime type. This can lead to an InvalidMediaTypeException:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is
org.springframework.http.InvalidMediaTypeException: Invalid mime type "application/octet-stream;charset=null": 
unsupported charset 'null'

Comment From: sbrannen

Thanks for raising the issue.

I am able to reproduce it with the following test case, which throws the reported exception for the assertThatCode(() -> MimeType.valueOf(contentTypeHeader2)).doesNotThrowAnyException() assertion.

@Test
void resetResetsCharset() {
    assertThat(response.isCharset()).isFalse();
    response.setCharacterEncoding("UTF-8");
    assertThat(response.isCharset()).isTrue();
    assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
    response.setContentType("text/plain");
    assertThat(response.getContentType()).isEqualTo("text/plain");
    String contentTypeHeader1 = response.getHeader(CONTENT_TYPE);
    assertThatCode(() -> MimeType.valueOf(contentTypeHeader1)).doesNotThrowAnyException();
    assertThat(contentTypeHeader1).isEqualTo("text/plain;charset=UTF-8");

    response.reset();
    // assertThat(response.isCharset()).isFalse();
    response.setContentType("text/plain");
    assertThat(response.getContentType()).isEqualTo("text/plain");
    String contentTypeHeader2 = response.getHeader(CONTENT_TYPE);
    assertThatCode(() -> MimeType.valueOf(contentTypeHeader2)).doesNotThrowAnyException();
    assertThat(contentTypeHeader2).isEqualTo("text/plain");
}

Comment From: sbrannen

This has been fixed in Spring Framework 5.2.x and master.

Feel free to try it out in the upcoming 5.2.9 and 5.3 M2 snapshots.