use of org.apache.catalina.Context 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());
}
use of org.apache.catalina.Context 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);
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestAsyncContextImpl method prepareApplicationWithGenericServlet.
private void prepareApplicationWithGenericServlet(String contextPath) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext(contextPath, null);
DispatchingGenericServlet dispatch = new DispatchingGenericServlet();
Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
wrapper.setAsyncSupported(true);
ctx.addServletMappingDecoded("/dispatch", "dispatch");
CustomGenericServlet customGeneric = new CustomGenericServlet();
Wrapper wrapper2 = Tomcat.addServlet(ctx, "customGeneric", customGeneric);
wrapper2.setAsyncSupported(true);
ctx.addServletMappingDecoded("/target", "customGeneric");
tomcat.start();
}
use of org.apache.catalina.Context 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());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestAsyncContextImpl method testForbiddenDispatching.
@Test
public void testForbiddenDispatching() throws Exception {
resetTracker();
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
NonAsyncServlet nonAsyncServlet = new NonAsyncServlet();
Wrapper wrapper = Tomcat.addServlet(ctx, "nonAsyncServlet", nonAsyncServlet);
wrapper.setAsyncSupported(true);
ctx.addServletMappingDecoded("/target", "nonAsyncServlet");
DispatchingGenericServlet forbiddenDispatchingServlet = new DispatchingGenericServlet();
Wrapper wrapper1 = Tomcat.addServlet(ctx, "forbiddenDispatchingServlet", forbiddenDispatchingServlet);
wrapper1.setAsyncSupported(true);
ctx.addServletMappingDecoded("/forbiddenDispatchingServlet", "forbiddenDispatchingServlet");
tomcat.start();
try {
getUrl("http://localhost:" + getPort() + "/forbiddenDispatchingServlet");
} 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();
}
// Request may complete before listener has finished processing so wait
// up to 5 seconds for the right response
String expectedTrack = "OKNonAsyncServletGet-";
int count = 0;
while (!expectedTrack.equals(getTrack()) && count < 100) {
Thread.sleep(50);
count++;
}
Assert.assertEquals(expectedTrack, getTrack());
}
Aggregations