Search in sources :

Example 6 with TesterServlet

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

the class TestHttp11Processor method testPipelining.

@Test
public void testPipelining() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add protected servlet
    Tomcat.addServlet(ctx, "TesterServlet", new TesterServlet());
    ctx.addServletMappingDecoded("/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);
    assertFalse(client.isResponse50x());
    assertTrue(client.isResponse200());
    assertEquals("OK", client.getResponseBody());
    // Read the second response. No need to sleep, read will block until
    // there is data to process
    client.readResponse(true);
    assertFalse(client.isResponse50x());
    assertTrue(client.isResponse200());
    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)

Example 7 with TesterServlet

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

the class TestNonLoginAndBasicAuthenticator method setUpNonLogin.

private void setUpNonLogin() throws Exception {
    // Must have a real docBase for webapps - just use temp
    nonloginContext = tomcat.addContext(CONTEXT_PATH_NOLOGIN, System.getProperty("java.io.tmpdir"));
    // Add protected servlet to the context
    Tomcat.addServlet(nonloginContext, "TesterServlet1", new TesterServlet());
    nonloginContext.addServletMappingDecoded(URI_PROTECTED, "TesterServlet1");
    SecurityCollection collection1 = new SecurityCollection();
    collection1.addPatternDecoded(URI_PROTECTED);
    SecurityConstraint sc1 = new SecurityConstraint();
    sc1.addAuthRole(ROLE);
    sc1.addCollection(collection1);
    nonloginContext.addConstraint(sc1);
    // Add unprotected servlet to the context
    Tomcat.addServlet(nonloginContext, "TesterServlet2", new TesterServlet());
    nonloginContext.addServletMappingDecoded(URI_PUBLIC, "TesterServlet2");
    SecurityCollection collection2 = new SecurityCollection();
    collection2.addPatternDecoded(URI_PUBLIC);
    SecurityConstraint sc2 = new SecurityConstraint();
    // do not add a role - which signals access permitted without one
    sc2.addCollection(collection2);
    nonloginContext.addConstraint(sc2);
    // Configure the authenticator and inherit the Realm from Engine
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("NONE");
    nonloginContext.setLoginConfig(lc);
    AuthenticatorBase nonloginAuthenticator = new NonLoginAuthenticator();
    nonloginContext.getPipeline().addValve(nonloginAuthenticator);
}
Also used : LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) TesterServlet(org.apache.catalina.startup.TesterServlet) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection)

Example 8 with TesterServlet

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

the class TestSSOnonLoginAndDigestAuthenticator method setUpNonLogin.

private void setUpNonLogin(Tomcat tomcat) throws Exception {
    // Must have a real docBase for webapps - just use temp
    Context ctxt = tomcat.addContext(CONTEXT_PATH_NOLOGIN, System.getProperty("java.io.tmpdir"));
    ctxt.setSessionTimeout(LONG_TIMEOUT_SECS);
    // Add protected servlet
    Tomcat.addServlet(ctxt, "TesterServlet1", new TesterServlet());
    ctxt.addServletMappingDecoded(URI_PROTECTED, "TesterServlet1");
    SecurityCollection collection1 = new SecurityCollection();
    collection1.addPatternDecoded(URI_PROTECTED);
    SecurityConstraint sc1 = new SecurityConstraint();
    sc1.addAuthRole(ROLE);
    sc1.addCollection(collection1);
    ctxt.addConstraint(sc1);
    // Add unprotected servlet
    Tomcat.addServlet(ctxt, "TesterServlet2", new TesterServlet());
    ctxt.addServletMappingDecoded(URI_PUBLIC, "TesterServlet2");
    SecurityCollection collection2 = new SecurityCollection();
    collection2.addPatternDecoded(URI_PUBLIC);
    SecurityConstraint sc2 = new SecurityConstraint();
    // do not add a role - which signals access permitted without one
    sc2.addCollection(collection2);
    ctxt.addConstraint(sc2);
    // Configure the appropriate authenticator
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("NONE");
    ctxt.setLoginConfig(lc);
    ctxt.getPipeline().addValve(new NonLoginAuthenticator());
}
Also used : Context(org.apache.catalina.Context) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) TesterServlet(org.apache.catalina.startup.TesterServlet) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection)

Example 9 with TesterServlet

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

the class TestConnector method testStop.

@Test
public void testStop() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    Wrapper w = Tomcat.addServlet(root, "tester", new TesterServlet());
    w.setAsyncSupported(true);
    root.addServletMappingDecoded("/", "tester");
    Connector connector = tomcat.getConnector();
    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null);
    assertEquals(200, rc);
    assertEquals("OK", bc.toString());
    rc = -1;
    bc.recycle();
    connector.stop();
    try {
        rc = getUrl("http://localhost:" + getPort() + "/", bc, 1000, null, null);
    } catch (SocketTimeoutException ste) {
        // May also see this with NIO
        // Make sure the test passes if we do
        rc = 503;
    }
    assertEquals(503, rc);
}
Also used : Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) SocketTimeoutException(java.net.SocketTimeoutException) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TesterServlet(org.apache.catalina.startup.TesterServlet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

TesterServlet (org.apache.catalina.startup.TesterServlet)9 Context (org.apache.catalina.Context)7 Tomcat (org.apache.catalina.startup.Tomcat)5 LoginConfig (org.apache.tomcat.util.descriptor.web.LoginConfig)5 SecurityCollection (org.apache.tomcat.util.descriptor.web.SecurityCollection)5 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)5 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 Wrapper (org.apache.catalina.Wrapper)2 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 SSLContext (javax.net.ssl.SSLContext)1 SSLSocket (javax.net.ssl.SSLSocket)1