Search in sources :

Example 16 with Tomcat

use of org.apache.catalina.startup.Tomcat in project tomcat by apache.

the class TestCoyoteAdapter method pathParamExtensionTest.

private void pathParamExtensionTest(String path, String expected) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("/testapp", null);
    Tomcat.addServlet(ctx, "servlet", new PathParamServlet());
    ctx.addServletMappingDecoded("*.txt", "servlet");
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + path);
    Assert.assertEquals(expected, res.toString());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 17 with Tomcat

use of org.apache.catalina.startup.Tomcat in project tomcat by apache.

the class TestCoyoteAdapter method doTestUriDecoding.

private void doTestUriDecoding(String path, String encoding, String expectedPathInfo) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    tomcat.getConnector().setURIEncoding(encoding);
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    PathInfoServlet servlet = new PathInfoServlet();
    Tomcat.addServlet(ctx, "servlet", servlet);
    ctx.addServletMappingDecoded("/*", "servlet");
    tomcat.start();
    int rc = getUrl("http://localhost:" + getPort() + path, new ByteChunk(), null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    Assert.assertEquals(expectedPathInfo, servlet.getPathInfo());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 18 with Tomcat

use of org.apache.catalina.startup.Tomcat in project tomcat by apache.

the class TestApplicationContext method testJspPropertyGroupsAreIsolated.

@Test
public void testJspPropertyGroupsAreIsolated() throws Exception {
    Tomcat tomcat = getTomcatInstanceTestWebapp(false, false);
    StandardContext standardContext = (StandardContext) tomcat.getHost().findChildren()[0];
    ServletContext servletContext = standardContext.getServletContext();
    Assert.assertNull(servletContext.getJspConfigDescriptor());
    tomcat.start();
    JspConfigDescriptor jspConfigDescriptor = servletContext.getJspConfigDescriptor();
    Collection<JspPropertyGroupDescriptor> propertyGroups = jspConfigDescriptor.getJspPropertyGroups();
    Assert.assertFalse(propertyGroups.isEmpty());
    propertyGroups.clear();
    jspConfigDescriptor = servletContext.getJspConfigDescriptor();
    propertyGroups = jspConfigDescriptor.getJspPropertyGroups();
    Assert.assertFalse(propertyGroups.isEmpty());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ServletContext(javax.servlet.ServletContext) JspConfigDescriptor(javax.servlet.descriptor.JspConfigDescriptor) JspPropertyGroupDescriptor(javax.servlet.descriptor.JspPropertyGroupDescriptor) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 19 with Tomcat

use of org.apache.catalina.startup.Tomcat in project tomcat by apache.

the class TestApplicationContextGetRequestDispatcher method doTestGetRequestDispatcher.

private void doTestGetRequestDispatcher(boolean useEncodedDispatchPaths, String startPath, String startQueryString, String dispatchPath, String targetPath, String expectedBody) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("/test", null);
    ctx.setDispatchersUseEncodedPaths(useEncodedDispatchPaths);
    // Add a default servlet to return 404 for not found resources
    Tomcat.addServlet(ctx, "Default", new Default404Servlet());
    ctx.addServletMappingDecoded("/*", "Default");
    // Add a target servlet to dispatch to
    Tomcat.addServlet(ctx, "target", new TargetServlet());
    ctx.addServletMappingDecoded(UDecoder.URLDecode(targetPath, "UTF-8"), "target");
    if (useAsync) {
        Wrapper w = Tomcat.addServlet(ctx, "rd", new AsyncDispatcherServlet(dispatchPath, useEncodedDispatchPaths));
        w.setAsyncSupported(true);
    } else {
        Tomcat.addServlet(ctx, "rd", new DispatcherServlet(dispatchPath));
    }
    ctx.addServletMappingDecoded(UDecoder.URLDecode(startPath, "UTF-8"), "rd");
    tomcat.start();
    StringBuilder url = new StringBuilder("http://localhost:");
    url.append(getPort());
    url.append("/test");
    url.append(startPath);
    if (startQueryString != null) {
        url.append('?');
        url.append(startQueryString);
    }
    ByteChunk bc = getUrl(url.toString());
    String body = bc.toString();
    Assert.assertEquals(expectedBody, body);
}
Also used : Context(org.apache.catalina.Context) AsyncContext(javax.servlet.AsyncContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 20 with Tomcat

use of org.apache.catalina.startup.Tomcat in project tomcat by apache.

the class TestApplicationHttpRequest method testParameterImmutability.

@Test
public void testParameterImmutability() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "forward", new ForwardServlet("/modify"));
    ctx.addServletMappingDecoded("/forward", "forward");
    Tomcat.addServlet(ctx, "modify", new ModifyParameterServlet());
    ctx.addServletMappingDecoded("/modify", "modify");
    tomcat.start();
    ByteChunk response = new ByteChunk();
    StringBuilder target = new StringBuilder("http://localhost:");
    target.append(getPort());
    target.append("/forward");
    int rc = getUrl(target.toString(), response, null);
    Assert.assertEquals(200, rc);
    Assert.assertEquals("OK", response.toString());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

Tomcat (org.apache.catalina.startup.Tomcat)316 Test (org.junit.Test)222 Context (org.apache.catalina.Context)215 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)186 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)129 File (java.io.File)91 AsyncContext (javax.servlet.AsyncContext)61 Wrapper (org.apache.catalina.Wrapper)46 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 WebSocketContainer (javax.websocket.WebSocketContainer)32 TesterContext (org.apache.tomcat.unittest.TesterContext)32 URI (java.net.URI)31 Session (javax.websocket.Session)31 List (java.util.List)29 ServletContext (javax.servlet.ServletContext)29 StandardContext (org.apache.catalina.core.StandardContext)27 HashMap (java.util.HashMap)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)24 ArrayList (java.util.ArrayList)19