* over multiple connections that do not meet the target server expectations.
*/
public void testHttpPostsWithExpectationVerification() throws Exception {
final int reqNo = 3;
final RequestCount requestCount = new RequestCount(reqNo);
final ResponseSequence responses = new ResponseSequence();
HttpRequestHandler requestHandler = new HttpRequestHandler() {
public void handle(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
StringEntity outgoing = new StringEntity("No content");
response.setEntity(outgoing);
}
};
HttpExpectationVerifier expectationVerifier = new HttpExpectationVerifier() {
public void verify(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws HttpException {
Header someheader = request.getFirstHeader("Secret");
if (someheader != null) {
int secretNumber;
try {
secretNumber = Integer.parseInt(someheader.getValue());
} catch (NumberFormatException ex) {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
return;
}
if (secretNumber < 2) {
response.setStatusCode(HttpStatus.SC_EXPECTATION_FAILED);
ByteArrayEntity outgoing = new ByteArrayEntity(
EncodingUtils.getAsciiBytes("Wrong secret number"));
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", (ResponseSequence) attachment);
context.setAttribute("REQ-COUNT", new Integer(0));
context.setAttribute("RES-COUNT", new Integer(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", "/");
post.addHeader("Secret", Integer.toString(i));
ByteArrayEntity outgoing = new ByteArrayEntity(
EncodingUtils.getAsciiBytes("No content"));
post.setEntity(outgoing);
context.setAttribute("REQ-COUNT", new Integer(i + 1));
}
return post;
}
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", new Integer(i));
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
entity.consumeContent();
} catch (IOException ex) {
requestCount.abort();
return;
}
}
list.addResponse(response);
requestCount.decrement();
if (i < reqNo) {
conn.requestInput();
}
}
};
NHttpServiceHandler serviceHandler = createHttpServiceHandler(
requestHandler,
expectationVerifier);
NHttpClientHandler clientHandler = createHttpClientHandler(
requestExecutionHandler);
this.server.start(serviceHandler);
this.client.start(clientHandler);
ListenerEndpoint endpoint = this.server.getListenerEndpoint();
endpoint.waitFor();
InetSocketAddress serverAddress = (InetSocketAddress) endpoint.getAddress();
this.client.openConnection(
new InetSocketAddress("localhost", serverAddress.getPort()),
responses);
requestCount.await(1000);
this.client.shutdown();
this.server.shutdown();
assertEquals(reqNo, responses.size());