use of io.undertow.server.handlers.SecureCookieHandler in project undertow by undertow-io.
the class MarkSecureHandlerTestCase method testMarkSecureHandlerWithSecureCookieHandler.
@Test
public void testMarkSecureHandlerWithSecureCookieHandler() throws IOException, GeneralSecurityException, ServletException {
final PathHandler root = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
ServletInfo s = new ServletInfo("servlet", MessageServlet.class).addInitParam(MessageServlet.MESSAGE, HELLO_WORLD).addMapping("/issecure");
DeploymentInfo builder = new DeploymentInfo().setClassLoader(MarkSecureHandlerTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServlet(s);
builder.addFilter(new FilterInfo("issecure-filter", IsSecureFilter.class));
builder.addFilterUrlMapping("issecure-filter", "/*", DispatcherType.REQUEST);
DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
root.addPrefixPath(builder.getContextPath(), manager.start());
DefaultServer.setRootHandler(new MarkSecureHandler(new SecureCookieHandler(root)));
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/issecure");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
// When MarkSecureHandler is enabled, req.isSecure() should be true
Assert.assertEquals("true", result.getHeaders("issecure")[0].getValue());
// When SecureCookieHandler is enabled with MarkSecureHandler, secure cookie is enabled as this channel is treated as secure
Header header = result.getFirstHeader("set-cookie");
Assert.assertEquals("foo=bar; secure", header.getValue());
final String response = HttpClientUtils.readResponse(result);
Assert.assertEquals(HELLO_WORLD, response);
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations