Search in sources :

Example 91 with ByteChunk

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

the class TestRewriteValve method doTestRewrite.

private void doTestRewrite(String config, String request, String expectedURI, String expectedQueryString, String expectedAttributeValue) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    RewriteValve rewriteValve = new RewriteValve();
    ctx.getPipeline().addValve(rewriteValve);
    rewriteValve.setConfiguration(config);
    Tomcat.addServlet(ctx, "snoop", new SnoopServlet());
    ctx.addServletMappingDecoded("/a/%5A", "snoop");
    ctx.addServletMappingDecoded("/c/*", "snoop");
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    ByteChunk res = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + request, res, null);
    res.setCharset(StandardCharsets.UTF_8);
    if (expectedURI == null) {
        // Rewrite is expected to fail. Probably because invalid characters
        // were written into the request target
        Assert.assertEquals(400, rc);
    } else {
        String body = res.toString();
        RequestDescriptor requestDesc = SnoopResult.parse(body);
        String requestURI = requestDesc.getRequestInfo("REQUEST-URI");
        Assert.assertEquals(expectedURI, requestURI);
        if (expectedQueryString != null) {
            String queryString = requestDesc.getRequestInfo("REQUEST-QUERY-STRING");
            Assert.assertEquals(expectedQueryString, queryString);
        }
        if (expectedAttributeValue != null) {
            String attributeValue = requestDesc.getAttribute("X-Test");
            Assert.assertEquals(expectedAttributeValue, attributeValue);
        }
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) DefaultServlet(org.apache.catalina.servlets.DefaultServlet)

Example 92 with ByteChunk

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

the class TestFileResource method testGetCodePath.

@Test
public void testGetCodePath() throws Exception {
    getTomcatInstanceTestWebapp(false, true);
    ByteChunk out = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug58096.jsp", out, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    // Build the expected location the same way the webapp base dir is built
    File f = new File("test/webapp/WEB-INF/classes");
    Assert.assertEquals(f.toURI().toURL().toString(), out.toString().trim());
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 93 with ByteChunk

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

the class TestValidator method testTldVersions31.

@Test
public void testTldVersions31() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-3.1");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/tld-versions.jsp");
    String result = res.toString();
    assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
    assertTrue(result.indexOf("<p>#{'01-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>02-hello world</p>") > 0);
    assertTrue(result.indexOf("<p>#{'03-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>04-hello world</p>") > 0);
    assertTrue(result.indexOf("<p>#{'05-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>06-hello world</p>") > 0);
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 94 with ByteChunk

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

the class TestValidator method testTldVersions30.

@Test
public void testTldVersions30() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-3.0");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/tld-versions.jsp");
    String result = res.toString();
    assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
    assertTrue(result.indexOf("<p>#{'01-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>02-hello world</p>") > 0);
    assertTrue(result.indexOf("<p>#{'03-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>04-hello world</p>") > 0);
    assertTrue(result.indexOf("<p>#{'05-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>06-hello world</p>") > 0);
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 95 with ByteChunk

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

the class TestValidator method testTldVersions22.

@Test
public void testTldVersions22() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-2.2");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/tld-versions.jsp");
    String result = res.toString();
    assertTrue(result.indexOf("<p>${'00-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>#{'01-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>${'02-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>#{'03-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>${'04-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>#{'05-hello world'}</p>") > 0);
    assertTrue(result.indexOf("<p>${'06-hello world'}</p>") > 0);
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) 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