Search in sources :

Example 11 with TesterServlet

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

the class TestHttp11Processor method testInconsistentHostHeader02.

@Test
public void testInconsistentHostHeader02() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // This setting means the connection will be closed at the end of the
    // request
    Assert.assertTrue(tomcat.getConnector().setProperty("maxKeepAliveRequests", "1"));
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add servlet
    Tomcat.addServlet(ctx, "TesterServlet", new TesterServlet());
    ctx.addServletMappingDecoded("/foo", "TesterServlet");
    tomcat.start();
    String request = "GET http://a:8080/foo HTTP/1.1" + SimpleHttpClient.CRLF + "Host: b:8080" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] { request });
    client.connect();
    client.processRequest();
    // Expected response is a 400 response.
    Assert.assertTrue(client.isResponse400());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(jakarta.servlet.AsyncContext) Tomcat(org.apache.catalina.startup.Tomcat) TesterServlet(org.apache.catalina.startup.TesterServlet) SimpleHttpClient(org.apache.catalina.startup.SimpleHttpClient) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 12 with TesterServlet

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

the class TestHttp11Processor method testInconsistentHostHeader06.

/*
     * Hostname (with port and user) is included in the request line, but Host
     * header is empty.
     * Added for bug 62739.
     */
@Test
public void testInconsistentHostHeader06() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // This setting means the connection will be closed at the end of the
    // request
    Assert.assertTrue(tomcat.getConnector().setProperty("maxKeepAliveRequests", "1"));
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add servlet
    Tomcat.addServlet(ctx, "TesterServlet", new TesterServlet());
    ctx.addServletMappingDecoded("/foo", "TesterServlet");
    tomcat.start();
    String request = "GET http://user:pwd@a/foo HTTP/1.1" + SimpleHttpClient.CRLF + "Host: " + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] { request });
    client.connect();
    client.processRequest();
    // Expected response is a 400 response.
    Assert.assertTrue(client.isResponse400());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(jakarta.servlet.AsyncContext) Tomcat(org.apache.catalina.startup.Tomcat) TesterServlet(org.apache.catalina.startup.TesterServlet) SimpleHttpClient(org.apache.catalina.startup.SimpleHttpClient) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 13 with TesterServlet

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

the class TestHttp11Processor method testMissingHostHeader.

@Test
public void testMissingHostHeader() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // This setting means the connection will be closed at the end of the
    // request
    Assert.assertTrue(tomcat.getConnector().setProperty("maxKeepAliveRequests", "1"));
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add servlet
    Tomcat.addServlet(ctx, "TesterServlet", new TesterServlet());
    ctx.addServletMappingDecoded("/foo", "TesterServlet");
    tomcat.start();
    String request = "GET /foo HTTP/1.1" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
    Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] { request });
    client.connect();
    client.processRequest();
    // Expected response is a 400 response.
    Assert.assertTrue(client.isResponse400());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(jakarta.servlet.AsyncContext) Tomcat(org.apache.catalina.startup.Tomcat) TesterServlet(org.apache.catalina.startup.TesterServlet) SimpleHttpClient(org.apache.catalina.startup.SimpleHttpClient) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 14 with TesterServlet

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

the class TestSsl method testRenegotiateWorks.

