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);
}
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);
}
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);
}
}
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));
}
}
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);
}
}
Aggregations