Search in sources :

Example 36 with ByteChunk

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

the class TestTomcatClassLoader method testDefaultClassLoader.

@Test
public void testDefaultClassLoader() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "ClassLoaderReport", new ClassLoaderReport(null));
    ctx.addServletMappingDecoded("/", "ClassLoaderReport");
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    assertEquals("WEBAPP,SYSTEM,OTHER,", res.toString());
}
Also used : Context(org.apache.catalina.Context) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) Test(org.junit.Test)

Example 37 with ByteChunk

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

the class TomcatBaseTest method getUrl.

/*
     *  Wrapper for getting the response.
     */
public static ByteChunk getUrl(String path) throws IOException {
    ByteChunk out = new ByteChunk();
    getUrl(path, out, null);
    return out;
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 38 with ByteChunk

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

the class TestDefaultServlet method testGetWithSubpathmount.

/*
     * Test https://bz.apache.org/bugzilla/show_bug.cgi?id=50026
     * Verify serving of resources from context root with subpath mapping.
     */
@Test
public void testGetWithSubpathmount() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    String contextPath = "/examples";
    File appDir = new File(getBuildDirectory(), "webapps" + contextPath);
    // app dir is relative to server home
    org.apache.catalina.Context ctx = tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
    ctx.addApplicationListener(WsContextListener.class.getName());
    // Override the default servlet with our own mappings
    Tomcat.addServlet(ctx, "default2", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default2");
    ctx.addServletMappingDecoded("/servlets/*", "default2");
    ctx.addServletMappingDecoded("/static/*", "default2");
    tomcat.start();
    final ByteChunk res = new ByteChunk();
    // Make sure DefaultServlet isn't exposing special directories
    // by remounting the webapp under a sub-path
    int rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/WEB-INF/web.xml", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/WEB-INF/doesntexistanywhere", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/WEB-INF/", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/META-INF/MANIFEST.MF", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/META-INF/doesntexistanywhere", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
    // Make sure DefaultServlet is serving resources relative to the
    // context root regardless of where the it is mapped
    final ByteChunk rootResource = new ByteChunk();
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/index.html", rootResource, null);
    assertEquals(HttpServletResponse.SC_OK, rc);
    final ByteChunk subpathResource = new ByteChunk();
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/servlets/index.html", subpathResource, null);
    assertEquals(HttpServletResponse.SC_OK, rc);
    assertFalse(rootResource.toString().equals(subpathResource.toString()));
    rc = getUrl("http://localhost:" + getPort() + contextPath + "/static/index.html", res, null);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) WsContextListener(org.apache.tomcat.websocket.server.WsContextListener) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) Context(org.apache.catalina.Context) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 39 with ByteChunk

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

the class TestTomcat method testSingleWebapp.

@Test
public void testSingleWebapp() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File(getBuildDirectory(), "webapps/examples");
    // app dir is relative to server home
    Context ctxt = tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());
    ctxt.addApplicationListener(WsContextListener.class.getName());
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/examples/servlets/servlet/HelloWorldExample");
    String text = res.toString();
    assertTrue(text, text.indexOf("<a href=\"../helloworld.html\">") > 0);
}
Also used : ReplicatedContext(org.apache.catalina.ha.context.ReplicatedContext) InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) WsContextListener(org.apache.tomcat.websocket.server.WsContextListener) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) Test(org.junit.Test)

Example 40 with ByteChunk

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

the class TestTomcat method testEnableNamingGlobal.

/*
     * Test for enabling JNDI and using global resources.
     */
@Test
public void testEnableNamingGlobal() 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 environment = new ContextEnvironment();
    environment.setType("java.lang.String");
    environment.setName("globalTest");
    environment.setValue("Tomcat User");
    tomcat.getServer().getGlobalNamingResources().addEnvironment(environment);
    ContextResourceLink link = new ContextResourceLink();
    link.setGlobal("globalTest");
    link.setName(HelloWorldJndi.JNDI_ENV_NAME);
    link.setType("java.lang.String");
    ctx.getNamingResources().addResourceLink(link);
    Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi());
    ctx.addServletMappingDecoded("/", "jndiServlet");
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    assertEquals("Hello, Tomcat User", res.toString());
}
Also used : ReplicatedContext(org.apache.catalina.ha.context.ReplicatedContext) InitialContext(javax.naming.InitialContext) Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) Test(org.junit.Test)

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