@Test
public void testRenegotiateWorks() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    TesterSupport.initSsl(tomcat);
    TesterSupport.configureSSLImplementation(tomcat, sslImplementationName);
    Assume.assumeTrue("SSL renegotiation has to be supported for this test", TesterSupport.isClientRenegotiationSupported(getTomcatInstance()));
    if (needApr) {
        AprLifecycleListener listener = new AprLifecycleListener();
        Assume.assumeTrue(AprLifecycleListener.isAprAvailable());
        StandardServer server = (StandardServer) tomcat.getServer();
        server.addLifecycleListener(listener);
    }
    Context root = tomcat.addContext("", TEMP_DIR);
    Wrapper w = Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/", "tester");
    tomcat.start();
    SSLContext sslCtx;
    // Force TLS 1.2 if TLS 1.3 is available as JSSE's TLS 1.3
    // implementation doesn't support Post Handshake Authentication
    // which is required for this test to pass.
    sslCtx = SSLContext.getInstance(Constants.SSL_PROTO_TLSv1_2);
    sslCtx.init(null, TesterSupport.getTrustManagers(), null);
    SSLSocketFactory socketFactory = sslCtx.getSocketFactory();
    SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost", getPort());
    OutputStream os = socket.getOutputStream();
    InputStream is = socket.getInputStream();
    Reader r = new InputStreamReader(is);
    doRequest(os, r);
    Assert.assertTrue("Checking no client issuer has been requested", TesterSupport.getLastClientAuthRequestedIssuerCount() == 0);
    TesterHandshakeListener listener = new TesterHandshakeListener();
    socket.addHandshakeCompletedListener(listener);
    socket.startHandshake();
    doRequest(os, r);
    // Handshake complete appears to be called asynchronously
    int wait = 0;
    while (wait < 5000 && !listener.isComplete()) {
        wait += 50;
        Thread.sleep(50);
    }
    Assert.assertTrue("Checking no client issuer has been requested", TesterSupport.getLastClientAuthRequestedIssuerCount() == 0);
    Assert.assertTrue(listener.isComplete());
    System.out.println("Renegotiation completed after " + wait + " ms");
}
Also used : SSLContext(javax.net.ssl.SSLContext) Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SSLContext(javax.net.ssl.SSLContext) AprLifecycleListener(org.apache.catalina.core.AprLifecycleListener) StandardServer(org.apache.catalina.core.StandardServer) TesterServlet(org.apache.catalina.startup.TesterServlet) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 15 with TesterServlet

use of org.apache.catalina.startup.TesterServlet in project tomcat70 by apache.

the class TestAbstractHttp11Processor method testPipelining.

@Test
public void testPipelining() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctxt = tomcat.addContext("", null);
    // Add protected servlet
    Tomcat.addServlet(ctxt, "TesterServlet", new TesterServlet());
    ctxt.addServletMapping("/foo", "TesterServlet");
    tomcat.start();
    String requestPart1 = "GET /foo HTTP/1.1" + SimpleHttpClient.CRLF;
    String requestPart2 = "Host: any" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF;
    final Client client = new Client(tomcat.getConnector().getLocalPort());
    client.setRequest(new String[] { requestPart1, requestPart2 });
    client.setRequestPause(1000);
    client.setUseContentLength(true);
    client.connect();
    Runnable send = new Runnable() {

        @Override
        public void run() {
            try {
                client.sendRequest();
                client.sendRequest();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
    Thread t = new Thread(send);
    t.start();
    // Sleep for 1500 ms which should mean the all of request 1 has been
    // sent and half of request 2
    Thread.sleep(1500);
    // Now read the first response
    client.readResponse(true);
    Assert.assertFalse(client.isResponse50x());
    Assert.assertTrue(client.isResponse200());
    Assert.assertEquals("OK", client.getResponseBody());
    // Read the second response. No need to sleep, read will block until
    // there is data to process
    client.readResponse(true);
    Assert.assertFalse(client.isResponse50x());
    Assert.assertTrue(client.isResponse200());
    Assert.assertEquals("OK", client.getResponseBody());
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) IOException(java.io.IOException) TesterServlet(org.apache.catalina.startup.TesterServlet) SimpleHttpClient(org.apache.catalina.startup.SimpleHttpClient) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

TesterServlet (org.apache.catalina.startup.TesterServlet)29 Context (org.apache.catalina.Context)25 Tomcat (org.apache.catalina.startup.Tomcat)21 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)14 Test (org.junit.Test)14 SimpleHttpClient (org.apache.catalina.startup.SimpleHttpClient)13 AsyncContext (jakarta.servlet.AsyncContext)12 LoginConfig (org.apache.tomcat.util.descriptor.web.LoginConfig)6 SecurityCollection (org.apache.tomcat.util.descriptor.web.SecurityCollection)6 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)6 LoginConfig (org.apache.catalina.deploy.LoginConfig)5 SecurityCollection (org.apache.catalina.deploy.SecurityCollection)5 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)5 Wrapper (org.apache.catalina.Wrapper)3 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)3 IOException (java.io.IOException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 List (java.util.List)2 AprLifecycleListener (org.apache.catalina.core.AprLifecycleListener)2 StandardServer (org.apache.catalina.core.StandardServer)2