@Test
public void testHttpOutputNode() throws Exception
{

  final List<String> receivedMessages = new ArrayList<String>();
  Handler handler = new AbstractHandler()
  {
    @Override
    @Consumes({MediaType.APPLICATION_JSON})
    public void handle(String string, Request rq, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      IOUtils.copy(request.getInputStream(), bos);
      receivedMessages.add(new String(bos.toByteArray()));
      response.setContentType("text/html");
      response.setStatus(HttpServletResponse.SC_OK);
      response.getWriter().println("<h1>Thanks</h1>");
      ((Request)request).setHandled(true);
      receivedMessage = true;
    }
  };
  Server server = new Server(0);
  server.setHandler(handler);
  server.start();
  String url = "http://localhost:" + server.getConnectors()[0].getLocalPort() + "/somecontext";
  HttpPostOutputOperator<Object> node = new HttpPostOutputOperator<Object>();
  node.setUrl(url);
  node.setup(null);
  Map<String, String> data = new HashMap<String, String>();
  data.put("somekey", "somevalue");
  node.input.process(data);
  // Wait till the message is received or a maximum timeout elapses
  int timeoutMillis = 10000;
  while (!receivedMessage && timeoutMillis > 0) {
    timeoutMillis -= 20;
    Thread.sleep(20);
  }
  Assert.assertEquals("number requests", 1, receivedMessages.size());
  JSONObject json = new JSONObject(data);
  Assert.assertTrue("request body " + receivedMessages.get(0), receivedMessages.get(0).contains(json.toString()));
  receivedMessages.clear();
  String stringData = "stringData";
  node.input.process(stringData);
  Assert.assertEquals("number requests", 1, receivedMessages.size());
  Assert.assertEquals("request body " + receivedMessages.get(0), stringData, receivedMessages.get(0));
  node.teardown();
  server.stop();
}