Search in sources :

Example 61 with Context

use of org.apache.catalina.Context 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 62 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestHttp11Processor method testResponseWithErrorChunked.

@Test
public void testResponseWithErrorChunked() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // This setting means the connection will be closed at the end of the
    // request
    tomcat.getConnector().setAttribute("maxKeepAliveRequests", "1");
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add protected servlet
    Tomcat.addServlet(ctx, "ChunkedResponseWithErrorServlet", new ResponseWithErrorServlet(true));
    ctx.addServletMappingDecoded("/*", "ChunkedResponseWithErrorServlet");
    tomcat.start();
    String request = "GET /anything HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] { request });
    client.connect();
    client.processRequest();
    // Expected response is a 200 response followed by an incomplete chunked
    // body.
    assertTrue(client.isResponse200());
    // Should use chunked encoding
    String transferEncoding = null;
    for (String header : client.getResponseHeaders()) {
        if (header.startsWith("Transfer-Encoding:")) {
            transferEncoding = header.substring(18).trim();
        }
    }
    Assert.assertEquals("chunked", transferEncoding);
    // There should not be an end chunk
    assertFalse(client.getResponseBody().endsWith("0"));
    // The last portion of text should be there
    assertTrue(client.getResponseBody().endsWith("line03"));
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) SimpleHttpClient(org.apache.catalina.startup.SimpleHttpClient) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 63 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestChunkedInputFilter method testNoTrailingHeaders.

@Test
public void testNoTrailingHeaders() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(true));
    ctx.addServletMappingDecoded("/", "servlet");
    tomcat.start();
    String request = "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + SimpleHttpClient.CRLF + "a=0" + SimpleHttpClient.CRLF + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
    TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] { request });
    client.connect();
    client.processRequest();
    assertEquals("nullnull7nullnull", client.getResponseBody());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 64 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestChunkedInputFilter method doTestChunkingCRLF.

private void doTestChunkingCRLF(boolean chunkHeaderUsesCRLF, boolean chunkUsesCRLF, boolean firstheaderUsesCRLF, boolean secondheaderUsesCRLF, boolean endUsesCRLF, boolean expectPass) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Configure allowed trailer headers
    tomcat.getConnector().setProperty("allowedTrailerHeaders", "X-Trailer1,X-Trailer2");
    EchoHeaderServlet servlet = new EchoHeaderServlet(expectPass);
    Tomcat.addServlet(ctx, "servlet", servlet);
    ctx.addServletMappingDecoded("/", "servlet");
    tomcat.start();
    String[] request = new String[] { "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + (chunkHeaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + "a=0" + (chunkUsesCRLF ? SimpleHttpClient.CRLF : LF) + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + "x-trailer1: Test", "Value1" + (firstheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + "x-trailer2: TestValue2" + (secondheaderUsesCRLF ? SimpleHttpClient.CRLF : LF) + (endUsesCRLF ? SimpleHttpClient.CRLF : LF) };
    TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
    client.setRequest(request);
    client.connect();
    Exception processException = null;
    try {
        client.processRequest();
    } catch (Exception e) {
        // Socket was probably closed before client had a chance to read
        // response
        processException = e;
    }
    if (expectPass) {
        assertTrue(client.isResponse200());
        assertEquals("nullnull7TestValue1TestValue2", client.getResponseBody());
        assertNull(processException);
        assertFalse(servlet.getExceptionDuringRead());
    } else {
        if (processException == null) {
            assertTrue(client.getResponseLine(), client.isResponse500());
        } else {
            // Use fall-back for checking the error occurred
            assertTrue(servlet.getExceptionDuringRead());
        }
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 65 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestChunkedInputFilter method doTestExtensionSizeLimit.

private void doTestExtensionSizeLimit(int len, boolean ok) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    tomcat.getConnector().setProperty("maxExtensionSize", Integer.toString(EXT_SIZE_LIMIT));
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "servlet", new EchoHeaderServlet(ok));
    ctx.addServletMappingDecoded("/", "servlet");
    tomcat.start();
    String extName = ";foo=";
    StringBuilder extValue = new StringBuilder(len);
    for (int i = 0; i < (len - extName.length()); i++) {
        extValue.append("x");
    }
    String[] request = new String[] { "POST /echo-params.jsp HTTP/1.1" + SimpleHttpClient.CRLF + "Host: any" + SimpleHttpClient.CRLF + "Transfer-encoding: chunked" + SimpleHttpClient.CRLF + "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient.CRLF + "Connection: close" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF + "3" + extName + extValue.toString() + SimpleHttpClient.CRLF + "a=0" + SimpleHttpClient.CRLF + "4" + SimpleHttpClient.CRLF + "&b=1" + SimpleHttpClient.CRLF + "0" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF };
    TrailerClient client = new TrailerClient(tomcat.getConnector().getLocalPort());
    client.setRequest(request);
    client.connect();
    client.processRequest();
    if (ok) {
        assertTrue(client.isResponse200());
    } else {
        assertTrue(client.isResponse500());
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat)

Aggregations

Context (org.apache.catalina.Context)376 Tomcat (org.apache.catalina.startup.Tomcat)212 Test (org.junit.Test)180 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)127 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)96 File (java.io.File)77 ServletContext (javax.servlet.ServletContext)74 AsyncContext (javax.servlet.AsyncContext)73 StandardContext (org.apache.catalina.core.StandardContext)65 Wrapper (org.apache.catalina.Wrapper)53 IOException (java.io.IOException)40 TesterContext (org.apache.tomcat.unittest.TesterContext)39 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 URI (java.net.URI)33 WebSocketContainer (javax.websocket.WebSocketContainer)32 Session (javax.websocket.Session)31 Host (org.apache.catalina.Host)30 Container (org.apache.catalina.Container)26 ArrayList (java.util.ArrayList)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24