Search in sources :

Example 66 with ByteChunk

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

the class TesterAjpNonBlockingClient method doTestAJPNonBlockingRead.

@Test
public void doTestAJPNonBlockingRead() throws Exception {
    Map<String, List<String>> resHeaders = new HashMap<>();
    ByteChunk out = new ByteChunk();
    int rc = postUrl(true, new DataWriter(2000), "http://localhost" + "/examples/servlets/nonblocking/bytecounter", out, resHeaders, null);
    System.out.println(out.toString());
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}
Also used : HashMap(java.util.HashMap) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) List(java.util.List) DataWriter(org.apache.catalina.nonblocking.TestNonBlockingAPI.DataWriter) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 67 with ByteChunk

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

the class TesterAjpNonBlockingClient method testNonBlockingWrite.

@Test
public void testNonBlockingWrite() throws Exception {
    SocketFactory factory = SocketFactory.getDefault();
    Socket s = factory.createSocket("localhost", 80);
    ByteChunk result = new ByteChunk();
    OutputStream os = s.getOutputStream();
    os.write(("GET /examples/servlets/nonblocking/numberwriter HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
    os.flush();
    InputStream is = s.getInputStream();
    byte[] buffer = new byte[8192];
    int read = 0;
    int readSinceLastPause = 0;
    while (read != -1) {
        read = is.read(buffer);
        if (read > 0) {
            result.append(buffer, 0, read);
        }
        readSinceLastPause += read;
        if (readSinceLastPause > 40000) {
            readSinceLastPause = 0;
            Thread.sleep(500);
        }
    }
    os.close();
    is.close();
    s.close();
    // Validate the result
    String resultString = result.toString();
    log.info("Client read " + resultString.length() + " bytes");
    System.out.println(resultString);
    Assert.assertTrue(resultString.contains("00000000000000010000"));
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) SocketFactory(javax.net.SocketFactory) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 68 with ByteChunk

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

the class TestMapperListener method testTomcatRestartListenerCount_Bug56717.

@Test
public void testTomcatRestartListenerCount_Bug56717() throws IOException, LifecycleException {
    // The test runs Tomcat twice, tests that it has started successfully,
    // and compares the counts of listeners registered on containers
    // after the first and the second starts.
    // Sample request is from TestTomcat#testSingleWebapp()
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File(getBuildDirectory(), "webapps/examples");
    // app dir is relative to server home
    Context ctxt = tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
    ctxt.addApplicationListener(WsContextListener.class.getName());
    tomcat.start();
    ByteChunk res;
    String text;
    res = getUrl("http://localhost:" + getPort() + "/examples/servlets/servlet/HelloWorldExample");
    text = res.toString();
    Assert.assertTrue(text, text.contains("<a href=\"../helloworld.html\">"));
    List<ListenersInfo> listenersFirst = new ArrayList<>();
    populateListenersInfo(listenersFirst, tomcat.getEngine());
    tomcat.stop();
    tomcat.start();
    res = getUrl("http://localhost:" + getPort() + "/examples/servlets/servlet/HelloWorldExample");
    text = res.toString();
    Assert.assertTrue(text, text.contains("<a href=\"../helloworld.html\">"));
    List<ListenersInfo> listenersSecond = new ArrayList<>();
    populateListenersInfo(listenersSecond, tomcat.getEngine());
    Assert.assertEquals(listenersFirst.size(), listenersSecond.size());
    for (int i = 0, len = listenersFirst.size(); i < len; i++) {
        ListenersInfo a = listenersFirst.get(i);
        ListenersInfo b = listenersSecond.get(i);
        boolean equal = a.container.getClass() == b.container.getClass() && a.containerListeners.length == b.containerListeners.length && a.lifecycleListeners.length == b.lifecycleListeners.length;
        if (!equal) {
            Assert.fail("The lists of listeners differ:\n" + a + "\n" + b);
        }
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WsContextListener(org.apache.tomcat.websocket.server.WsContextListener) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ArrayList(java.util.ArrayList) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 69 with ByteChunk

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

the class TestStandardContextResources method assertPageContains.

private void assertPageContains(String pageUrl, String expectedBody, int expectedStatus) throws IOException {
    ByteChunk res = new ByteChunk();
    int sc = getUrl("http://localhost:" + getPort() + pageUrl, res, null);
    assertEquals(expectedStatus, sc);
    if (expectedStatus == 200) {
        String result = res.toString();
        assertTrue(result, result.indexOf(expectedBody) > 0);
    }
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 70 with ByteChunk

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

the class TestStandardContextValve method testBug51653a.

@Test
public void testBug51653a() throws Exception {
    // Set up a container
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Traces order of events across multiple components
    StringBuilder trace = new StringBuilder();
    //Add the error page
    Tomcat.addServlet(ctx, "errorPage", new Bug51653ErrorPage(trace));
    ctx.addServletMappingDecoded("/error", "errorPage");
    // And the handling for 404 responses
    ErrorPage errorPage = new ErrorPage();
    errorPage.setErrorCode(Response.SC_NOT_FOUND);
    errorPage.setLocation("/error");
    ctx.addErrorPage(errorPage);
    // Add the request listener
    Bug51653RequestListener reqListener = new Bug51653RequestListener(trace);
    ((StandardContext) ctx).addApplicationEventListener(reqListener);
    tomcat.start();
    // Request a page that does not exist
    int rc = getUrl("http://localhost:" + getPort() + "/invalid", new ByteChunk(), null);
    // Need to allow time (but not too long in case the test fails) for
    // ServletRequestListener to complete
    int i = 20;
    while (i > 0) {
        if (trace.toString().endsWith("Destroy")) {
            break;
        }
        Thread.sleep(250);
        i--;
    }
    assertEquals(Response.SC_NOT_FOUND, rc);
    assertEquals("InitErrorDestroy", trace.toString());
}
Also used : Context(org.apache.catalina.Context) 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)

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