Search in sources :

Example 11 with LocalConnector

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

the class ContextHandlerTest method checkWildcardHost.

private void checkWildcardHost(boolean succeed, Server server, String[] contextHosts, String[] requestHosts) throws Exception {
    LocalConnector connector = (LocalConnector) server.getConnectors()[0];
    ContextHandler context = (ContextHandler) server.getHandler();
    context.setVirtualHosts(contextHosts);
    IsHandledHandler handler = (IsHandledHandler) context.getHandler();
    for (String host : requestHosts) {
        connector.getResponse("GET / HTTP/1.1\n" + "Host: " + host + "\nConnection:close\n\n");
        if (succeed)
            Assert.assertTrue("'" + host + "' should have been handled.", handler.isHandled());
        else
            Assert.assertFalse("'" + host + "' should not have been handled.", handler.isHandled());
        handler.reset();
    }
}
Also used : LocalConnector(org.eclipse.jetty.server.LocalConnector)

Example 12 with LocalConnector

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

the class ContextHandlerTest method testVirtualHostNormalization.

@Test
public void testVirtualHostNormalization() throws Exception {
    Server server = new Server();
    LocalConnector connector = new LocalConnector(server);
    server.setConnectors(new Connector[] { connector });
    ContextHandler contextA = new ContextHandler("/");
    contextA.setVirtualHosts(new String[] { "www.example.com" });
    IsHandledHandler handlerA = new IsHandledHandler();
    contextA.setHandler(handlerA);
    ContextHandler contextB = new ContextHandler("/");
    IsHandledHandler handlerB = new IsHandledHandler();
    contextB.setHandler(handlerB);
    contextB.setVirtualHosts(new String[] { "www.example2.com." });
    ContextHandler contextC = new ContextHandler("/");
    IsHandledHandler handlerC = new IsHandledHandler();
    contextC.setHandler(handlerC);
    HandlerCollection c = new HandlerCollection();
    c.addHandler(contextA);
    c.addHandler(contextB);
    c.addHandler(contextC);
    server.setHandler(c);
    try {
        server.start();
        connector.getResponses("GET / HTTP/1.0\n" + "Host: www.example.com.\n\n");
        Assert.assertTrue(handlerA.isHandled());
        Assert.assertFalse(handlerB.isHandled());
        Assert.assertFalse(handlerC.isHandled());
        handlerA.reset();
        handlerB.reset();
        handlerC.reset();
        connector.getResponses("GET / HTTP/1.0\n" + "Host: www.example2.com\n\n");
        Assert.assertFalse(handlerA.isHandled());
        Assert.assertTrue(handlerB.isHandled());
        Assert.assertFalse(handlerC.isHandled());
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) Test(org.junit.Test)

Example 13 with LocalConnector

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

the class ContextHandlerCollectionTest method checkWildcardHost.

private void checkWildcardHost(boolean succeed, Server server, String[] contextHosts, String[] requestHosts) throws Exception {
    LocalConnector connector = (LocalConnector) server.getConnectors()[0];
    ContextHandlerCollection handlerCollection = (ContextHandlerCollection) server.getHandler();
    ContextHandler context = (ContextHandler) handlerCollection.getHandlers()[0];
    IsHandledHandler handler = (IsHandledHandler) context.getHandler();
    context.setVirtualHosts(contextHosts);
    // trigger this manually; it's supposed to be called when adding the handler
    handlerCollection.mapContexts();
    for (String host : requestHosts) {
        // System.err.printf("host=%s in %s%n",host,contextHosts==null?Collections.emptyList():Arrays.asList(contextHosts));
        String response = connector.getResponse("GET / HTTP/1.0\n" + "Host: " + host + "\nConnection:close\n\n");
        // System.err.println(response);
        if (succeed)
            assertTrue("'" + host + "' should have been handled.", handler.isHandled());
        else
            assertFalse("'" + host + "' should not have been handled.", handler.isHandled());
        handler.reset();
    }
}
Also used : LocalConnector(org.eclipse.jetty.server.LocalConnector) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 14 with LocalConnector

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

the class WebAppContextTest method testNullPath.

@Test
public void testNullPath() throws Exception {
    Server server = new Server(0);
    HandlerList handlers = new HandlerList();
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    WebAppContext context = new WebAppContext();
    context.setBaseResource(Resource.newResource("./src/test/webapp"));
    context.setContextPath("/");
    server.setHandler(handlers);
    handlers.addHandler(contexts);
    contexts.addHandler(context);
    LocalConnector connector = new LocalConnector(server);
    server.addConnector(connector);
    server.start();
    try {
        String response = connector.getResponses("GET http://localhost:8080 HTTP/1.1\r\nHost: localhost:8080\r\nConnection: close\r\n\r\n");
        Assert.assertTrue(response.indexOf("200 OK") >= 0);
    } finally {
        server.stop();
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) Test(org.junit.Test)

Example 15 with LocalConnector

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

the class ServletContextHandlerTest method createServer.

@Before
public void createServer() {
    _server = new Server();
    _connector = new LocalConnector(_server);
    _server.addConnector(_connector);
    __testServlets.set(0);
}
Also used : Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) Before(org.junit.Before)

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