binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(EchoServlet.class).in(Scopes.SINGLETON);
}
});
HttpServerInfo httpServerInfo = injector.getInstance(HttpServerInfo.class);
InMemoryEventClient eventClient = injector.getInstance(InMemoryEventClient.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;
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, Charsets.UTF_8))
.build(),
createStringResponseHandler());
afterRequest = System.currentTimeMillis();
assertEquals(response.getStatusCode(), responseCode);
assertEquals(response.getBody(), responseBody);
assertEquals(response.getHeader("Content-Type"), responseContentType);
}
finally {
server.stop();
}
List<Object> events = eventClient.getEvents();
Assert.assertEquals(events.size(), 1);
HttpRequestEvent event = (HttpRequestEvent) events.get(0);
Assert.assertEquals(event.getClientAddress(), echoServlet.remoteAddress);