use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.
the class TestOverrideMethodFilterInHttpServer method setup.
@BeforeClass
public void setup() throws Exception {
resource = new TestResource();
server = createServer(resource);
client = new JettyHttpClient();
server.start();
}
use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.
the class TestHttpServerModule method testHttpRequestEvent.
@Test
public void testHttpRequestEvent() throws Exception {
Map<String, String> properties = new ImmutableMap.Builder<String, String>().put("http-server.http.port", "0").put("http-server.log.path", new File(tempDir, "http-request.log").getAbsolutePath()).build();
SingleUseEventClient eventClient = new SingleUseEventClient();
Bootstrap app = new Bootstrap(new HttpServerModule(), new TestingNodeModule(), new InMemoryEventModule(), new TraceTokenModule(), binder -> newSetBinder(binder, EventClient.class).addBinding().toInstance(eventClient), binder -> binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(EchoServlet.class).in(Scopes.SINGLETON));
Injector injector = app.setRequiredConfigurationProperties(properties).doNotInitializeLogging().initialize();
HttpServerInfo httpServerInfo = injector.getInstance(HttpServerInfo.class);
EchoServlet echoServlet = (EchoServlet) injector.getInstance(Key.get(Servlet.class, TheServlet.class));
HttpServer server = injector.getInstance(HttpServer.class);
server.start();
URI requestUri = httpServerInfo.getHttpUri().resolve("/my/path");
String userAgent = "my-user-agent";
String referrer = "http://www.google.com";
String token = "this is a trace token";
String requestBody = Joiner.on(" ").join(nCopies(50, "request"));
String requestContentType = "request/type";
int responseCode = 555;
String responseBody = Joiner.on(" ").join(nCopies(100, "response"));
String responseContentType = "response/type";
echoServlet.responseBody = responseBody;
echoServlet.responseStatusCode = responseCode;
echoServlet.responseHeaders.put("Content-Type", responseContentType);
long beforeRequest = System.currentTimeMillis();
long afterRequest;
HttpRequestEvent event;
try (JettyHttpClient client = new JettyHttpClient()) {
// test servlet bound correctly
StringResponse response = client.execute(preparePost().setUri(requestUri).addHeader(USER_AGENT, userAgent).addHeader(CONTENT_TYPE, requestContentType).addHeader(REFERER, referrer).addHeader("X-Airlift-TraceToken", token).setBodyGenerator(createStaticBodyGenerator(requestBody, UTF_8)).build(), createStringResponseHandler());
afterRequest = System.currentTimeMillis();
assertEquals(response.getStatusCode(), responseCode);
assertEquals(response.getBody(), responseBody);
assertEquals(response.getHeader("Content-Type"), responseContentType);
event = (HttpRequestEvent) eventClient.getEvent().get(10, TimeUnit.SECONDS);
} finally {
server.stop();
}
assertEquals(event.getClientAddress(), echoServlet.remoteAddress);
assertEquals(event.getProtocol(), "http");
assertEquals(event.getMethod(), "POST");
assertEquals(event.getRequestUri(), requestUri.getPath());
assertNull(event.getUser());
assertEquals(event.getAgent(), userAgent);
assertEquals(event.getReferrer(), referrer);
assertEquals(event.getTraceToken(), token);
assertEquals(event.getRequestSize(), requestBody.length());
assertEquals(event.getRequestContentType(), requestContentType);
assertEquals(event.getResponseSize(), responseBody.length());
assertEquals(event.getResponseCode(), responseCode);
assertEquals(event.getResponseContentType(), responseContentType);
assertTrue(event.getTimeStamp().toEpochMilli() >= beforeRequest);
assertTrue(event.getTimeToLastByte() <= afterRequest - beforeRequest);
assertNotNull(event.getTimeToFirstByte());
assertTrue(event.getTimeToDispatch() <= event.getTimeToFirstByte());
assertTrue(event.getTimeToFirstByte() <= event.getTimeToLastByte());
}
use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.
the class TestHttpServerModule method testServer.
@Test
public void testServer() throws Exception {
Map<String, String> properties = new ImmutableMap.Builder<String, String>().put("http-server.http.port", "0").put("http-server.log.path", new File(tempDir, "http-request.log").getAbsolutePath()).build();
Bootstrap app = new Bootstrap(new HttpServerModule(), new TestingNodeModule(), new EventModule(), binder -> {
binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(DummyServlet.class);
newSetBinder(binder, Filter.class, TheServlet.class).addBinding().to(DummyFilter.class).in(Scopes.SINGLETON);
httpServerBinder(binder).bindResource("/", "webapp/user").withWelcomeFile("user-welcome.txt");
httpServerBinder(binder).bindResource("/", "webapp/user2");
httpServerBinder(binder).bindResource("path", "webapp/user").withWelcomeFile("user-welcome.txt");
httpServerBinder(binder).bindResource("path", "webapp/user2");
});
Injector injector = app.setRequiredConfigurationProperties(properties).doNotInitializeLogging().initialize();
HttpServerInfo httpServerInfo = injector.getInstance(HttpServerInfo.class);
HttpServer server = injector.getInstance(HttpServer.class);
server.start();
try (HttpClient client = new JettyHttpClient()) {
// test servlet bound correctly
URI httpUri = httpServerInfo.getHttpUri();
StatusResponse response = client.execute(prepareGet().setUri(httpUri).build(), createStatusResponseHandler());
assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
// test filter bound correctly
response = client.execute(prepareGet().setUri(httpUri.resolve("/filter")).build(), createStatusResponseHandler());
assertEquals(response.getStatusCode(), HttpServletResponse.SC_PAYMENT_REQUIRED);
// test http resources
assertResource(httpUri, client, "", "welcome user!");
assertResource(httpUri, client, "user-welcome.txt", "welcome user!");
assertResource(httpUri, client, "user.txt", "user");
assertResource(httpUri, client, "user2.txt", "user2");
assertRedirect(httpUri, client, "path", "/path/");
assertResource(httpUri, client, "path/", "welcome user!");
assertResource(httpUri, client, "path/user-welcome.txt", "welcome user!");
assertResource(httpUri, client, "path/user.txt", "user");
assertResource(httpUri, client, "path/user2.txt", "user2");
} finally {
server.stop();
}
}
use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.
the class TestTestingHttpServer method testGuiceInjectionWithResources.
@Test
public void testGuiceInjectionWithResources() {
DummyServlet servlet = new DummyServlet();
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());
httpServerBinder(binder).bindResource("/", "webapp/user").withWelcomeFile("user-welcome.txt");
httpServerBinder(binder).bindResource("/", "webapp/user2");
httpServerBinder(binder).bindResource("path", "webapp/user").withWelcomeFile("user-welcome.txt");
httpServerBinder(binder).bindResource("path", "webapp/user2");
});
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)))) {
// test http resources
URI uri = server.getBaseUrl();
assertResource(uri, client, "", "welcome user!");
assertResource(uri, client, "user-welcome.txt", "welcome user!");
assertResource(uri, client, "user.txt", "user");
assertResource(uri, client, "user2.txt", "user2");
assertResource(uri, client, "path", "welcome user!");
assertResource(uri, client, "path/", "welcome user!");
assertResource(uri, client, "path/user-welcome.txt", "welcome user!");
assertResource(uri, client, "path/user.txt", "user");
assertResource(uri, client, "path/user2.txt", "user2");
// verify that servlet did not receive resource requests
assertEquals(servlet.getCallCount(), 0);
} finally {
lifeCycleManager.stop();
}
}
use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.
the class TestTestingHttpServer method testGuiceInjectionWithoutFilters.
@Test
public void testGuiceInjectionWithoutFilters() {
DummyServlet servlet = new DummyServlet();
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());
});
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);
} finally {
lifeCycleManager.stop();
}
}
Aggregations