*/
public void testHttpPostsWithExpectContinue() throws Exception {
final int connNo = 3;
final int reqNo = 20;
final RequestCount requestCount = new RequestCount(connNo * reqNo);
final ByteSequence requestData = new ByteSequence();
requestData.rnd(reqNo);
List<ByteSequence> responseData = new ArrayList<ByteSequence>(connNo);
for (int i = 0; i < connNo; i++) {
responseData.add(new ByteSequence());
}
HttpRequestHandler requestHandler = new HttpRequestHandler() {
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
byte[] data = EntityUtils.toByteArray(incoming);
NByteArrayEntity outgoing = new NByteArrayEntity(data);
outgoing.setChunked(true);
response.setEntity(outgoing);
} else {
NStringEntity outgoing = new NStringEntity("No content");
response.setEntity(outgoing);
}
}
};
// Activate 'expect: continue' handshake
this.client.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
HttpRequestExecutionHandler requestExecutionHandler = new HttpRequestExecutionHandler() {
public void initalizeContext(final HttpContext context, final Object attachment) {
context.setAttribute("LIST", attachment);
context.setAttribute("REQ-COUNT", Integer.valueOf(0));
context.setAttribute("RES-COUNT", Integer.valueOf(0));
}
public void finalizeContext(final HttpContext context) {
}
public HttpRequest submitRequest(final HttpContext context) {
int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
BasicHttpEntityEnclosingRequest post = null;
if (i < reqNo) {
post = new BasicHttpEntityEnclosingRequest("POST", "/?" + i);
byte[] data = requestData.getBytes(i);
NByteArrayEntity outgoing = new NByteArrayEntity(data);
outgoing.setChunked(true);
post.setEntity(outgoing);
context.setAttribute("REQ-COUNT", Integer.valueOf(i + 1));
}
return post;
}
public void handleResponse(final HttpResponse response, final HttpContext context) {
NHttpConnection conn = (NHttpConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
ByteSequence list = (ByteSequence) context.getAttribute("LIST");
int i = ((Integer) context.getAttribute("RES-COUNT")).intValue();
i++;
context.setAttribute("RES-COUNT", Integer.valueOf(i));
try {
HttpEntity entity = response.getEntity();
byte[] data = EntityUtils.toByteArray(entity);
list.addBytes(data);
requestCount.decrement();
} catch (IOException ex) {
requestCount.abort();
return;
}
if (i < reqNo) {
conn.requestInput();
}
}
};
NHttpServiceHandler serviceHandler = createHttpServiceHandler(
requestHandler,
null);
NHttpClientHandler clientHandler = createHttpClientHandler(
requestExecutionHandler);
this.server.setRequestCount(requestCount);
this.client.setRequestCount(requestCount);
this.server.start(serviceHandler);
this.client.start(clientHandler);
ListenerEndpoint endpoint = this.server.getListenerEndpoint();
endpoint.waitFor();
InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
for (int i = 0; i < responseData.size(); i++) {
this.client.openConnection(
new InetSocketAddress("localhost", serverAddress.getPort()),
responseData.get(i));
}
requestCount.await(10000);
assertEquals(0, requestCount.getValue());
this.client.shutdown();
this.server.shutdown();
for (int c = 0; c < responseData.size(); c++) {