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

Only print printable content for request and response body in Spring MVC Test [SPR-14776] #19342

Closed
2 tasks done
spring-projects-issues opened this issue Oct 3, 2016 · 3 comments
Closed
2 tasks done

Comments

@spring-projects-issues
Copy link
Task lists! Give feedback
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 3, 2016

Sam Brannen opened SPR-14776 and commented

Overview

This issue picks up where #19282 left.

Deliverables

Using a mechanism similar to MessageHeaderAccessor.isReadableContentType(), update the PrintingResultHandler so that we ...

  1. Only print readable content for a request body in Spring MVC Test
  2. Only print readable content for a response body in Spring MVC Test

Referenced from: commits e65a1a4, 8cf1933

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 11, 2016

Sam Brannen commented

This has been completed as described in GitHub commit e65a1a4.

Print only printable request/response body in MVC Test

Prior to this commit, PrintingResultHandler always printed the request
or response body regardless of its content type. For binary content,
however, the output was unreadable and therefore useless.

This commit addresses this issue by only printing the request or
response body if it is "printable" (i.e., if its content type is known
to be text-based such as plain text, HTML, XHTML, XML, JSON, etc.). If
the content type is unknown (e.g., unspecified for the HTTP request in
the test), it is assumed that the body is printable.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 13, 2016

Sam Brannen commented

Reopened to address this in a different way.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 13, 2016

Sam Brannen commented

This work has been further revised as described in GitHub commit 8cf1933:

Only print request/response body if char enc is present in MVC Test

Commit e65a1a4 introduced support in PrintingResultHandler for only
printing the request or response body in the Spring MVC Test framework
if the content type is known to be text-based (e.g., plain text, HTML,
XHTML, XML, JSON, etc.). For unknown content types the body is assumed
to be text-based and is therefore always printed. The latter behavior,
however, is undesirable since the content may in fact not be text-based.

This commit addresses this issue by making the printing of the request
or response body an opt-in feature. Specifically, if a character
encoding has been set, the request or response body will be printed by
the PrintingResultHandler. Note, however, that the character encoding
is set to ISO-8859-1 in MockHttpServletResponse by default.

In addition, MockHttpServletRequest's getContentAsString() method now
throws an IllegalStateException if the character encoding has not been
set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants