Search in sources :

Example 41 with Context

use of org.apache.catalina.Context 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 42 with Context

use of org.apache.catalina.Context 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)

Example 43 with Context

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

the class TestCoyoteOutputStream method doNonBlockingTest.

private void doNonBlockingTest(int asyncWriteTarget, int syncWriteTarget, boolean useContainerThreadToSetListener) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    Wrapper w = Tomcat.addServlet(root, "nbWrite", new NonBlockingWriteServlet(asyncWriteTarget, useContainerThreadToSetListener));
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/nbWrite", "nbWrite");
    Tomcat.addServlet(root, "write", new BlockingWriteServlet(asyncWriteTarget, syncWriteTarget));
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/write", "write");
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    // Extend timeout to 5 mins for debugging
    int rc = getUrl("http://localhost:" + getPort() + "/nbWrite", bc, 300000, null, null);
    int totalCount = asyncWriteTarget + syncWriteTarget;
    StringBuilder sb = new StringBuilder(totalCount * 16);
    for (int i = 0; i < totalCount; i++) {
        sb.append("OK - " + i + System.lineSeparator());
    }
    String expected = null;
    if (sb.length() > 0) {
        expected = sb.toString();
    }
    Assert.assertEquals(200, rc);
    Assert.assertEquals(expected, bc.toString());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 44 with Context

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

the class TestOutputBuffer method testBug52577.

@Test
public void testBug52577() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    Bug52577Servlet bug52577 = new Bug52577Servlet();
    Tomcat.addServlet(root, "bug52577", bug52577);
    root.addServletMappingDecoded("/", "bug52577");
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);
    assertEquals(HttpServletResponse.SC_OK, rc);
    assertEquals("OK", 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 45 with Context

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

the class TestRequest method testBug49424WithChunking.

@Test
public void testBug49424WithChunking() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    Tomcat.addServlet(root, "Bug37794", new Bug37794Servlet());
    root.addServletMappingDecoded("/", "Bug37794");
    tomcat.start();
    HttpURLConnection conn = getConnection("http://localhost:" + getPort() + "/");
    conn.setChunkedStreamingMode(8 * 1024);
    InputStream is = conn.getInputStream();
    assertNotNull(is);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) 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