Search in sources :

Example 11 with Tomcat

use of org.apache.catalina.startup.Tomcat 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 12 with Tomcat

use of org.apache.catalina.startup.Tomcat in project tomcat by apache.

the class TestSSOnonLoginAndDigestAuthenticator method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // create a tomcat server using the default in-memory Realm
    Tomcat tomcat = getTomcatInstance();
    // associate the SingeSignOn Valve before the Contexts
    SingleSignOn sso = new SingleSignOn();
    tomcat.getHost().getPipeline().addValve(sso);
    // add the test user and role to the Realm
    tomcat.addUser(USER, PWD);
    tomcat.addRole(USER, ROLE);
    // setup both NonLogin, Login and digest webapps
    setUpNonLogin(tomcat);
    setUpDigest(tomcat);
    tomcat.start();
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat)

Example 13 with Tomcat

use of org.apache.catalina.startup.Tomcat 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 14 with Tomcat

use of org.apache.catalina.startup.Tomcat 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)

Example 15 with Tomcat

use of org.apache.catalina.startup.Tomcat in project tomcat by apache.

the class TestCoyoteAdapter method testBug54928.

@Test
public void testBug54928() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncServlet servlet = new AsyncServlet();
    Wrapper w = Tomcat.addServlet(ctx, "async", servlet);
    w.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/async", "async");
    tomcat.start();
    SimpleHttpClient client = new SimpleHttpClient() {

        @Override
        public boolean isResponseBodyOK() {
            return true;
        }
    };
    String request = "GET /async HTTP/1.1" + SimpleHttpClient.CRLF + "Host: a" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
    client.setPort(getPort());
    client.setRequest(new String[] { request });
    client.connect();
    client.sendRequest();
    for (int i = 0; i < 10; i++) {
        String line = client.readLine();
        if (line != null && line.length() > 20) {
            log.info(line.subSequence(0, 20) + "...");
        }
    }
    client.disconnect();
    // Wait for server thread to stop
    Thread t = servlet.getThread();
    long startTime = System.nanoTime();
    t.join(5000);
    long endTime = System.nanoTime();
    log.info("Waited for servlet thread to stop for " + (endTime - startTime) / 1000000 + " ms");
    Assert.assertTrue(servlet.isCompleted());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) SimpleHttpClient(org.apache.catalina.startup.SimpleHttpClient) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

Tomcat (org.apache.catalina.startup.Tomcat)316 Test (org.junit.Test)222 Context (org.apache.catalina.Context)215 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)186 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)129 File (java.io.File)91 AsyncContext (javax.servlet.AsyncContext)61 Wrapper (org.apache.catalina.Wrapper)46 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 WebSocketContainer (javax.websocket.WebSocketContainer)32 TesterContext (org.apache.tomcat.unittest.TesterContext)32 URI (java.net.URI)31 Session (javax.websocket.Session)31 List (java.util.List)29 ServletContext (javax.servlet.ServletContext)29 StandardContext (org.apache.catalina.core.StandardContext)27 HashMap (java.util.HashMap)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)24 ArrayList (java.util.ArrayList)19