final IServer server = new HttpServer(new EchoHandler());
server.start();
IHttpClientEndpoint httpClient = new HttpClient();
FutureResponseHandler hdl = new FutureResponseHandler();
HttpRequestHeader header = new HttpRequestHeader("POST", "http://localhost:" + server.getLocalPort() + "/");
header.setContentType("text/plain; charset=UTF-8");
BodyDataSink bodyDataSink = httpClient.send(header, hdl);
Assert.assertTrue("flushmode is not sync", bodyDataSink.getFlushmode() == FlushMode.SYNC);
File file = QAUtil.createTestfile_40k();
InputStream is = new FileInputStream(file);
System.out.println("got inputstream " + is);
int chunkSize = 64;
int count = 1;
do {
byte[] chunk = new byte[chunkSize];
count = is.read(chunk);
if (count > 0) {
if (count < chunkSize) {
byte[] newArray = new byte[count];
System.arraycopy(chunk, 0, newArray, 0, count);
chunk = newArray;
}
try {
bodyDataSink.write(chunk);
bodyDataSink.flush();
} catch (Exception e) {
System.out.println("error occured by writing chunk " + e.toString());
throw e;
}
}
} while (count >= 0);
bodyDataSink.close();
System.out.println("close bodyDataSink");
IHttpResponse response = hdl.getResponse();
if (!response.hasBody()) {
System.out.println("response should have a body");
Assert.fail("response should have a body");
}
String body = response.getBlockingBody().readString();
file.delete();
server.close();
httpClient.close();
if (body.indexOf("</html>") == -1) {
System.out.println("incomplete or wrong body");
Assert.fail("incomplete or wrong body");
}