Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockHttpServletResponse reset() does not reset charset field #25501

Closed
Nkyn opened this issue Jul 31, 2020 · 3 comments
Closed

MockHttpServletResponse reset() does not reset charset field #25501

Nkyn opened this issue Jul 31, 2020 · 3 comments
Assignees
Labels
in: test Issues in the test module status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@Nkyn
Copy link

Nkyn commented Jul 31, 2020

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'
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 31, 2020
@sbrannen sbrannen added the in: test Issues in the test module label Aug 1, 2020
@sbrannen sbrannen changed the title MockHttpServletResponse reset does not clear the field boolean charset MockHttpServletResponse reset() does not reset charset field Aug 1, 2020
@sbrannen
Copy link
Member

sbrannen commented Aug 1, 2020

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");
}

@sbrannen sbrannen added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 1, 2020
@sbrannen sbrannen self-assigned this Aug 1, 2020
@sbrannen sbrannen added this to the 5.2.9 milestone Aug 1, 2020
@sbrannen
Copy link
Member

sbrannen commented Aug 1, 2020

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.

@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.1.x labels Aug 3, 2020
jhoeller added a commit that referenced this issue Aug 7, 2020

Verified

This commit was signed with the committer’s verified signature.
fredbi fredbi
Closes gh-25501
jhoeller added a commit that referenced this issue Aug 7, 2020
jhoeller added a commit that referenced this issue Aug 7, 2020
FelixFly pushed a commit to FelixFly/spring-framework that referenced this issue Aug 16, 2020
Prior to this commit, calling reset() on MockHttpServletResponse did not
reset the `charset` field to `false` which could result in the
"Content-Type" header containing `;charset=null` which in turn would
result in errors when parsing the "Content-Type" header.

This commit resets the charset field to `false` in
MockHttpServletResponse's reset() method to avoid such errors.

Closes spring-projectsgh-25501
zx20110729 pushed a commit to zx20110729/spring-framework that referenced this issue Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

5 participants
@sbrannen @jhoeller @spring-projects-issues @Nkyn and others