Search in sources :

Example 41 with ByteChunk

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

the class TestTomcat method testEnableNaming.

/*
     * Test for enabling JNDI.
     */
@Test
public void testEnableNaming() 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(HelloWorldJndi.JNDI_ENV_NAME);
    environment.setValue("Tomcat User");
    ctx.getNamingResources().addEnvironment(environment);
    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) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) Test(org.junit.Test)

Example 42 with ByteChunk

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

the class TestHttp11Processor method doTestNon2xxResponseAndExpectation.

private void doTestNon2xxResponseAndExpectation(boolean useExpectation) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "echo", new EchoBodyServlet());
    ctx.addServletMappingDecoded("/echo", "echo");
    SecurityCollection collection = new SecurityCollection("All", "");
    collection.addPatternDecoded("/*");
    SecurityConstraint constraint = new SecurityConstraint();
    constraint.addAuthRole("Any");
    constraint.addCollection(collection);
    ctx.addConstraint(constraint);
    tomcat.start();
    byte[] requestBody = "HelloWorld".getBytes(StandardCharsets.UTF_8);
    Map<String, List<String>> reqHeaders = null;
    if (useExpectation) {
        reqHeaders = new HashMap<>();
        List<String> expectation = new ArrayList<>();
        expectation.add("100-continue");
        reqHeaders.put("Expect", expectation);
    }
    ByteChunk responseBody = new ByteChunk();
    Map<String, List<String>> responseHeaders = new HashMap<>();
    int rc = postUrl(requestBody, "http://localhost:" + getPort() + "/echo", responseBody, reqHeaders, responseHeaders);
    Assert.assertEquals(HttpServletResponse.SC_FORBIDDEN, rc);
    List<String> connectionHeaders = responseHeaders.get("Connection");
    if (useExpectation) {
        Assert.assertEquals(1, connectionHeaders.size());
        Assert.assertEquals("close", connectionHeaders.get(0).toLowerCase(Locale.ENGLISH));
    } else {
        Assert.assertNull(connectionHeaders);
    }
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) ArrayList(java.util.ArrayList) List(java.util.List) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection)

Example 43 with ByteChunk

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

the class TestHttp11Processor method doTestBug53677.

private void doTestBug53677(boolean flush) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "LargeHeaderServlet", new LargeHeaderServlet(flush));
    ctx.addServletMappingDecoded("/test", "LargeHeaderServlet");
    tomcat.start();
    ByteChunk responseBody = new ByteChunk();
    Map<String, List<String>> responseHeaders = new HashMap<>();
    int rc = getUrl("http://localhost:" + getPort() + "/test", responseBody, responseHeaders);
    assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
    if (responseBody.getLength() > 0) {
        // It will be >0 if the standard error page handling has been
        // triggered
        assertFalse(responseBody.toString().contains("FAIL"));
    }
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 44 with ByteChunk

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

the class TestHttp11Processor method testBug59310.

@Test
public void testBug59310() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "Bug59310", new Bug59310Servlet());
    ctx.addServletMappingDecoded("/test", "Bug59310");
    tomcat.start();
    ByteChunk responseBody = new ByteChunk();
    Map<String, List<String>> responseHeaders = new HashMap<>();
    int rc = headUrl("http://localhost:" + getPort() + "/test", responseBody, responseHeaders);
    assertEquals(HttpServletResponse.SC_OK, rc);
    assertEquals(0, responseBody.getLength());
    assertFalse(responseHeaders.containsKey("Content-Length"));
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 45 with ByteChunk

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

the class TestHttp11Processor method testNoChunking11NoContentLengthConnectionClose.

@Test
public void testNoChunking11NoContentLengthConnectionClose() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "NoContentLengthConnectionCloseFlushingServlet", new NoContentLengthConnectionCloseFlushingServlet());
    ctx.addServletMappingDecoded("/test", "NoContentLengthConnectionCloseFlushingServlet");
    tomcat.start();
    ByteChunk responseBody = new ByteChunk();
    Map<String, List<String>> responseHeaders = new HashMap<>();
    int rc = getUrl("http://localhost:" + getPort() + "/test", responseBody, responseHeaders);
    assertEquals(HttpServletResponse.SC_OK, rc);
    assertTrue(responseHeaders.containsKey("Connection"));
    List<String> connections = responseHeaders.get("Connection");
    assertEquals(1, connections.size());
    assertEquals("close", connections.get(0));
    assertFalse(responseHeaders.containsKey("Transfer-Encoding"));
    assertEquals("OK", responseBody.toString());
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) 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