Search in sources :

Example 31 with LocalConnector

use of org.eclipse.jetty.server.LocalConnector in project jetty.project by eclipse.

the class ContextHandlerCollectionTest method testWrappedContext.

@Test
public void testWrappedContext() throws Exception {
    Server server = new Server();
    LocalConnector connector = new LocalConnector(server);
    server.setConnectors(new Connector[] { connector });
    ContextHandler root = new ContextHandler("/");
    root.setHandler(new IsHandledHandler("root"));
    ContextHandler left = new ContextHandler("/left");
    left.setHandler(new IsHandledHandler("left"));
    HandlerList centre = new HandlerList();
    ContextHandler centreLeft = new ContextHandler("/leftcentre");
    centreLeft.setHandler(new IsHandledHandler("left of centre"));
    ContextHandler centreRight = new ContextHandler("/rightcentre");
    centreRight.setHandler(new IsHandledHandler("right of centre"));
    centre.setHandlers(new Handler[] { centreLeft, new WrappedHandler(centreRight) });
    ContextHandler right = new ContextHandler("/right");
    right.setHandler(new IsHandledHandler("right"));
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(new Handler[] { root, left, centre, new WrappedHandler(right) });
    server.setHandler(contexts);
    server.start();
    String response = connector.getResponses("GET / HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("root"));
    assertThat(response, not(containsString("Wrapped: TRUE")));
    response = connector.getResponses("GET /foobar/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("root"));
    assertThat(response, not(containsString("Wrapped: TRUE")));
    response = connector.getResponses("GET /left/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("left"));
    assertThat(response, not(containsString("Wrapped: TRUE")));
    response = connector.getResponses("GET /leftcentre/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("left of centre"));
    assertThat(response, not(containsString("Wrapped: TRUE")));
    response = connector.getResponses("GET /rightcentre/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("right of centre"));
    assertThat(response, containsString("Wrapped: TRUE"));
    response = connector.getResponses("GET /right/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("right"));
    assertThat(response, containsString("Wrapped: TRUE"));
}
Also used : Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 32 with LocalConnector

use of org.eclipse.jetty.server.LocalConnector in project jetty.project by eclipse.

the class ContextHandlerCollectionTest method testVirtualHosts.

@Test
public void testVirtualHosts() throws Exception {
    Server server = new Server();
    LocalConnector connector0 = new LocalConnector(server);
    LocalConnector connector1 = new LocalConnector(server);
    connector1.setName("connector1");
    server.setConnectors(new Connector[] { connector0, connector1 });
    ContextHandler contextA = new ContextHandler("/ctx");
    contextA.setVirtualHosts(new String[] { "www.example.com", "alias.example.com" });
    IsHandledHandler handlerA = new IsHandledHandler("A");
    contextA.setHandler(handlerA);
    contextA.setAllowNullPathInfo(true);
    ContextHandler contextB = new ContextHandler("/ctx");
    IsHandledHandler handlerB = new IsHandledHandler("B");
    contextB.setHandler(handlerB);
    contextB.setVirtualHosts(new String[] { "*.other.com", "@connector1" });
    ContextHandler contextC = new ContextHandler("/ctx");
    IsHandledHandler handlerC = new IsHandledHandler("C");
    contextC.setHandler(handlerC);
    ContextHandler contextD = new ContextHandler("/");
    IsHandledHandler handlerD = new IsHandledHandler("D");
    contextD.setHandler(handlerD);
    ContextHandler contextE = new ContextHandler("/ctx/foo");
    IsHandledHandler handlerE = new IsHandledHandler("E");
    contextE.setHandler(handlerE);
    ContextHandler contextF = new ContextHandler("/ctxlong");
    IsHandledHandler handlerF = new IsHandledHandler("F");
    contextF.setHandler(handlerF);
    ContextHandlerCollection c = new ContextHandlerCollection();
    c.addHandler(contextA);
    c.addHandler(contextB);
    c.addHandler(contextC);
    HandlerList list = new HandlerList();
    list.addHandler(contextE);
    list.addHandler(contextF);
    list.addHandler(contextD);
    c.addHandler(list);
    server.setHandler(c);
    try {
        server.start();
        Object[][] tests = new Object[][] { { connector0, "www.example.com.", "/ctx", handlerA }, { connector0, "www.example.com.", "/ctx/", handlerA }, { connector0, "www.example.com.", "/ctx/info", handlerA }, { connector0, "www.example.com", "/ctx/info", handlerA }, { connector0, "alias.example.com", "/ctx/info", handlerA }, { connector1, "www.example.com.", "/ctx/info", handlerA }, { connector1, "www.example.com", "/ctx/info", handlerA }, { connector1, "alias.example.com", "/ctx/info", handlerA }, { connector1, "www.other.com", "/ctx", null }, { connector1, "www.other.com", "/ctx/", handlerB }, { connector1, "www.other.com", "/ctx/info", handlerB }, { connector0, "www.other.com", "/ctx/info", handlerC }, { connector0, "www.example.com", "/ctxinfo", handlerD }, { connector1, "unknown.com", "/unknown", handlerD }, { connector0, "alias.example.com", "/ctx/foo/info", handlerE }, { connector0, "alias.example.com", "/ctxlong/info", handlerF } };
        for (int i = 0; i < tests.length; i++) {
            Object[] test = tests[i];
            LocalConnector connector = (LocalConnector) test[0];
            String host = (String) test[1];
            String uri = (String) test[2];
            IsHandledHandler handler = (IsHandledHandler) test[3];
            handlerA.reset();
            handlerB.reset();
            handlerC.reset();
            handlerD.reset();
            handlerE.reset();
            handlerF.reset();
            String t = String.format("test   %d %s@%s --> %s | %s%n", i, uri, host, connector.getName(), handler);
            String response = connector.getResponses("GET " + uri + " HTTP/1.0\nHost: " + host + "\n\n");
            if (handler == null) {
                Assert.assertThat(t, response, Matchers.containsString(" 302 "));
            } else {
                assertThat(t, response, endsWith(handler.toString()));
                if (!handler.isHandled()) {
                    System.err.printf("FAILED %s", t);
                    System.err.println(response);
                    Assert.fail();
                }
            }
        }
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 33 with LocalConnector

use of org.eclipse.jetty.server.LocalConnector in project jetty.project by eclipse.

the class ContextHandlerCollectionTest method testAsyncWrappedContext.

@Test
public void testAsyncWrappedContext() throws Exception {
    Server server = new Server();
    LocalConnector connector = new LocalConnector(server);
    server.setConnectors(new Connector[] { connector });
    ContextHandler root = new ContextHandler("/");
    root.setHandler(new AsyncHandler("root"));
    ContextHandler left = new ContextHandler("/left");
    left.setHandler(new AsyncHandler("left"));
    HandlerList centre = new HandlerList();
    ContextHandler centreLeft = new ContextHandler("/leftcentre");
    centreLeft.setHandler(new AsyncHandler("left of centre"));
    ContextHandler centreRight = new ContextHandler("/rightcentre");
    centreRight.setHandler(new AsyncHandler("right of centre"));
    centre.setHandlers(new Handler[] { centreLeft, new WrappedHandler(centreRight) });
    ContextHandler right = new ContextHandler("/right");
    right.setHandler(new AsyncHandler("right"));
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(new Handler[] { root, left, centre, new WrappedHandler(right) });
    server.setHandler(contexts);
    server.start();
    String response = connector.getResponses("GET / HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("root"));
    assertThat(response, not(containsString("Wrapped: TRUE")));
    response = connector.getResponses("GET /foobar/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("root"));
    assertThat(response, not(containsString("Wrapped: TRUE")));
    response = connector.getResponses("GET /left/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("left"));
    assertThat(response, not(containsString("Wrapped: TRUE")));
    response = connector.getResponses("GET /leftcentre/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("left of centre"));
    assertThat(response, not(containsString("Wrapped: TRUE")));
    response = connector.getResponses("GET /rightcentre/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("right of centre"));
    assertThat(response, containsString("Wrapped: ASYNC"));
    response = connector.getResponses("GET /right/info HTTP/1.0\r\n\r\n");
    assertThat(response, startsWith("HTTP/1.1 200 OK"));
    assertThat(response, endsWith("right"));
    assertThat(response, containsString("Wrapped: ASYNC"));
}
Also used : Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 34 with LocalConnector

use of org.eclipse.jetty.server.LocalConnector in project jetty.project by eclipse.

the class ThreadLimitHandlerTest method before.

@Before
public void before() throws Exception {
    _server = new Server();
    _connector = new ServerConnector(_server);
    _local = new LocalConnector(_server);
    _server.setConnectors(new Connector[] { _local, _connector });
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) Before(org.junit.Before)

Example 35 with LocalConnector

use of org.eclipse.jetty.server.LocalConnector in project jetty.project by eclipse.

the class ContextHandlerTest method testLifeCycle.

@Test
public void testLifeCycle() throws Exception {
    Server server = new Server();
    LocalConnector connector = new LocalConnector(server);
    server.setConnectors(new Connector[] { connector });
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    server.setHandler(contexts);
    ContextHandler root = new ContextHandler(contexts, "/");
    root.setHandler(new ContextPathHandler());
    ContextHandler foo = new ContextHandler(contexts, "/foo");
    foo.setHandler(new ContextPathHandler());
    ContextHandler foobar = new ContextHandler(contexts, "/foo/bar");
    foobar.setHandler(new ContextPathHandler());
    // check that all contexts start normally
    server.start();
    Assert.assertThat(connector.getResponses("GET / HTTP/1.0\n\n"), Matchers.containsString("ctx=''"));
    Assert.assertThat(connector.getResponses("GET /foo/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx='/foo'"));
    Assert.assertThat(connector.getResponses("GET /foo/bar/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx='/foo/bar'"));
    // If we stop foobar, then requests will be handled by foo
    foobar.stop();
    Assert.assertThat(connector.getResponses("GET / HTTP/1.0\n\n"), Matchers.containsString("ctx=''"));
    Assert.assertThat(connector.getResponses("GET /foo/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx='/foo'"));
    Assert.assertThat(connector.getResponses("GET /foo/bar/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx='/foo'"));
    // If we shutdown foo then requests will be 503'd
    foo.shutdown().get();
    Assert.assertThat(connector.getResponses("GET / HTTP/1.0\n\n"), Matchers.containsString("ctx=''"));
    Assert.assertThat(connector.getResponses("GET /foo/xxx HTTP/1.0\n\n"), Matchers.containsString("503"));
    Assert.assertThat(connector.getResponses("GET /foo/bar/xxx HTTP/1.0\n\n"), Matchers.containsString("503"));
    // If we stop foo then requests will be handled by root
    foo.stop();
    Assert.assertThat(connector.getResponses("GET / HTTP/1.0\n\n"), Matchers.containsString("ctx=''"));
    Assert.assertThat(connector.getResponses("GET /foo/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx=''"));
    Assert.assertThat(connector.getResponses("GET /foo/bar/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx=''"));
    // If we start foo then foobar requests will be handled by foo
    foo.start();
    Assert.assertThat(connector.getResponses("GET / HTTP/1.0\n\n"), Matchers.containsString("ctx=''"));
    Assert.assertThat(connector.getResponses("GET /foo/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx='/foo'"));
    Assert.assertThat(connector.getResponses("GET /foo/bar/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx='/foo'"));
    // If we start foobar then foobar requests will be handled by foobar
    foobar.start();
    Assert.assertThat(connector.getResponses("GET / HTTP/1.0\n\n"), Matchers.containsString("ctx=''"));
    Assert.assertThat(connector.getResponses("GET /foo/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx='/foo'"));
    Assert.assertThat(connector.getResponses("GET /foo/bar/xxx HTTP/1.0\n\n"), Matchers.containsString("ctx='/foo/bar'"));
}
Also used : Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) Test(org.junit.Test)

Aggregations

LocalConnector (org.eclipse.jetty.server.LocalConnector)47 Server (org.eclipse.jetty.server.Server)43 Before (org.junit.Before)20 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)13 Test (org.junit.Test)12 Matchers.containsString (org.hamcrest.Matchers.containsString)7 BeforeClass (org.junit.BeforeClass)7 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)6 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)5 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)4 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)4 Password (org.eclipse.jetty.util.security.Password)4 File (java.io.File)3 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ServerConnector (org.eclipse.jetty.server.ServerConnector)3 HandlerList (org.eclipse.jetty.server.handler.HandlerList)3 Path (java.nio.file.Path)2 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2