Search in sources :

Example 21 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestAsyncContextImpl method testErrorHandling.

@Test
public void testErrorHandling() throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ErrorServlet error = new ErrorServlet();
    Tomcat.addServlet(ctx, "error", error);
    ctx.addServletMappingDecoded("/error", "error");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/error");
    int rc = getUrl(url.toString(), new ByteChunk(), null);
    assertEquals(500, rc);
    // Without this test may complete before access log has a chance to log
    // the request
    Thread.sleep(REQUEST_TIME);
    // Check the access log
    alv.validateAccessLog(1, 500, 0, REQUEST_TIME);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 22 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestNamingContextListener method testBug54096.

@Test
public void testBug54096() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();
    ContextEnvironment environmentA = new ContextEnvironment();
    environmentA.setType(Bug54096EnvA.class.getName());
    environmentA.setName(BUG54096_NameA);
    environmentA.setValue(BUG54096_ValueA);
    ctx.getNamingResources().addEnvironment(environmentA);
    ContextEnvironment environmentB = new ContextEnvironment();
    environmentB.setType(Bug54096EnvB.class.getName());
    environmentB.setName(BUG54096_NameB);
    environmentB.setValue(BUG54096_ValueB);
    ctx.getNamingResources().addEnvironment(environmentB);
    ctx.addApplicationListener(Bug54096Listener.class.getName());
    tomcat.start();
    assertEquals(LifecycleState.STARTED, ctx.getState());
}
Also used : InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) Tomcat(org.apache.catalina.startup.Tomcat) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 23 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestSSOnonLoginAndDigestAuthenticator method setUpDigest.

private void setUpDigest(Tomcat tomcat) throws Exception {
    // Must have a real docBase for webapps - just use temp
    Context ctxt = tomcat.addContext(CONTEXT_PATH_DIGEST, System.getProperty("java.io.tmpdir"));
    ctxt.setSessionTimeout(SHORT_TIMEOUT_SECS);
    // Add protected servlet
    Tomcat.addServlet(ctxt, "TesterServlet3", new TesterServlet());
    ctxt.addServletMappingDecoded(URI_PROTECTED, "TesterServlet3");
    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded(URI_PROTECTED);
    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctxt.addConstraint(sc);
    // Configure the appropriate authenticator
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("DIGEST");
    ctxt.setLoginConfig(lc);
    ctxt.getPipeline().addValve(new DigestAuthenticator());
}
Also used : Context(org.apache.catalina.Context) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) TesterServlet(org.apache.catalina.startup.TesterServlet) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection)

Example 24 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestConnector method doTestTrace.

private void doTestTrace(Servlet servlet, boolean allowTrace) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp");
    Context root = tomcat.addContext("", appDir.getAbsolutePath());
    Tomcat.addServlet(root, "default", servlet);
    root.addServletMappingDecoded("/", "default");
    Connector connector = tomcat.getConnector();
    connector.setAllowTrace(allowTrace);
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    Map<String, List<String>> respHeaders = new HashMap<>();
    int rc = methodUrl("http://localhost:" + getPort() + "/index.html", bc, 30000, null, respHeaders, "OPTIONS");
    assertEquals(200, rc);
    boolean foundTrace = false;
    for (String header : respHeaders.get("Allow")) {
        if (header.contains("TRACE")) {
            foundTrace = true;
            break;
        }
    }
    if (allowTrace) {
        Assert.assertTrue(foundTrace);
    } else {
        Assert.assertFalse(foundTrace);
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) List(java.util.List) File(java.io.File)

Example 25 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestCoyoteAdapter method testPathParamsRedirect.

@Test
public void testPathParamsRedirect() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // Must have a real docBase. Don't use java.io.tmpdir as it may not be
    // writable.
    File docBase = new File(getTemporaryDirectory(), "testCoyoteAdapter");
    addDeleteOnTearDown(docBase);
    if (!docBase.mkdirs() && !docBase.isDirectory()) {
        Assert.fail("Failed to create: [" + docBase.toString() + "]");
    }
    // Create the folder that will trigger the redirect
    File foo = new File(docBase, "foo");
    addDeleteOnTearDown(foo);
    if (!foo.mkdirs() && !foo.isDirectory()) {
        Assert.fail("Unable to create foo directory in docBase");
    }
    Context ctx = tomcat.addContext("", docBase.getAbsolutePath());
    Tomcat.addServlet(ctx, "servlet", new PathParamServlet());
    ctx.addServletMappingDecoded("/", "servlet");
    tomcat.start();
    testPath("/", "none");
    testPath("/;jsessionid=1234", "1234");
    testPath("/foo;jsessionid=1234", "1234");
    testPath("/foo;jsessionid=1234;dummy", "1234");
    testPath("/foo;jsessionid=1234;dummy=5678", "1234");
    testPath("/foo;jsessionid=1234;=5678", "1234");
    testPath("/foo;jsessionid=1234/bar", "1234");
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Tomcat(org.apache.catalina.startup.Tomcat) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

Context (org.apache.catalina.Context)376 Tomcat (org.apache.catalina.startup.Tomcat)212 Test (org.junit.Test)180 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)127 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)96 File (java.io.File)77 ServletContext (javax.servlet.ServletContext)74 AsyncContext (javax.servlet.AsyncContext)73 StandardContext (org.apache.catalina.core.StandardContext)65 Wrapper (org.apache.catalina.Wrapper)53 IOException (java.io.IOException)40 TesterContext (org.apache.tomcat.unittest.TesterContext)39 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 URI (java.net.URI)33 WebSocketContainer (javax.websocket.WebSocketContainer)32 Session (javax.websocket.Session)31 Host (org.apache.catalina.Host)30 Container (org.apache.catalina.Container)26 ArrayList (java.util.ArrayList)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24