Search in sources :

Example 1 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve in project tomcat by apache.

the class TestAsyncContextImpl method testCommitOnComplete.

@Test
public void testCommitOnComplete() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncStatusServlet asyncStatusServlet = new AsyncStatusServlet(HttpServletResponse.SC_BAD_REQUEST);
    Wrapper wrapper = Tomcat.addServlet(ctx, "asyncStatusServlet", asyncStatusServlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/asyncStatusServlet", "asyncStatusServlet");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/asyncStatusServlet");
    int rc = getUrl(url.toString(), new ByteChunk(), null);
    assertEquals(HttpServletResponse.SC_BAD_REQUEST, rc);
    // 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) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 2 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve in project tomcat by apache.

the class TestAsyncContextImpl method doTestTimeoutErrorDispatch.

private void doTestTimeoutErrorDispatch(Boolean asyncError, ErrorPageAsyncMode mode) throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    TimeoutServlet timeout = new TimeoutServlet(null, null);
    Wrapper w1 = Tomcat.addServlet(ctx, "time", timeout);
    w1.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/async", "time");
    NonAsyncServlet nonAsync = new NonAsyncServlet();
    Wrapper w2 = Tomcat.addServlet(ctx, "nonAsync", nonAsync);
    w2.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/error/nonasync", "nonAsync");
    AsyncErrorPage asyncErrorPage = new AsyncErrorPage(mode);
    Wrapper w3 = Tomcat.addServlet(ctx, "asyncErrorPage", asyncErrorPage);
    w3.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/error/async", "asyncErrorPage");
    if (asyncError != null) {
        ErrorPage ep = new ErrorPage();
        ep.setErrorCode(500);
        if (asyncError.booleanValue()) {
            ep.setLocation("/error/async");
        } else {
            ep.setLocation("/error/nonasync");
        }
        ctx.addErrorPage(ep);
    }
    ctx.addApplicationListener(TrackingRequestListener.class.getName());
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    TesterAccessLogValve alvGlobal = new TesterAccessLogValve();
    tomcat.getHost().getPipeline().addValve(alvGlobal);
    tomcat.start();
    ByteChunk res = new ByteChunk();
    try {
        getUrl("http://localhost:" + getPort() + "/async", res, null);
    } catch (IOException ioe) {
    // Ignore - expected for some error conditions
    }
    StringBuilder expected = new StringBuilder();
    expected.append("requestInitialized-TimeoutServletGet-");
    if (asyncError != null) {
        if (asyncError.booleanValue()) {
            expected.append("AsyncErrorPageGet-");
            if (mode == ErrorPageAsyncMode.NO_COMPLETE) {
                expected.append("NoOp-");
            } else if (mode == ErrorPageAsyncMode.COMPLETE) {
                expected.append("Complete-");
            } else if (mode == ErrorPageAsyncMode.DISPATCH) {
                expected.append("Dispatch-NonAsyncServletGet-");
            }
        } else {
            expected.append("NonAsyncServletGet-");
        }
    }
    expected.append("requestDestroyed");
    // Request may complete before listener has finished processing so wait
    // up to 5 seconds for the right response
    String expectedTrack = expected.toString();
    int count = 0;
    while (!expectedTrack.equals(getTrack()) && count < 100) {
        Thread.sleep(50);
        count++;
    }
    Assert.assertEquals(expectedTrack, getTrack());
    // Check the access log
    alvGlobal.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
    alv.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + 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) IOException(java.io.IOException) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

Example 3 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve in project tomcat by apache.

the class TestAsyncContextImpl method doTestTimeout.

