Search in sources :

Example 11 with ByteChunk

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

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

Example 13 with ByteChunk

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

the class TestSSOnonLoginAndDigestAuthenticator method doTestNonLogin.

public void doTestNonLogin(String uri, boolean addCookies, boolean expectedReject, int expectedRC) throws Exception {
    Map<String, List<String>> reqHeaders = new HashMap<>();
    Map<String, List<String>> respHeaders = new HashMap<>();
    ByteChunk bc = new ByteChunk();
    if (addCookies) {
        addCookies(reqHeaders);
    }
    int rc = getUrl(HTTP_PREFIX + getPort() + uri, bc, reqHeaders, respHeaders);
    if (expectedReject) {
        assertEquals(expectedRC, rc);
        assertTrue(bc.getLength() > 0);
    } else {
        assertEquals(200, rc);
        assertEquals("OK", bc.toString());
        saveCookies(respHeaders);
    }
}
Also used : HashMap(java.util.HashMap) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ArrayList(java.util.ArrayList) List(java.util.List) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 14 with ByteChunk

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

the class TestSSOnonLoginAndDigestAuthenticator method doTestDigest.

public void doTestDigest(String user, String pwd, String uri, boolean expectedReject1, int expectedRC1, boolean useServerNonce, boolean useServerOpaque, String nc1, String cnonce, String qop, boolean req2expect200) throws Exception {
    String digestUri = uri;
    List<String> auth = new ArrayList<>();
    Map<String, List<String>> reqHeaders1 = new HashMap<>();
    Map<String, List<String>> respHeaders1 = new HashMap<>();
    // the first access attempt should be challenged
    auth.add(buildDigestResponse(user, pwd, digestUri, REALM, "null", "null", nc1, cnonce, qop));
    reqHeaders1.put(CLIENT_AUTH_HEADER, auth);
    ByteChunk bc = new ByteChunk();
    int rc = getUrl(HTTP_PREFIX + getPort() + uri, bc, reqHeaders1, respHeaders1);
    if (expectedReject1) {
        assertEquals(expectedRC1, rc);
        assertTrue(bc.getLength() > 0);
    } else {
        assertEquals(200, rc);
        assertEquals("OK", bc.toString());
        saveCookies(respHeaders1);
        return;
    }
    // Second request should succeed (if we use the server nonce)
    Map<String, List<String>> reqHeaders2 = new HashMap<>();
    Map<String, List<String>> respHeaders2 = new HashMap<>();
    auth.clear();
    if (useServerNonce) {
        if (useServerOpaque) {
            auth.add(buildDigestResponse(user, pwd, digestUri, getAuthToken(respHeaders1, REALM), getAuthToken(respHeaders1, NONCE), getAuthToken(respHeaders1, OPAQUE), nc1, cnonce, qop));
        } else {
            auth.add(buildDigestResponse(user, pwd, digestUri, getAuthToken(respHeaders1, REALM), getAuthToken(respHeaders1, NONCE), "null", nc1, cnonce, qop));
        }
    } else {
        auth.add(buildDigestResponse(user, pwd, digestUri, getAuthToken(respHeaders2, REALM), "null", getAuthToken(respHeaders1, OPAQUE), nc1, cnonce, QOP));
    }
    reqHeaders2.put(CLIENT_AUTH_HEADER, auth);
    bc.recycle();
    rc = getUrl(HTTP_PREFIX + getPort() + uri, bc, reqHeaders2, respHeaders2);
    if (req2expect200) {
        assertEquals(200, rc);
        assertEquals("OK", bc.toString());
        saveCookies(respHeaders2);
    } else {
        assertEquals(401, rc);
        assertTrue((bc.getLength() > 0));
    }
}
Also used : HashMap(java.util.HashMap) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 15 with ByteChunk

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

the class TestConnector method doTestTrace.

private void doTestTrace(Servlet servlet, boolean allowTrace) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp");
    Context root = tomcat.addContext("", appDir.getAbsolutePath());
    Tomcat.addServlet(root, "default", servlet);
    root.addServletMappingDecoded("/", "default");
    Connector connector = tomcat.getConnector();
    connector.setAllowTrace(allowTrace);
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    Map<String, List<String>> respHeaders = new HashMap<>();
    int rc = methodUrl("http://localhost:" + getPort() + "/index.html", bc, 30000, null, respHeaders, "OPTIONS");
    assertEquals(200, rc);
    boolean foundTrace = false;
    for (String header : respHeaders.get("Allow")) {
        if (header.contains("TRACE")) {
            foundTrace = true;
            break;
        }
    }
    if (allowTrace) {
        Assert.assertTrue(foundTrace);
    } else {
        Assert.assertFalse(foundTrace);
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) List(java.util.List) File(java.io.File)

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