Search in sources :

Example 1 with Tomcat

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

the class TestRequest method doBug56501.

private void doBug56501(String deployPath, String requestPath, String expected) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext(deployPath, null);
    Tomcat.addServlet(ctx, "servlet", new Bug56501Servlet());
    ctx.addServletMappingDecoded("/*", "servlet");
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + requestPath);
    String resultPath = res.toString();
    if (resultPath == null) {
        resultPath = "";
    }
    assertEquals(expected, resultPath);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 2 with Tomcat

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

the class TestAbortedUpload method configureAndStartWebApplication.

@Override
protected void configureAndStartWebApplication() throws LifecycleException {
    Tomcat tomcat = getTomcatInstance();
    // Retain '/simple' url-pattern since it enables code re-use
    Context ctxt = tomcat.addContext("", null);
    Tomcat.addServlet(ctxt, "abort", new AbortServlet());
    ctxt.addServletMappingDecoded("/simple", "abort");
    tomcat.start();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat)

Example 3 with Tomcat

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

the class TestCookiesDefaultSysProps method testCookiesInstance.

@Override
@Test
public void testCookiesInstance() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    addServlets(tomcat);
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/invalid");
    assertEquals("Cookie name fail", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/null");
    assertEquals("Cookie name fail", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/blank");
    assertEquals("Cookie name fail", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/invalidFwd");
    assertEquals("Cookie name fail", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/invalidStrict");
    assertEquals("Cookie name ok", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/valid");
    assertEquals("Cookie name ok", res.toString());
    // Need to read response headers to test version switching
    Map<String, List<String>> headers = new HashMap<>();
    getUrl("http://localhost:" + getPort() + "/switch", res, headers);
    List<String> cookieHeaders = headers.get("Set-Cookie");
    for (String cookieHeader : cookieHeaders) {
        assertEquals("name=\"val?ue\"; Version=1", cookieHeader);
    }
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) List(java.util.List) Test(org.junit.Test)

Example 4 with Tomcat

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

the class TestNonBlockingAPI method testNonBlockingWriteError.

@Test
public void testNonBlockingWriteError() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    NBWriteServlet servlet = new NBWriteServlet();
    String servletName = NBWriteServlet.class.getName();
    Tomcat.addServlet(ctx, servletName, servlet);
    ctx.addServletMappingDecoded("/", servletName);
    tomcat.getConnector().setProperty("socket.txBufSize", "1024");
    tomcat.start();
    SocketFactory factory = SocketFactory.getDefault();
    Socket s = factory.createSocket("localhost", getPort());
    ByteChunk result = new ByteChunk();
    OutputStream os = s.getOutputStream();
    os.write(("GET / HTTP/1.1\r\n" + "Host: localhost:" + getPort() + "\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
    os.flush();
    InputStream is = s.getInputStream();
    byte[] buffer = new byte[8192];
    int read = 0;
    int readSinceLastPause = 0;
    int readTotal = 0;
    while (read != -1 && readTotal < WRITE_SIZE / 32) {
        long start = System.currentTimeMillis();
        read = is.read(buffer);
        long end = System.currentTimeMillis();
        log.info("Client read [" + read + "] bytes in [" + (end - start) + "] ms");
        if (read > 0) {
            result.append(buffer, 0, read);
        }
        readSinceLastPause += read;
        readTotal += read;
        if (readSinceLastPause > WRITE_SIZE / 64) {
            readSinceLastPause = 0;
            Thread.sleep(WRITE_PAUSE_MS);
        }
    }
    os.close();
    is.close();
    s.close();
    String resultString = result.toString();
    log.info("Client read " + resultString.length() + " bytes");
    int lineStart = 0;
    int lineEnd = resultString.indexOf('\n', 0);
    String line = resultString.substring(lineStart, lineEnd + 1);
    Assert.assertEquals("HTTP/1.1 200 \r\n", line);
    // Listeners are invoked and access valve entries created on a different
    // thread so give that thread a chance to complete its work.
    int count = 0;
    while (count < 100 && !(servlet.wlistener.onErrorInvoked || servlet.rlistener.onErrorInvoked)) {
        Thread.sleep(100);
        count++;
    }
    while (count < 100 && alv.getEntryCount() < 1) {
        Thread.sleep(100);
        count++;
    }
    Assert.assertTrue("Error listener should have been invoked.", servlet.wlistener.onErrorInvoked || servlet.rlistener.onErrorInvoked);
    // TODO Figure out why non-blocking writes with the NIO connector appear
    // to be slower on Linux
    alv.validateAccessLog(1, 500, WRITE_PAUSE_MS, WRITE_PAUSE_MS + 30 * 1000);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) SocketFactory(javax.net.SocketFactory) ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) Socket(java.net.Socket) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 5 with Tomcat

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

the class TestStandardWrapper method testBug51445AddServlet.

@Test
public void testBug51445AddServlet() throws Exception {
    initLatch();
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "Bug51445", new Bug51445Servlet());
    ctx.addServletMappingDecoded("/", "Bug51445");
    tomcat.start();
    // Start the threads
    Bug51445Thread[] threads = new Bug51445Thread[5];
    for (int i = 0; i < BUG51445_THREAD_COUNT; i++) {
        threads[i] = new Bug51445Thread(getPort());
        threads[i].start();
    }
    // Wait for threads to finish
    for (int i = 0; i < BUG51445_THREAD_COUNT; i++) {
        threads[i].join();
    }
    Set<String> servlets = new HashSet<>();
    // Output the result
    for (int i = 0; i < BUG51445_THREAD_COUNT; i++) {
        System.out.println(threads[i].getResult());
    }
    // Check the result
    for (int i = 0; i < BUG51445_THREAD_COUNT; i++) {
        String[] results = threads[i].getResult().split(",");
        assertEquals(2, results.length);
        assertEquals("10", results[0]);
        assertFalse(servlets.contains(results[1]));
        servlets.add(results[1]);
    }
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Tomcat(org.apache.catalina.startup.Tomcat) HttpConstraint(javax.servlet.annotation.HttpConstraint) HttpMethodConstraint(javax.servlet.annotation.HttpMethodConstraint) HashSet(java.util.HashSet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

Tomcat (org.apache.catalina.startup.Tomcat)850 Test (org.junit.Test)592 Context (org.apache.catalina.Context)510 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)506 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)363 File (java.io.File)319 Wrapper (org.apache.catalina.Wrapper)111 AsyncContext (jakarta.servlet.AsyncContext)103 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)92 List (java.util.List)84 StandardContext (org.apache.catalina.core.StandardContext)77 URI (java.net.URI)63 HashMap (java.util.HashMap)62 AsyncContext (javax.servlet.AsyncContext)50 ArrayList (java.util.ArrayList)48 IOException (java.io.IOException)46 TesterContext (org.apache.tomcat.unittest.TesterContext)42 Connector (org.apache.catalina.connector.Connector)41 Session (jakarta.websocket.Session)39 WebSocketContainer (jakarta.websocket.WebSocketContainer)38