use of org.apache.catalina.Context 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.catalina.Context in project tomcat by apache.
the class TestNamingContextListener method testBug54096.
@Test
public void testBug54096() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Enable JNDI - it is disabled by default
tomcat.enableNaming();
ContextEnvironment environmentA = new ContextEnvironment();
environmentA.setType(Bug54096EnvA.class.getName());
environmentA.setName(BUG54096_NameA);
environmentA.setValue(BUG54096_ValueA);
ctx.getNamingResources().addEnvironment(environmentA);
ContextEnvironment environmentB = new ContextEnvironment();
environmentB.setType(Bug54096EnvB.class.getName());
environmentB.setName(BUG54096_NameB);
environmentB.setValue(BUG54096_ValueB);
ctx.getNamingResources().addEnvironment(environmentB);
ctx.addApplicationListener(Bug54096Listener.class.getName());
tomcat.start();
assertEquals(LifecycleState.STARTED, ctx.getState());
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestSSOnonLoginAndDigestAuthenticator method setUpDigest.
private void setUpDigest(Tomcat tomcat) throws Exception {
// Must have a real docBase for webapps - just use temp
Context ctxt = tomcat.addContext(CONTEXT_PATH_DIGEST, System.getProperty("java.io.tmpdir"));
ctxt.setSessionTimeout(SHORT_TIMEOUT_SECS);
// Add protected servlet
Tomcat.addServlet(ctxt, "TesterServlet3", new TesterServlet());
ctxt.addServletMappingDecoded(URI_PROTECTED, "TesterServlet3");
SecurityCollection collection = new SecurityCollection();
collection.addPatternDecoded(URI_PROTECTED);
SecurityConstraint sc = new SecurityConstraint();
sc.addAuthRole(ROLE);
sc.addCollection(collection);
ctxt.addConstraint(sc);
// Configure the appropriate authenticator
LoginConfig lc = new LoginConfig();
lc.setAuthMethod("DIGEST");
ctxt.setLoginConfig(lc);
ctxt.getPipeline().addValve(new DigestAuthenticator());
}
use of org.apache.catalina.Context 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);
}
}
use of org.apache.catalina.Context in project tomcat by apache.
the class TestCoyoteAdapter method testPathParamsRedirect.
@Test
public void testPathParamsRedirect() throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// Must have a real docBase. Don't use java.io.tmpdir as it may not be
// writable.
File docBase = new File(getTemporaryDirectory(), "testCoyoteAdapter");
addDeleteOnTearDown(docBase);
if (!docBase.mkdirs() && !docBase.isDirectory()) {
Assert.fail("Failed to create: [" + docBase.toString() + "]");
}
// Create the folder that will trigger the redirect
File foo = new File(docBase, "foo");
addDeleteOnTearDown(foo);
if (!foo.mkdirs() && !foo.isDirectory()) {
Assert.fail("Unable to create foo directory in docBase");
}
Context ctx = tomcat.addContext("", docBase.getAbsolutePath());
Tomcat.addServlet(ctx, "servlet", new PathParamServlet());
ctx.addServletMappingDecoded("/", "servlet");
tomcat.start();
testPath("/", "none");
testPath("/;jsessionid=1234", "1234");
testPath("/foo;jsessionid=1234", "1234");
testPath("/foo;jsessionid=1234;dummy", "1234");
testPath("/foo;jsessionid=1234;dummy=5678", "1234");
testPath("/foo;jsessionid=1234;=5678", "1234");
testPath("/foo;jsessionid=1234/bar", "1234");
}
Aggregations