String basepath = file.getParentFile().getAbsolutePath();
IServer server = new HttpServer(new FileServiceRequestHandler(basepath, true));
server.start();
IBlockingConnection con = new BlockingConnection("localhost", server.getLocalPort());
con.write("GET /" + file.getName() + " HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"User-Agent: me\r\n" +
"\r\n");
String header = con.readStringByDelimiter("\r\n\r\n") + "\r\n";
if (header.indexOf("200") == -1) {
String txt = "unexpected response " + header;
System.out.println("ERROR " + txt);
Assert.fail(txt);
}
int contentLength = QAUtil.readContentLength(header);
File tempFile = QAUtil.createTempfile();
RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
FileChannel fc = raf.getChannel();
con.transferTo(fc, contentLength);
fc.close();
raf.close();
Assert.assertTrue(QAUtil.isEquals(file, tempFile));
tempFile.delete();
file.delete();
con.close();
server.close();
}