Skip to content

Commit

Permalink
Merge pull request #758 from jaikiran/undertow-1540
Browse files Browse the repository at this point in the history
Fix for UNDERTOW-1540
  • Loading branch information
fl4via committed May 23, 2019
2 parents cc8f7ba + 6b058c6 commit 0ddeb21
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
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");
}
}

0 comments on commit 0ddeb21

Please sign in to comment.