Search in sources :

Example 16 with JettyHttpClient

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

the class TestTestingHttpServer method testGuiceInjectionWithFilters.

@Test
public void testGuiceInjectionWithFilters() {
    DummyServlet servlet = new DummyServlet();
    DummyFilter filter = new DummyFilter();
    Bootstrap app = new Bootstrap(new TestingNodeModule(), new TestingHttpServerModule(), binder -> {
        binder.bind(Servlet.class).annotatedWith(TheServlet.class).toInstance(servlet);
        binder.bind(new TypeLiteral<Map<String, String>>() {
        }).annotatedWith(TheServlet.class).toInstance(ImmutableMap.of());
        newSetBinder(binder, Filter.class, TheServlet.class).addBinding().toInstance(filter);
    });
    Injector injector = app.doNotInitializeLogging().initialize();
    LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
    TestingHttpServer server = injector.getInstance(TestingHttpServer.class);
    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 {
        lifeCycleManager.stop();
    }
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) Duration(io.airlift.units.Duration) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) TheServlet(io.airlift.http.server.TheServlet) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Example 17 with JettyHttpClient

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

the class TestHttpServerProvider method testStop.

@Test(timeOut = 30000)
public void testStop() throws Exception {
    DummyServlet servlet = new DummyServlet();
    createAndStartServer(servlet);
    try (HttpClient client = new JettyHttpClient()) {
        URI uri = URI.create(httpServerInfo.getHttpUri().toASCIIString() + "/?sleep=50000");
        Request request = prepareGet().setUri(uri).build();
        HttpResponseFuture<?> future = client.executeAsync(request, createStatusResponseHandler());
        // wait until the servlet starts processing the request
        servlet.getSleeping().get(1, SECONDS);
        // stop server while the request is still active
        server.stop();
        // wait until the server is stopped
        server.join();
        // request should fail rather than sleeping the full duration
        assertThatThrownBy(() -> future.get(5, SECONDS)).isInstanceOf(ExecutionException.class).hasCauseInstanceOf(UncheckedIOException.class).hasMessageContaining("Failed communicating with server");
    }
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(io.airlift.http.client.Request) UncheckedIOException(java.io.UncheckedIOException) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 18 with JettyHttpClient

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

the class TestHttpServerProvider method testHttp.

@Test
public void testHttp() throws Exception {
    createServer();
    server.start();
    try (JettyHttpClient httpClient = new JettyHttpClient(new HttpClientConfig().setHttp2Enabled(false))) {
        StatusResponse response = httpClient.execute(prepareGet().setUri(httpServerInfo.getHttpUri()).build(), createStatusResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(response.getHeader("X-Protocol"), "HTTP/1.1");
    }
    try (JettyHttpClient httpClient = new JettyHttpClient(new HttpClientConfig().setHttp2Enabled(true))) {
        StatusResponse response = httpClient.execute(prepareGet().setUri(httpServerInfo.getHttpUri()).build(), createStatusResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(response.getHeader("X-Protocol"), "HTTP/2.0");
    }
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) Test(org.testng.annotations.Test)

Example 19 with JettyHttpClient

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

the class TestHttpServerProvider method testFilter.

@Test
public void testFilter() throws Exception {
    createServer();
    server.start();
    try (JettyHttpClient client = new JettyHttpClient()) {
        StatusResponse response = client.execute(prepareGet().setUri(httpServerInfo.getHttpUri().resolve("/filter")).build(), createStatusResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_PAYMENT_REQUIRED);
    }
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) Test(org.testng.annotations.Test)

Example 20 with JettyHttpClient

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

the class TestHttpServerProvider method testShowStackTraceDisabled.

@Test
public void testShowStackTraceDisabled() throws Exception {
    config.setShowStackTrace(false);
    createServer(new ErrorServlet());
    server.start();
    try (HttpClient client = new JettyHttpClient()) {
        StringResponse response = client.execute(prepareGet().setUri(httpServerInfo.getHttpUri()).build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), 500);
        assertThat(response.getBody()).doesNotContain("ErrorServlet.java");
    }
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) Test(org.testng.annotations.Test)

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