Search in sources :

Example 21 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk 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 22 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk 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 23 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk 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 24 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk 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 25 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk 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

ByteChunk (org.apache.tomcat.util.buf.ByteChunk)274 Test (org.junit.Test)201 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)180 Tomcat (org.apache.catalina.startup.Tomcat)129 Context (org.apache.catalina.Context)98 File (java.io.File)49 List (java.util.List)48 AsyncContext (javax.servlet.AsyncContext)40 HashMap (java.util.HashMap)35 Wrapper (org.apache.catalina.Wrapper)22 StandardContext (org.apache.catalina.core.StandardContext)21 ArrayList (java.util.ArrayList)20 TesterContext (org.apache.tomcat.unittest.TesterContext)18 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)16 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)13 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)13 ServletContext (javax.servlet.ServletContext)10 WsContextListener (org.apache.tomcat.websocket.server.WsContextListener)10 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)9 InitialContext (javax.naming.InitialContext)8