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

Fix for UNDERTOW-1540 #758

Merged
merged 1 commit into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ public String getRequestCharacterEncoding() {
public void setRequestCharacterEncoding(String encoding) {
ensureNotInitialized();
ensureNotProgramaticListener();
deploymentInfo.setDefaultRequestEncoding(getContextPath());
deploymentInfo.setDefaultRequestEncoding(encoding);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -76,6 +77,35 @@ private void testDefaultEncoding(String defaultCharacterEncoding,
}
}

private void testServletContextCharacterEncoding(final String requestCharacterEncoding, final String responseCharacterEncoding)
throws IOException, ServletException {
DeploymentUtils.setupServlet(new ServletExtension() {
@Override
public void handleDeployment(final DeploymentInfo deploymentInfo, final ServletContext servletContext) {
servletContext.setRequestCharacterEncoding(requestCharacterEncoding);
servletContext.setResponseCharacterEncoding(responseCharacterEncoding);
}
},
Servlets.servlet("servlet", DefaultCharacterEncodingServlet.class).addMapping("/"));
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
final String expectedRequestCharEncoding = requestCharacterEncoding == null ? "null" : requestCharacterEncoding;
Assert.assertEquals("Unexpected request character encoding",
expectedRequestCharEncoding, readParameter(response, "requestCharacterEncoding"));
// spec mandates "ISO-8859-1" as the default (see javadoc of ServletResponse#getCharacterEncoding())
final String expectedResponseCharEncoding = responseCharacterEncoding == null ? "ISO-8859-1" : responseCharacterEncoding;
Assert.assertEquals("Unexpected response character encoding",
expectedResponseCharEncoding, readParameter(response, "responseCharacterEncoding"));
} finally {
client.getConnectionManager().shutdown();
}

}

private String readParameter(String response, String parameter) {
Pattern pattern = Pattern.compile(parameter + "=(.*?);");
Matcher matcher = pattern.matcher(response);
Expand All @@ -100,4 +130,19 @@ public void testDefaultEncodingSetEqualDefault() throws IOException, ServletExce
public void testDefaultEncodingSetNotEqualDefault() throws IOException, ServletException {
testDefaultEncoding("UTF-8", "UTF-8", "UTF-8");
}

/**
* Tests that the character encoding set on the servlet context using {@link ServletContext#setRequestCharacterEncoding(String)}
* and {@link ServletContext#setResponseCharacterEncoding(String)} is honoured at runtime during request/response processing
*
* @throws Exception
*/
@Test
public void testServletContextCharEncoding() throws Exception {
testServletContextCharacterEncoding(null, null);
testServletContextCharacterEncoding("UTF-8", null);
testServletContextCharacterEncoding("UTF-8", "UTF-8");
testServletContextCharacterEncoding(null, "UTF-8");
testServletContextCharacterEncoding(StandardCharsets.UTF_16BE.name(), "UTF-8");
}
}