final ByteSequence requestData = new ByteSequence();
requestData.rnd(reqNo);
List<ResponseSequence> responseData1 = new ArrayList<ResponseSequence>(connNo);
for (int i = 0; i < connNo; i++) {
responseData1.add(new ResponseSequence());
}
List<ResponseSequence> responseData2 = new ArrayList<ResponseSequence>(connNo);
for (int i = 0; i < connNo; i++) {
responseData2.add(new ResponseSequence());
}
final String[] method = new String[1];
NHttpRequestHandler requestHandler = new SimpleNHttpRequestHandler() {
public ConsumingNHttpEntity entityRequest(
final HttpEntityEnclosingRequest request,
final HttpContext context) throws HttpException, IOException {
return new BufferingNHttpEntity(
request.getEntity(),
new HeapByteBufferAllocator());
}
@Override
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
String s = request.getRequestLine().getUri();
URI uri;
try {
uri = new URI(s);
} catch (URISyntaxException ex) {
throw new HttpException("Invalid request URI: " + s);
}
int index = Integer.parseInt(uri.getQuery());
byte[] bytes = requestData.getBytes(index);
NByteArrayEntity entity = new NByteArrayEntity(bytes);
response.setEntity(entity);
}
};
NHttpRequestExecutionHandler requestExecutionHandler = new NHttpRequestExecutionHandler() {
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 ConsumingNHttpEntity responseEntity(
final HttpResponse response,
final HttpContext context) throws IOException {
return new BufferingNHttpEntity(response.getEntity(),
new HeapByteBufferAllocator());
}
public HttpRequest submitRequest(final HttpContext context) {
int i = ((Integer) context.getAttribute("REQ-COUNT")).intValue();
BasicHttpRequest request = null;
if (i < reqNo) {
request = new BasicHttpRequest(method[0], "/?" + i);
context.setAttribute("REQ-COUNT", Integer.valueOf(i + 1));
}
return request;
}
public void handleResponse(final HttpResponse response, final HttpContext context) {
NHttpConnection conn = (NHttpConnection) context.getAttribute(
ExecutionContext.HTTP_CONNECTION);
ResponseSequence list = (ResponseSequence) context.getAttribute("LIST");
int i = ((Integer) context.getAttribute("RES-COUNT")).intValue();
i++;
context.setAttribute("RES-COUNT", Integer.valueOf(i));
list.addResponse(response);
requestCount.decrement();
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();
method[0] = "GET";
for (int i = 0; i < responseData1.size(); i++) {
this.client.openConnection(
new InetSocketAddress("localhost", serverAddress.getPort()),
responseData1.get(i));
}
requestCount.await(connNo * reqNo, 10000);
assertEquals(connNo * reqNo, requestCount.getValue());
method[0] = "HEAD";
for (int i = 0; i < responseData2.size(); i++) {
this.client.openConnection(
new InetSocketAddress("localhost", serverAddress.getPort()),
responseData2.get(i));
}
requestCount.await(10000);
assertEquals(0, requestCount.getValue());
this.client.shutdown();
this.server.shutdown();
for (int c = 0; c < connNo; c++) {
ResponseSequence getResponses = responseData1.get(c);
ResponseSequence headResponses = responseData2.get(c);
ByteSequence expectedPackets = requestData;
assertEquals(expectedPackets.size(), headResponses.size());
assertEquals(expectedPackets.size(), getResponses.size());
for (int p = 0; p < requestData.size(); p++) {
HttpResponse getResponse = getResponses.getResponse(p);
HttpResponse headResponse = headResponses.getResponse(p);
assertEquals(null, headResponse.getEntity());
Header[] getHeaders = getResponse.getAllHeaders();
Header[] headHeaders = headResponse.getAllHeaders();
assertEquals(getHeaders.length, headHeaders.length);