Search in sources :

Example 76 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestStandardWrapper method testSecurityAnnotationsNoWebXmlConstraints.

@Test
public void testSecurityAnnotationsNoWebXmlConstraints() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-servletsecurity");
    tomcat.addWebapp(null, "", appDir.getAbsolutePath());
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc;
    rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);
    assertTrue(bc.getLength() > 0);
    assertEquals(403, rc);
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) HttpConstraint(javax.servlet.annotation.HttpConstraint) HttpMethodConstraint(javax.servlet.annotation.HttpMethodConstraint) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 77 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestStandardWrapper method testSecurityAnnotationsMetaDataPriority.

@Test
public void testSecurityAnnotationsMetaDataPriority() throws Exception {
    getTomcatInstanceTestWebapp(false, true);
    ByteChunk bc = new ByteChunk();
    int rc;
    rc = getUrl("http://localhost:" + getPort() + "/test/testStandardWrapper/securityAnnotationsMetaDataPriority", bc, null, null);
    assertEquals("OK", bc.toString());
    assertEquals(200, rc);
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HttpConstraint(javax.servlet.annotation.HttpConstraint) HttpMethodConstraint(javax.servlet.annotation.HttpMethodConstraint) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 78 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestStandardContext method testWebappLoaderStartFail.

@Test
public void testWebappLoaderStartFail() throws Exception {
    // Test that if WebappLoader start() fails and if the cause of
    // the failure is gone, the context can be started without
    // a need to redeploy it.
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    tomcat.start();
    // To not start Context automatically, as we have to configure it first
    ((ContainerBase) tomcat.getHost()).setStartChildren(false);
    FailingWebappLoader loader = new FailingWebappLoader();
    File root = new File("test/webapp");
    Context context = tomcat.addWebapp("", root.getAbsolutePath());
    context.setLoader(loader);
    try {
        context.start();
        fail();
    } catch (LifecycleException ex) {
    // As expected
    }
    assertEquals(LifecycleState.FAILED, context.getState());
    // The second attempt
    loader.setFail(false);
    context.start();
    assertEquals(LifecycleState.STARTED, context.getState());
    // Using a test from testBug49922() to check that the webapp is running
    ByteChunk result = getUrl("http://localhost:" + getPort() + "/bug49922/target");
    assertEquals("Target", result.toString());
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Tomcat(org.apache.catalina.startup.Tomcat) LifecycleException(org.apache.catalina.LifecycleException) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 79 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestStandardContext method testBug49922.

@Test
public void testBug49922() throws Exception {
    // Test that filter mapping works. Test that the same filter is
    // called only once, even if is selected by several mapping
    // url-patterns or by both a url-pattern and a servlet-name.
    getTomcatInstanceTestWebapp(false, true);
    ByteChunk result = new ByteChunk();
    // Check filter and servlet aren't called
    int rc = getUrl("http://localhost:" + getPort() + "/test/bug49922/foo", result, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    assertTrue(result.getLength() > 0);
    // Check extension mapping works
    result = getUrl("http://localhost:" + getPort() + "/test/foo.do");
    assertEquals("FilterServlet", result.toString());
    // Check path mapping works
    result = getUrl("http://localhost:" + getPort() + "/test/bug49922/servlet");
    assertEquals("FilterServlet", result.toString());
    // Check servlet name mapping works
    result = getUrl("http://localhost:" + getPort() + "/test/foo.od");
    assertEquals("FilterServlet", result.toString());
    // Check filter is only called once
    result = getUrl("http://localhost:" + getPort() + "/test/bug49922/servlet/foo.do");
    assertEquals("FilterServlet", result.toString());
    result = getUrl("http://localhost:" + getPort() + "/test/bug49922/servlet/foo.od");
    assertEquals("FilterServlet", result.toString());
    // Check dispatcher mapping
    result = getUrl("http://localhost:" + getPort() + "/test/bug49922/target");
    assertEquals("Target", result.toString());
    result = getUrl("http://localhost:" + getPort() + "/test/bug49922/forward");
    assertEquals("FilterTarget", result.toString());
    result = getUrl("http://localhost:" + getPort() + "/test/bug49922/include");
    assertEquals("IncludeFilterTarget", result.toString());
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 80 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestStandardContext method testWebappListenerConfigureFail.

@Test
public void testWebappListenerConfigureFail() throws Exception {
    // Test that if LifecycleListener on webapp fails during
    // configure_start event and if the cause of the failure is gone,
    // the context can be started without a need to redeploy it.
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    tomcat.start();
    // To not start Context automatically, as we have to configure it first
    ((ContainerBase) tomcat.getHost()).setStartChildren(false);
    FailingLifecycleListener listener = new FailingLifecycleListener();
    File root = new File("test/webapp");
    Context context = tomcat.addWebapp("", root.getAbsolutePath());
    context.addLifecycleListener(listener);
    try {
        context.start();
        fail();
    } catch (LifecycleException ex) {
    // As expected
    }
    assertEquals(LifecycleState.FAILED, context.getState());
    // The second attempt
    listener.setFail(false);
    context.start();
    assertEquals(LifecycleState.STARTED, context.getState());
    // Using a test from testBug49922() to check that the webapp is running
    ByteChunk result = getUrl("http://localhost:" + getPort() + "/bug49922/target");
    assertEquals("Target", result.toString());
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Tomcat(org.apache.catalina.startup.Tomcat) LifecycleException(org.apache.catalina.LifecycleException) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

ByteChunk (org.apache.tomcat.util.buf.ByteChunk)274 Test (org.junit.Test)201 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)180 Tomcat (org.apache.catalina.startup.Tomcat)129 Context (org.apache.catalina.Context)98 File (java.io.File)49 List (java.util.List)48 AsyncContext (javax.servlet.AsyncContext)40 HashMap (java.util.HashMap)35 Wrapper (org.apache.catalina.Wrapper)22 StandardContext (org.apache.catalina.core.StandardContext)21 ArrayList (java.util.ArrayList)20 TesterContext (org.apache.tomcat.unittest.TesterContext)18 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)16 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)13 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)13 ServletContext (javax.servlet.ServletContext)10 WsContextListener (org.apache.tomcat.websocket.server.WsContextListener)10 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)9 InitialContext (javax.naming.InitialContext)8