Search in sources :

Example 71 with ServletContainer

use of io.undertow.servlet.api.ServletContainer 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();
    }
}
Also used : DeploymentManager(io.undertow.servlet.api.DeploymentManager) HttpGet(org.apache.http.client.methods.HttpGet) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) MarkSecureHandler(io.undertow.servlet.handlers.MarkSecureHandler) TestHttpClient(io.undertow.testutils.TestHttpClient) ServletInfo(io.undertow.servlet.api.ServletInfo) Header(org.apache.http.Header) ServletContainer(io.undertow.servlet.api.ServletContainer) SecureCookieHandler(io.undertow.server.handlers.SecureCookieHandler) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) FilterInfo(io.undertow.servlet.api.FilterInfo) Test(org.junit.Test)

Example 72 with ServletContainer

use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.

the class MarkSecureHandlerTestCase method testMarkSecureHandler.

@Test
public void testMarkSecureHandler() 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(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 not enabled, secure cookie is not automatically enabled.
        Header header = result.getFirstHeader("set-cookie");
        Assert.assertEquals("foo=bar", header.getValue());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals(HELLO_WORLD, response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : DeploymentManager(io.undertow.servlet.api.DeploymentManager) HttpGet(org.apache.http.client.methods.HttpGet) PathHandler(io.undertow.server.handlers.PathHandler) HttpResponse(org.apache.http.HttpResponse) MarkSecureHandler(io.undertow.servlet.handlers.MarkSecureHandler) TestHttpClient(io.undertow.testutils.TestHttpClient) ServletInfo(io.undertow.servlet.api.ServletInfo) Header(org.apache.http.Header) ServletContainer(io.undertow.servlet.api.ServletContainer) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) FilterInfo(io.undertow.servlet.api.FilterInfo) Test(org.junit.Test)

Example 73 with ServletContainer

use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.

the class ServletSessionListenerOrderingTestCase method setup.

@BeforeClass
public static void setup() throws ServletException {
    final PathHandler path = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(SimpleServletTestCase.class.getClassLoader()).setContextPath("/listener").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("listener.war").addListener(new ListenerInfo(FirstListener.class)).addListener(new ListenerInfo(SecondListener.class)).addServlet(new ServletInfo("message", EmptyServlet.class).addMapping("/*"));
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    path.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(path);
}
Also used : ServletInfo(io.undertow.servlet.api.ServletInfo) ListenerInfo(io.undertow.servlet.api.ListenerInfo) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ServletContainer(io.undertow.servlet.api.ServletContainer) PathHandler(io.undertow.server.handlers.PathHandler) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) SimpleServletTestCase(io.undertow.servlet.test.SimpleServletTestCase) BeforeClass(org.junit.BeforeClass)

Example 74 with ServletContainer

use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.

the class AsyncListenerOnErrorTest method setup.

@BeforeClass
public static void setup() throws ServletException {
    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    ServletInfo f = new ServletInfo("faultyServlet", FaultyServlet.class).addMapping("/faulty");
    ServletInfo a1 = new ServletInfo("asyncServlet1", AsyncServlet1.class).setAsyncSupported(true).addMapping("/async1");
    ServletInfo a2 = new ServletInfo("asyncServlet2", AsyncServlet2.class).setAsyncSupported(true).addMapping("/async2");
    ServletInfo a3 = new ServletInfo("asyncServlet3", AsyncServlet3.class).setAsyncSupported(true).addMapping("/async3");
    ServletInfo a4 = new ServletInfo("asyncServlet4", AsyncServlet4.class).setAsyncSupported(true).addMapping("/async4");
    ServletInfo a5 = new ServletInfo("asyncServlet5", AsyncServlet5.class).setAsyncSupported(true).addMapping("/async5");
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(AsyncListenerOnErrorTest.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServlets(f, a1, a2, a3, a4, a5);
    builder.setExceptionHandler(LoggingExceptionHandler.builder().add(IllegalStateException.class, "io.undertow", Logger.Level.DEBUG).build());
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(root);
}
Also used : ServletInfo(io.undertow.servlet.api.ServletInfo) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ServletContainer(io.undertow.servlet.api.ServletContainer) PathHandler(io.undertow.server.handlers.PathHandler) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) BeforeClass(org.junit.BeforeClass)

Example 75 with ServletContainer

use of io.undertow.servlet.api.ServletContainer in project undertow by undertow-io.

the class NestedListenerInvocationTestCase method setup.

@BeforeClass
public static void setup() throws ServletException {
    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    ServletInfo a = new ServletInfo("asyncServlet", AsyncServlet.class).setAsyncSupported(true).addMapping("/async");
    DeploymentInfo builder = new DeploymentInfo().setClassLoader(NestedListenerInvocationTestCase.class.getClassLoader()).setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE).setDeploymentName("servletContext.war").addServlets(a).addListener(new ListenerInfo(SimpleRequestListener.class));
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(root);
}
Also used : ServletInfo(io.undertow.servlet.api.ServletInfo) ListenerInfo(io.undertow.servlet.api.ListenerInfo) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ServletContainer(io.undertow.servlet.api.ServletContainer) PathHandler(io.undertow.server.handlers.PathHandler) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) BeforeClass(org.junit.BeforeClass)

Aggregations

ServletContainer (io.undertow.servlet.api.ServletContainer)92 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)88 DeploymentManager (io.undertow.servlet.api.DeploymentManager)78 PathHandler (io.undertow.server.handlers.PathHandler)76 BeforeClass (org.junit.BeforeClass)69 ServletInfo (io.undertow.servlet.api.ServletInfo)65 TestResourceLoader (io.undertow.servlet.test.util.TestResourceLoader)20 SimpleServletTestCase (io.undertow.servlet.test.SimpleServletTestCase)19 ServletException (javax.servlet.ServletException)19 FilterInfo (io.undertow.servlet.api.FilterInfo)18 LoginConfig (io.undertow.servlet.api.LoginConfig)18 ServletIdentityManager (io.undertow.servlet.test.security.constraint.ServletIdentityManager)16 Test (org.junit.Test)15 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)14 SecurityConstraint (io.undertow.servlet.api.SecurityConstraint)12 WebResourceCollection (io.undertow.servlet.api.WebResourceCollection)12 ListenerInfo (io.undertow.servlet.api.ListenerInfo)10 TestHttpClient (io.undertow.testutils.TestHttpClient)9 ServletSecurityInfo (io.undertow.servlet.api.ServletSecurityInfo)8 AuthMethodConfig (io.undertow.servlet.api.AuthMethodConfig)7