Search in sources :

Example 71 with ByteChunk

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

the class TestStandardHostValve method doTestErrorPageHandling.

private void doTestErrorPageHandling(int error, String report) throws Exception {
    // Request a page that triggers an error
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/error?errorCode=" + error, bc, null);
    Assert.assertEquals(error, rc);
    Assert.assertEquals(report, bc.toString());
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 72 with ByteChunk

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

the class TestStandardHostValve method testSRLAfterError.

@Test
public void testSRLAfterError() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add the error page
    Tomcat.addServlet(ctx, "error", new ErrorServlet());
    ctx.addServletMappingDecoded("/error", "error");
    final List<String> result = new ArrayList<>();
    // Add the request listener
    ServletRequestListener servletRequestListener = new ServletRequestListener() {

        @Override
        public void requestDestroyed(ServletRequestEvent sre) {
            result.add("Visit requestDestroyed");
        }

        @Override
        public void requestInitialized(ServletRequestEvent sre) {
            result.add("Visit requestInitialized");
        }
    };
    ((StandardContext) ctx).addApplicationEventListener(servletRequestListener);
    tomcat.start();
    // Request a page that triggers an error
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/error?errorCode=400", bc, null);
    Assert.assertEquals(400, rc);
    Assert.assertTrue(result.contains("Visit requestInitialized"));
    Assert.assertTrue(result.contains("Visit requestDestroyed"));
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ServletRequestListener(javax.servlet.ServletRequestListener) ArrayList(java.util.ArrayList) ServletRequestEvent(javax.servlet.ServletRequestEvent) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 73 with ByteChunk

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

the class TestStandardWrapper method doTest.

private void doTest(String servletClassName, boolean usePost, boolean useRole, boolean expect200, boolean denyUncovered) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.setDenyUncoveredHttpMethods(denyUncovered);
    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servletClassName);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/", "servlet");
    if (useRole) {
        TesterMapRealm realm = new TesterMapRealm();
        realm.addUser("testUser", "testPwd");
        realm.addUserRole("testUser", "testRole");
        ctx.setRealm(realm);
        ctx.setLoginConfig(new LoginConfig("BASIC", null, null, null));
        ctx.getPipeline().addValve(new BasicAuthenticator());
    }
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    Map<String, List<String>> reqHeaders = null;
    if (useRole) {
        reqHeaders = new HashMap<>();
        List<String> authHeaders = new ArrayList<>();
        // testUser, testPwd
        authHeaders.add("Basic dGVzdFVzZXI6dGVzdFB3ZA==");
        reqHeaders.put("Authorization", authHeaders);
    }
    int rc;
    if (usePost) {
        rc = postUrl(null, "http://localhost:" + getPort() + "/", bc, reqHeaders, null);
    } else {
        rc = getUrl("http://localhost:" + getPort() + "/", bc, reqHeaders, null);
    }
    if (expect200) {
        assertEquals("OK", bc.toString());
        assertEquals(200, rc);
    } else {
        assertTrue(bc.getLength() > 0);
        assertEquals(403, rc);
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ArrayList(java.util.ArrayList) HttpConstraint(javax.servlet.annotation.HttpConstraint) HttpMethodConstraint(javax.servlet.annotation.HttpMethodConstraint) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) BasicAuthenticator(org.apache.catalina.authenticator.BasicAuthenticator) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) ArrayList(java.util.ArrayList) List(java.util.List)

Example 74 with ByteChunk

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

the class TestStandardWrapper method testSecurityAnnotationsWebXmlPriority.

@Test
public void testSecurityAnnotationsWebXmlPriority() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-fragments");
    Context ctx = tomcat.addWebapp(null, "", appDir.getAbsolutePath());
    skipTldsForResourceJars(ctx);
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc;
    rc = getUrl("http://localhost:" + getPort() + "/testStandardWrapper/securityAnnotationsWebXmlPriority", bc, null, null);
    assertTrue(bc.getLength() > 0);
    assertEquals(403, rc);
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) 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 75 with ByteChunk

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

the class TestStandardWrapper method doTestSecurityAnnotationsAddServlet.

private void doTestSecurityAnnotationsAddServlet(boolean useCreateServlet) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Servlet s = new DenyAllServlet();
    ServletContainerInitializer sci = new SCI(s, useCreateServlet);
    ctx.addServletContainerInitializer(sci, null);
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc;
    rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);
    if (useCreateServlet) {
        assertTrue(bc.getLength() > 0);
        assertEquals(403, rc);
    } else {
        assertEquals("OK", bc.toString());
        assertEquals(200, rc);
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HttpServlet(javax.servlet.http.HttpServlet) Servlet(javax.servlet.Servlet) HttpConstraint(javax.servlet.annotation.HttpConstraint) HttpMethodConstraint(javax.servlet.annotation.HttpMethodConstraint)

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