Search in sources :

Example 31 with Context

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

the class TestApplicationHttpRequest method doQueryStringTest.

private void doQueryStringTest(String originalQueryString, String forwardQueryString, Map<String, String[]> expected) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    if (forwardQueryString == null) {
        Tomcat.addServlet(ctx, "forward", new ForwardServlet("/display"));
    } else {
        Tomcat.addServlet(ctx, "forward", new ForwardServlet("/display?" + forwardQueryString));
    }
    ctx.addServletMappingDecoded("/forward", "forward");
    Tomcat.addServlet(ctx, "display", new DisplayParameterServlet(expected));
    ctx.addServletMappingDecoded("/display", "display");
    tomcat.start();
    ByteChunk response = new ByteChunk();
    StringBuilder target = new StringBuilder("http://localhost:");
    target.append(getPort());
    target.append("/forward");
    if (originalQueryString != null) {
        target.append('?');
        target.append(originalQueryString);
    }
    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)

Example 32 with Context

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

the class TestApplicationMapping method doTestMappingNamedInclude.

private void doTestMappingNamedInclude(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext(contextPath, null);
    Tomcat.addServlet(ctx, "Include", new NamedIncludeServlet());
    ctx.addServletMappingDecoded(mapping, "Include");
    Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
    ctx.addServletMappingDecoded("/mapping", "Mapping");
    tomcat.start();
    ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
    String body = bc.toString();
    Assert.assertTrue(body, body.contains("MatchValue=[" + matchValue + "]"));
    Assert.assertTrue(body, body.contains("Pattern=[" + mapping + "]"));
    Assert.assertTrue(body, body.contains("MatchType=[" + matchType + "]"));
    Assert.assertTrue(body, body.contains("ServletName=[Include]"));
}
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 33 with Context

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

the class TestApplicationMapping method doTestMappingInclude.

private void doTestMappingInclude(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext(contextPath, null);
    Tomcat.addServlet(ctx, "Include", new IncludeServlet());
    ctx.addServletMappingDecoded(mapping, "Include");
    Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
    ctx.addServletMappingDecoded("/mapping", "Mapping");
    tomcat.start();
    ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
    String body = bc.toString();
    Assert.assertTrue(body, body.contains("MatchValue=[" + matchValue + "]"));
    Assert.assertTrue(body, body.contains("Pattern=[" + mapping + "]"));
    Assert.assertTrue(body, body.contains("MatchType=[" + matchType + "]"));
    Assert.assertTrue(body, body.contains("ServletName=[Include]"));
    Assert.assertTrue(body, body.contains("IncludeMatchValue=[mapping]"));
    Assert.assertTrue(body, body.contains("IncludePattern=[/mapping]"));
    Assert.assertTrue(body, body.contains("IncludeMatchType=[EXACT]"));
    Assert.assertTrue(body, body.contains("IncludeServletName=[Mapping]"));
}
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 34 with Context

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

the class TestApplicationMapping method doTestMappingNamedForward.

private void doTestMappingNamedForward(String contextPath, String mapping, String requestPath, String matchValue, String matchType) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext(contextPath, null);
    Tomcat.addServlet(ctx, "Forward", new NamedForwardServlet());
    ctx.addServletMappingDecoded(mapping, "Forward");
    Tomcat.addServlet(ctx, "Mapping", new MappingServlet());
    ctx.addServletMappingDecoded("/mapping", "Mapping");
    tomcat.start();
    ByteChunk bc = getUrl("http://localhost:" + getPort() + contextPath + requestPath);
    String body = bc.toString();
    Assert.assertTrue(body, body.contains("MatchValue=[" + matchValue + "]"));
    Assert.assertTrue(body, body.contains("Pattern=[" + mapping + "]"));
    Assert.assertTrue(body, body.contains("MatchType=[" + matchType + "]"));
    Assert.assertTrue(body, body.contains("ServletName=[Forward]"));
}
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 35 with Context

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

the class TestAsyncContextImpl method testBug49567.

@Test
public void testBug49567() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug49567Servlet servlet = new Bug49567Servlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "servlet", servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/", "servlet");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    // Call the servlet once
    ByteChunk bc = getUrl("http://localhost:" + getPort() + "/");
    assertEquals("OK", bc.toString());
    // Give the async thread a chance to finish (but not too long)
    int counter = 0;
    while (!servlet.isDone() && counter < 10) {
        Thread.sleep(1000);
        counter++;
    }
    assertEquals("1false2true3true4true5false", servlet.getResult());
    // Check the access log
    alv.validateAccessLog(1, 200, Bug49567Servlet.THREAD_SLEEP_TIME, Bug49567Servlet.THREAD_SLEEP_TIME + REQUEST_TIME);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Wrapper(org.apache.catalina.Wrapper) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) 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)

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