private void doTestTimeout(Boolean completeOnTimeout, Boolean asyncDispatch) throws Exception {
    resetTracker();
    String dispatchUrl = null;
    if (asyncDispatch != null) {
        if (asyncDispatch.booleanValue()) {
            dispatchUrl = "/async";
        } else {
            dispatchUrl = "/nonasync";
        }
    }
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    TimeoutServlet timeout = new TimeoutServlet(completeOnTimeout, dispatchUrl);
    Wrapper wrapper = Tomcat.addServlet(ctx, "time", timeout);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/start", "time");
    if (asyncDispatch != null) {
        if (asyncDispatch.booleanValue()) {
            AsyncStartRunnable asyncStartRunnable = new AsyncStartRunnable();
            Wrapper async = Tomcat.addServlet(ctx, "async", asyncStartRunnable);
            async.setAsyncSupported(true);
            ctx.addServletMappingDecoded(dispatchUrl, "async");
        } else {
            NonAsyncServlet nonAsync = new NonAsyncServlet();
            Tomcat.addServlet(ctx, "nonasync", nonAsync);
            ctx.addServletMappingDecoded(dispatchUrl, "nonasync");
        }
    }
    ctx.addApplicationListener(TrackingRequestListener.class.getName());
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    TesterAccessLogValve alvGlobal = new TesterAccessLogValve();
    tomcat.getHost().getPipeline().addValve(alvGlobal);
    tomcat.start();
    try {
        getUrl("http://localhost:" + getPort() + "/start");
    } catch (IOException ioe) {
    // Ignore - expected for some error conditions
    }
    StringBuilder expected = new StringBuilder("requestInitialized-");
    expected.append("TimeoutServletGet-");
    if (completeOnTimeout == null) {
        expected.append("requestDestroyed");
    } else if (completeOnTimeout.booleanValue()) {
        expected.append("onTimeout-");
        expected.append("onComplete-");
        expected.append("requestDestroyed");
    } else {
        expected.append("onTimeout-");
        if (asyncDispatch != null) {
            if (asyncDispatch.booleanValue()) {
                expected.append("onStartAsync-Runnable-");
            } else {
                expected.append("NonAsyncServletGet-");
            }
        }
        expected.append("onComplete-");
        expected.append("requestDestroyed");
    }
    // Request may complete before listener has finished processing so wait
    // up to 5 seconds for the right response
    String expectedTrack = expected.toString();
    int count = 0;
    while (!expectedTrack.equals(getTrack()) && count < 100) {
        Thread.sleep(50);
        count++;
    }
    assertEquals(expectedTrack, getTrack());
    // Check the access log
    if (completeOnTimeout == null || (!completeOnTimeout.booleanValue() && asyncDispatch == null)) {
        alvGlobal.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
        alv.validateAccessLog(1, 500, TimeoutServlet.ASYNC_TIMEOUT, TimeoutServlet.ASYNC_TIMEOUT + TIMEOUT_MARGIN + REQUEST_TIME);
    } else {
        long timeoutDelay = TimeoutServlet.ASYNC_TIMEOUT;
        if (asyncDispatch != null && asyncDispatch.booleanValue() && !completeOnTimeout.booleanValue()) {
            // The async dispatch includes a sleep
            timeoutDelay += AsyncStartRunnable.THREAD_SLEEP_TIME;
        }
        alvGlobal.validateAccessLog(1, 200, timeoutDelay, timeoutDelay + TIMEOUT_MARGIN + REQUEST_TIME);
        alv.validateAccessLog(1, 200, timeoutDelay, timeoutDelay + TIMEOUT_MARGIN + 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) IOException(java.io.IOException) TesterAccessLogValve(org.apache.catalina.valves.TesterAccessLogValve)

Example 4 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve in project tomcat by apache.

the class TestAsyncContextImpl method testBug49528.

@Test
public void testBug49528() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Bug49528Servlet servlet = new Bug49528Servlet();
    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, Bug49528Servlet.THREAD_SLEEP_TIME, Bug49528Servlet.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)

Example 5 with TesterAccessLogValve

use of org.apache.catalina.valves.TesterAccessLogValve in project tomcat by apache.

the class TestAsyncContextImpl method testErrorHandling.

@Test
public void testErrorHandling() throws Exception {
    resetTracker();
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ErrorServlet error = new ErrorServlet();
    Tomcat.addServlet(ctx, "error", error);
    ctx.addServletMappingDecoded("/error", "error");
    TesterAccessLogValve alv = new TesterAccessLogValve();
    ctx.getPipeline().addValve(alv);
    tomcat.start();
    StringBuilder url = new StringBuilder(48);
    url.append("http://localhost:");
    url.append(getPort());
    url.append("/error");
    int rc = getUrl(url.toString(), new ByteChunk(), null);
    assertEquals(500, rc);
    // 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, 500, 0, REQUEST_TIME);
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) 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

AsyncContext (javax.servlet.AsyncContext)16 Context (org.apache.catalina.Context)16 Tomcat (org.apache.catalina.startup.Tomcat)16 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)16 TesterContext (org.apache.tomcat.unittest.TesterContext)15 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)14 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)14 Wrapper (org.apache.catalina.Wrapper)14 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)11 Test (org.junit.Test)11 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)9 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 ErrorPage (org.apache.tomcat.util.descriptor.web.ErrorPage)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 HashMap (java.util.HashMap)1 SocketFactory (javax.net.SocketFactory)1