Search in sources :

Example 6 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestTestingHttpServer method testFilteredRequest.

@Test
public void testFilteredRequest() throws Exception {
    DummyServlet servlet = new DummyServlet();
    DummyFilter filter = new DummyFilter();
    TestingHttpServer server = createTestingHttpServerWithFilter(servlet, ImmutableMap.of(), filter);
    try {
        server.start();
        try (HttpClient client = new JettyHttpClient(new HttpClientConfig().setConnectTimeout(new Duration(1, SECONDS)))) {
            StatusResponse response = client.execute(prepareGet().setUri(server.getBaseUrl()).build(), createStatusResponseHandler());
            assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
            assertEquals(servlet.getCallCount(), 1);
            assertEquals(filter.getCallCount(), 1);
        }
    } finally {
        server.stop();
    }
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) Duration(io.airlift.units.Duration) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) Test(org.testng.annotations.Test)

Example 7 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestTestingHttpServer method testRequest.

@Test
public void testRequest() throws Exception {
    DummyServlet servlet = new DummyServlet();
    TestingHttpServer server = createTestingHttpServer(servlet, ImmutableMap.of());
    try {
        server.start();
        try (HttpClient client = new JettyHttpClient(new HttpClientConfig().setConnectTimeout(new Duration(1, SECONDS)))) {
            StatusResponse response = client.execute(prepareGet().setUri(server.getBaseUrl()).build(), createStatusResponseHandler());
            assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
            assertEquals(servlet.getCallCount(), 1);
        }
    } finally {
        server.stop();
    }
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) Duration(io.airlift.units.Duration) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) Test(org.testng.annotations.Test)

Example 8 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestServer method setup.

@BeforeMethod
public void setup() throws Exception {
    Bootstrap app = new Bootstrap(new TestingNodeModule(), new TestingHttpServerModule(), new JsonModule(), new JaxrsModule(), new JmxHttpModule(), new JmxModule(), new MainModule());
    Injector injector = app.doNotInitializeLogging().initialize();
    lifeCycleManager = injector.getInstance(LifeCycleManager.class);
    server = injector.getInstance(TestingHttpServer.class);
    client = new JettyHttpClient();
}
Also used : LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) TestingHttpServerModule(io.airlift.http.server.testing.TestingHttpServerModule) JmxModule(io.airlift.jmx.JmxModule) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JaxrsModule(io.airlift.jaxrs.JaxrsModule) TestingHttpServer(io.airlift.http.server.testing.TestingHttpServer) Bootstrap(io.airlift.bootstrap.Bootstrap) JsonModule(io.airlift.json.JsonModule) JmxHttpModule(io.airlift.jmx.JmxHttpModule) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 9 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestServiceInventory method testHttpServiceInventory.

@Test
public void testHttpServiceInventory() throws Exception {
    String serviceInventoryJson = Resources.toString(Resources.getResource("service-inventory.json"), UTF_8);
    Server server = null;
    try (JettyHttpClient httpClient = new JettyHttpClient()) {
        int port;
        try (ServerSocket socket = new ServerSocket()) {
            socket.bind(new InetSocketAddress(0));
            port = socket.getLocalPort();
        }
        URI baseURI = new URI("http", null, "127.0.0.1", port, null, null, null);
        HttpConfiguration httpConfiguration = new HttpConfiguration();
        httpConfiguration.setSendServerVersion(false);
        httpConfiguration.setSendXPoweredBy(false);
        server = new Server();
        ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfiguration));
        httpConnector.setPort(port);
        httpConnector.setName("http");
        server.addConnector(httpConnector);
        ServletHolder servletHolder = new ServletHolder(new ServiceInventoryServlet(serviceInventoryJson));
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        context.addServlet(servletHolder, "/*");
        HandlerCollection handlers = new HandlerCollection();
        handlers.addHandler(context);
        server.setHandler(handlers);
        server.start();
        // test
        ServiceInventoryConfig serviceInventoryConfig = new ServiceInventoryConfig().setServiceInventoryUri(baseURI);
        ServiceInventory serviceInventory = new ServiceInventory(serviceInventoryConfig, new NodeInfo("test"), JsonCodec.jsonCodec(ServiceDescriptorsRepresentation.class), httpClient);
        assertEquals(Iterables.size(serviceInventory.getServiceDescriptors()), 2);
        assertEquals(Iterables.size(serviceInventory.getServiceDescriptors("discovery")), 2);
        assertEquals(Iterables.size(serviceInventory.getServiceDescriptors("discovery", "general")), 2);
        serviceInventory.updateServiceInventory();
        assertEquals(Iterables.size(serviceInventory.getServiceDescriptors()), 2);
        assertEquals(Iterables.size(serviceInventory.getServiceDescriptors("discovery")), 2);
        assertEquals(Iterables.size(serviceInventory.getServiceDescriptors("discovery", "general")), 2);
    } finally {
        if (server != null) {
            server.stop();
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) InetSocketAddress(java.net.InetSocketAddress) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServerSocket(java.net.ServerSocket) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) URI(java.net.URI) ServerConnector(org.eclipse.jetty.server.ServerConnector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) NodeInfo(io.airlift.node.NodeInfo) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.testng.annotations.Test)

Example 10 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestServer method setup.

@BeforeMethod
public void setup() {
    Bootstrap app = new Bootstrap(new TestingNodeModule(), new InMemoryEventModule(), new TestingHttpServerModule(), new JsonModule(), new JaxrsModule(), new MainModule());
    Injector injector = app.doNotInitializeLogging().initialize();
    lifeCycleManager = injector.getInstance(LifeCycleManager.class);
    server = injector.getInstance(TestingHttpServer.class);
    store = injector.getInstance(PersonStore.class);
    eventClient = injector.getInstance(InMemoryEventClient.class);
    client = new JettyHttpClient();
}
Also used : TestingHttpServerModule(io.airlift.http.server.testing.TestingHttpServerModule) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) JaxrsModule(io.airlift.jaxrs.JaxrsModule) JsonModule(io.airlift.json.JsonModule) InMemoryEventModule(io.airlift.event.client.InMemoryEventModule) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) TestingHttpServer(io.airlift.http.server.testing.TestingHttpServer) Bootstrap(io.airlift.bootstrap.Bootstrap) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)29 Test (org.testng.annotations.Test)21 HttpClient (io.airlift.http.client.HttpClient)11 HttpClientConfig (io.airlift.http.client.HttpClientConfig)11 Injector (com.google.inject.Injector)10 Bootstrap (io.airlift.bootstrap.Bootstrap)10 Duration (io.airlift.units.Duration)9 LifeCycleManager (io.airlift.bootstrap.LifeCycleManager)8 TestingNodeModule (io.airlift.node.testing.TestingNodeModule)8 StatusResponse (io.airlift.http.client.StatusResponseHandler.StatusResponse)7 StringResponse (io.airlift.http.client.StringResponseHandler.StringResponse)7 URI (java.net.URI)7 ImmutableMap (com.google.common.collect.ImmutableMap)5 NodeInfo (io.airlift.node.NodeInfo)4 InMemoryEventModule (io.airlift.event.client.InMemoryEventModule)3 TheServlet (io.airlift.http.server.TheServlet)3 TraceTokenModule (io.airlift.tracetoken.TraceTokenModule)3 File (java.io.File)3 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3