Search in sources :

Example 26 with Context

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

Example 27 with Context

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

the class TestCoyoteAdapter method pathParamExtensionTest.

private void pathParamExtensionTest(String path, String expected) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("/testapp", null);
    Tomcat.addServlet(ctx, "servlet", new PathParamServlet());
    ctx.addServletMappingDecoded("*.txt", "servlet");
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + path);
    Assert.assertEquals(expected, res.toString());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 28 with Context

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

the class TestCoyoteAdapter method doTestUriDecoding.

private void doTestUriDecoding(String path, String encoding, String expectedPathInfo) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    tomcat.getConnector().setURIEncoding(encoding);
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    PathInfoServlet servlet = new PathInfoServlet();
    Tomcat.addServlet(ctx, "servlet", servlet);
    ctx.addServletMappingDecoded("/*", "servlet");
    tomcat.start();
    int rc = getUrl("http://localhost:" + getPort() + path, new ByteChunk(), null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    Assert.assertEquals(expectedPathInfo, servlet.getPathInfo());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 29 with Context

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

the class TestApplicationContextGetRequestDispatcher method doTestGetRequestDispatcher.

private void doTestGetRequestDispatcher(boolean useEncodedDispatchPaths, String startPath, String startQueryString, String dispatchPath, String targetPath, String expectedBody) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("/test", null);
    ctx.setDispatchersUseEncodedPaths(useEncodedDispatchPaths);
    // Add a default servlet to return 404 for not found resources
    Tomcat.addServlet(ctx, "Default", new Default404Servlet());
    ctx.addServletMappingDecoded("/*", "Default");
    // Add a target servlet to dispatch to
    Tomcat.addServlet(ctx, "target", new TargetServlet());
    ctx.addServletMappingDecoded(UDecoder.URLDecode(targetPath, "UTF-8"), "target");
    if (useAsync) {
        Wrapper w = Tomcat.addServlet(ctx, "rd", new AsyncDispatcherServlet(dispatchPath, useEncodedDispatchPaths));
        w.setAsyncSupported(true);
    } else {
        Tomcat.addServlet(ctx, "rd", new DispatcherServlet(dispatchPath));
    }
    ctx.addServletMappingDecoded(UDecoder.URLDecode(startPath, "UTF-8"), "rd");
    tomcat.start();
    StringBuilder url = new StringBuilder("http://localhost:");
    url.append(getPort());
    url.append("/test");
    url.append(startPath);
    if (startQueryString != null) {
        url.append('?');
        url.append(startQueryString);
    }
    ByteChunk bc = getUrl(url.toString());
    String body = bc.toString();
    Assert.assertEquals(expectedBody, body);
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 30 with Context

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

the class TestApplicationHttpRequest method testParameterImmutability.

@Test
public void testParameterImmutability() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "forward", new ForwardServlet("/modify"));
    ctx.addServletMappingDecoded("/forward", "forward");
    Tomcat.addServlet(ctx, "modify", new ModifyParameterServlet());
    ctx.addServletMappingDecoded("/modify", "modify");
    tomcat.start();
    ByteChunk response = new ByteChunk();
    StringBuilder target = new StringBuilder("http://localhost:");
    target.append(getPort());
    target.append("/forward");
    int rc = getUrl(target.toString(), response, null);
    Assert.assertEquals(200, rc);
    Assert.assertEquals("OK", response.toString());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) 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