Search in sources :

Example 26 with ByteChunk

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

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

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

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestCoyoteInputStream method testReadWithByteBuffer.

@Test
public void testReadWithByteBuffer() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    Tomcat.addServlet(root, "testServlet", new TestServlet());
    root.addServletMappingDecoded("/", "testServlet");
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    String requestBody = "HelloWorld";
    int rc = postUrl(requestBody.getBytes(StandardCharsets.UTF_8), "http://localhost:" + getPort() + "/", bc, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    Assert.assertTrue(requestBody.equals(bc.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)

Example 30 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestCoyoteOutputStream method testWriteWithByteBuffer.

@Test
public void testWriteWithByteBuffer() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    Tomcat.addServlet(root, "testServlet", new TestServlet());
    root.addServletMappingDecoded("/", "testServlet");
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    File file = new File("test/org/apache/catalina/connector/test_content.txt");
    try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
        ByteChunk expected = new ByteChunk();
        expected.append(raf.getChannel().map(MapMode.READ_ONLY, 0, file.length()));
        Assert.assertTrue(expected.equals(bc));
    }
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Tomcat(org.apache.catalina.startup.Tomcat) RandomAccessFile(java.io.RandomAccessFile) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) 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