public void testCompletionHandler() throws Exception {
HttpServer server = new HttpServer(new RequestHandler());
server.start();
HttpClient httpClient = new HttpClient();
FutureResponseHandler hdl = new FutureResponseHandler();
BodyDataSink ds = httpClient.send(new HttpRequestHeader("POST", "http://localhost:" +server.getLocalPort() + "/", "text/plain"), 200, hdl);
try {
for (int i = 0; i < 20; i++) {
IWriteCompletionHandler ch = new IWriteCompletionHandler() {
public void onWritten(int written) throws IOException {
System.out.println("written " + written);
}
public void onException(IOException ioe) {
ioe.printStackTrace();
}
};
ds.write(new ByteBuffer[] { DataConverter.toByteBuffer("0123456789", "UTF-8") }, ch);
QAUtil.sleep(50);
}
ds.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
IHttpResponse response = hdl.get();
Assert.assertEquals(200, response.getStatus());
httpClient.close();
server.close();
}