Search in sources :

Example 26 with Tomcat

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

the class TestAsyncContextImpl method testTimeoutDispatchCustomErrorPage.

/*
     * See https://bz.apache.org/bugzilla/show_bug.cgi?id=58751 comment 1
     */
@Test
public void testTimeoutDispatchCustomErrorPage() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context context = tomcat.addContext("", null);
    tomcat.addServlet("", "timeout", Bug58751AsyncServlet.class.getName()).setAsyncSupported(true);
    CustomErrorServlet customErrorServlet = new CustomErrorServlet();
    Tomcat.addServlet(context, "customErrorServlet", customErrorServlet);
    context.addServletMappingDecoded("/timeout", "timeout");
    context.addServletMappingDecoded("/error", "customErrorServlet");
    ErrorPage errorPage = new ErrorPage();
    errorPage.setLocation("/error");
    context.addErrorPage(errorPage);
    tomcat.start();
    ByteChunk responseBody = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/timeout", responseBody, null);
    Assert.assertEquals(503, rc);
    Assert.assertEquals(CustomErrorServlet.ERROR_MESSAGE, responseBody.toString());
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Tomcat(org.apache.catalina.startup.Tomcat) ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 27 with Tomcat

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

the class TestAsyncContextImpl method doTestBug51197.

private void doTestBug51197(boolean threaded, boolean customError) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncErrorServlet asyncErrorServlet = new AsyncErrorServlet(HttpServletResponse.SC_BAD_REQUEST, threaded);
    Wrapper wrapper = Tomcat.addServlet(ctx, "asyncErrorServlet", asyncErrorServlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/asyncErrorServlet", "asyncErrorServlet");
    if (customError) {
        CustomErrorServlet customErrorServlet = new CustomErrorServlet();
        Tomcat.addServlet(ctx, "customErrorServlet", customErrorServlet);
        ctx.addServletMappingDecoded("/customErrorServlet", "customErrorServlet");
        ErrorPage ep = new ErrorPage();
        ep.setLocation("/customErrorServlet");
        ctx.addErrorPage(ep);
    }
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/asyncErrorServlet");
    ByteChunk res = new ByteChunk();
    int rc = getUrl(url.toString(), res, null);
    assertEquals(HttpServletResponse.SC_BAD_REQUEST, rc);
    // SRV 10.9.2 - Handling an error is entirely the application's
    // responsibility when an error occurs on an application thread.
    // Calling sendError() followed by complete() and expecting the standard
    // error page mechanism to kick in could be viewed as handling the error
    String responseBody = res.toString();
    Assert.assertNotNull(responseBody);
    assertTrue(responseBody.length() > 0);
    if (customError) {
        assertTrue(responseBody, responseBody.contains(CustomErrorServlet.ERROR_MESSAGE));
    } else {
        assertTrue(responseBody, responseBody.contains(AsyncErrorServlet.ERROR_MESSAGE));
    }
    // Without this test may complete before access log has a chance to log
    // the request
    Thread.sleep(REQUEST_TIME);
    // Check the access log
    alv.validateAccessLog(1, HttpServletResponse.SC_BAD_REQUEST, 0, 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) ErrorPage(org.apache.tomcat.util.descriptor.web.ErrorPage) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

Example 28 with Tomcat

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

the class TestAsyncContextImpl method prepareApplicationWithGenericServlet.

private void prepareApplicationWithGenericServlet(String contextPath) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext(contextPath, null);
    DispatchingGenericServlet dispatch = new DispatchingGenericServlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/dispatch", "dispatch");
    CustomGenericServlet customGeneric = new CustomGenericServlet();
    Wrapper wrapper2 = Tomcat.addServlet(ctx, "customGeneric", customGeneric);
    wrapper2.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/target", "customGeneric");
    tomcat.start();
}
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)

Example 29 with Tomcat

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

the class TestAsyncContextImpl method testBug54178.

@Test
public void testBug54178() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug54178ServletA bug54178ServletA = new Bug54178ServletA();
    Wrapper wrapper = Tomcat.addServlet(ctx, "bug54178ServletA", bug54178ServletA);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/bug54178ServletA", "bug54178ServletA");
    Bug54178ServletB bug54178ServletB = new Bug54178ServletB();
    Tomcat.addServlet(ctx, "bug54178ServletB", bug54178ServletB);
    ctx.addServletMappingDecoded("/bug54178ServletB", "bug54178ServletB");
    tomcat.start();
    ByteChunk body = new ByteChunk();
    int rc = -1;
    try {
        rc = getUrl("http://localhost:" + getPort() + "/bug54178ServletA?" + Bug54178ServletA.PARAM_NAME + "=bar", body, null);
    } catch (IOException ioe) {
        // This may happen if test fails. Output the exception in case it is
        // useful and let asserts handle the failure
        ioe.printStackTrace();
    }
    assertEquals(HttpServletResponse.SC_OK, rc);
    body.recycle();
    rc = getUrl("http://localhost:" + getPort() + "/bug54178ServletB", body, null);
    assertEquals(HttpServletResponse.SC_OK, rc);
    assertEquals("OK", body.toString());
}
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) IOException(java.io.IOException) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 30 with Tomcat

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

the class TestAsyncContextImpl method testForbiddenDispatching.

@Test
public void testForbiddenDispatching() throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    NonAsyncServlet nonAsyncServlet = new NonAsyncServlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "nonAsyncServlet", nonAsyncServlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/target", "nonAsyncServlet");
    DispatchingGenericServlet forbiddenDispatchingServlet = new DispatchingGenericServlet();
    Wrapper wrapper1 = Tomcat.addServlet(ctx, "forbiddenDispatchingServlet", forbiddenDispatchingServlet);
    wrapper1.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/forbiddenDispatchingServlet", "forbiddenDispatchingServlet");
    tomcat.start();
    try {
        getUrl("http://localhost:" + getPort() + "/forbiddenDispatchingServlet");
    } catch (IOException ioe) {
        // This may happen if test fails. Output the exception in case it is
        // useful and let asserts handle the failure
        ioe.printStackTrace();
    }
    // Request may complete before listener has finished processing so wait
    // up to 5 seconds for the right response
    String expectedTrack = "OKNonAsyncServletGet-";
    int count = 0;
    while (!expectedTrack.equals(getTrack()) && count < 100) {
        Thread.sleep(50);
        count++;
    }
    Assert.assertEquals(expectedTrack, getTrack());
}
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) IOException(java.io.IOException) 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