use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestWebappClassLoaderThreadLocalMemoryLeak method testThreadLocalLeak1.
@Test
public void testThreadLocalLeak1() throws Exception {
Tomcat tomcat = getTomcatInstance();
// Need to make sure we see a leak for the right reasons
tomcat.getServer().addLifecycleListener(new JreMemoryLeakPreventionListener());
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "leakServlet1", "org.apache.tomcat.unittest.TesterLeakingServlet1");
ctx.addServletMappingDecoded("/leak1", "leakServlet1");
tomcat.start();
Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);
// Configure logging filter to check leak message appears
LogValidationFilter f = new LogValidationFilter("The web application [ROOT] created a ThreadLocal with key of");
LogManager.getLogManager().getLogger("org.apache.catalina.loader.WebappClassLoaderBase").setFilter(f);
// Need to force loading of all web application classes via the web
// application class loader
loadClass("TesterCounter", (WebappClassLoader) ctx.getLoader().getClassLoader());
loadClass("TesterLeakingServlet1", (WebappClassLoader) ctx.getLoader().getClassLoader());
// This will trigger the ThreadLocal creation
int rc = getUrl("http://localhost:" + getPort() + "/leak1", new ByteChunk(), null);
// Make sure request is OK
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
// Destroy the context
ctx.stop();
tomcat.getHost().removeChild(ctx);
ctx = null;
// Make sure we have a memory leak
String[] leaks = ((StandardHost) tomcat.getHost()).findReloadedContextMemoryLeaks();
Assert.assertNotNull(leaks);
Assert.assertTrue(leaks.length > 0);
// Make sure the message was logged
Assert.assertEquals(1, f.getMessageCount());
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestAddCharSetFilter method doTest.
private void doTest(String encoding, String expected, int mode) throws Exception {
// Setup Tomcat instance
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
// Add the Servlet
CharsetServlet servlet = new CharsetServlet(mode);
Tomcat.addServlet(ctx, "servlet", servlet);
ctx.addServletMappingDecoded("/", "servlet");
// Add the Filter
FilterDef filterDef = new FilterDef();
filterDef.setFilterClass(AddDefaultCharsetFilter.class.getName());
filterDef.setFilterName("filter");
if (encoding != null) {
filterDef.addInitParameter("encoding", encoding);
}
ctx.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setFilterName("filter");
filterMap.addServletName("servlet");
ctx.addFilterMap(filterMap);
tomcat.start();
Map<String, List<String>> headers = new HashMap<>();
getUrl("http://localhost:" + getPort() + "/", new ByteChunk(), headers);
List<String> ctHeaders = headers.get("Content-Type");
assertEquals(1, ctHeaders.size());
String ct = ctHeaders.get(0);
assertEquals("text/plain;charset=" + expected, ct);
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestErrorReportValve method testBug54220DoNotSetNotFound.
@Test
public void testBug54220DoNotSetNotFound() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "bug54220", new Bug54220Servlet(false));
ctx.addServletMappingDecoded("/", "bug54220");
tomcat.start();
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort(), res, null);
Assert.assertNull(res.toString());
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestErrorReportValve method testBug54220SetNotFound.
@Test
public void testBug54220SetNotFound() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "bug54220", new Bug54220Servlet(true));
ctx.addServletMappingDecoded("/", "bug54220");
tomcat.start();
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort(), res, null);
Assert.assertNull(res.toString());
Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
}
use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.
the class TestErrorReportValve method testBug54536.
/*
* Custom error/status codes should not result in a blank response.
*/
@Test
public void testBug54536() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "bug54536", new Bug54536Servlet());
ctx.addServletMappingDecoded("/", "bug54536");
tomcat.start();
ByteChunk res = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort(), res, null);
Assert.assertEquals(Bug54536Servlet.ERROR_STATUS, rc);
String body = res.toString();
Assert.assertNotNull(body);
Assert.assertTrue(body, body.contains(Bug54536Servlet.ERROR_MESSAGE));
}
Aggregations