if (initParam != null) {
w.addInitParameter(initParam, "true");
}
root.addServletMapping("/", "comet");
TesterAccessLogValve alv = new TesterAccessLogValve();
root.getPipeline().addValve(alv);
tomcat.start();
// Create connection to Comet servlet
final Socket socket =
SocketFactory.getDefault().createSocket("localhost", getPort());
socket.setSoTimeout(60000);
final OutputStream os = socket.getOutputStream();
String requestLine = "POST http://localhost:" + getPort() +
"/ HTTP/1.1\r\n";
os.write(requestLine.getBytes());
os.write("transfer-encoding: chunked\r\n".getBytes());
os.write("\r\n".getBytes());
PingWriterThread writeThread = new PingWriterThread(4, os);
writeThread.start();
socket.setSoTimeout(25000);
InputStream is = socket.getInputStream();
ResponseReaderThread readThread = new ResponseReaderThread(is);
readThread.start();
readThread.join();
os.close();
is.close();
String[] response = readThread.getResponse().split("\r\n");
if (initParam == null) {
// Normal response expected
// Validate response
assertEquals("HTTP/1.1 200 OK", response[0]);
assertEquals("Server: Apache-Coyote/1.1", response[1]);
assertTrue(response[2].startsWith("Set-Cookie: JSESSIONID="));
assertEquals("Content-Type: text/plain;charset=ISO-8859-1", response[3]);
assertEquals("Transfer-Encoding: chunked", response[4]);
assertTrue(response[5].startsWith("Date: "));
assertEquals("", response[6]);
assertEquals("7", response[7]);
assertEquals("BEGIN", response[8]);
assertEquals("", response[9]);
assertEquals("17", response[10]);
assertEquals("Client: READ: 4 bytes", response[11]);
assertEquals("", response[12]);
assertEquals("17", response[13]);
assertEquals("Client: READ: 4 bytes", response[14]);
assertEquals("", response[15]);
assertEquals("17", response[16]);
assertEquals("Client: READ: 4 bytes", response[17]);
assertEquals("", response[18]);
assertEquals("17", response[19]);
assertEquals("Client: READ: 4 bytes", response[20]);
assertEquals("", response[21]);
assertEquals("d", response[22]);
assertEquals("Client: END", response[23]);
assertEquals("", response[24]);
assertEquals("0", response[25]);
// Expect 26 lines
assertEquals(26, response.length);
} else {
// Failure expected only expected for the fail on begin
// Failure at any later stage and the response headers (including
// the 200 response code will already have been sent to the client
if (SimpleCometServlet.FAIL_ON_BEGIN.equals(initParam)) {
assertEquals("HTTP/1.1 500 Internal Server Error", response[0]);
alv.validateAccessLog(1, 500, 0, 1000);
} else {
assertEquals("HTTP/1.1 200 OK", response[0]);
alv.validateAccessLog(1, 200, 0, 5000);
}
}
}