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();
}
}
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");
}
}
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");
}
}
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);
}
}
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");
}
}
Aggregations