@Test
  public void testSendMessageSimpleXml() throws Exception {

    String message = // -
        "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r"
            + // -
            "EVN||200803051509\r"
            + // -
            "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // -
    message =
        new DefaultXMLParser().encode(PipeParser.getInstanceWithNoValidation().parse(message));

    HohRawClientSimple client = new HohRawClientSimple("localhost", myPort, "/theUri");
    client.setAuthorizationCallback(new SingleCredentialClientCallback("hello", "hapiworld"));
    IReceivable<String> response = client.sendAndReceive(new RawSendable(message));

    ourLog.info("Received response");

    assertEquals(message, myServerSocketThread.getMessage());
    String responseMessage = response.getMessage();
    assertTrue(responseMessage, responseMessage.contains("<MSH>"));
    assertEquals(myServerSocketThread.getReply().encode(), responseMessage);

    assertEquals(EncodingStyle.XML.getContentType(), myServerSocketThread.getContentType());
    assertEquals(EncodingStyle.XML, myServerSocketThread.getEncoding());
  }
  /**
   * Ensure that if chunked transfer encoding is used, and there is a pause in the middle of
   * transmission, the whole message is still read
   */
  @Test
  public void testSendMessageWithChunkedResponseAndPauseInMiddleWithLongSoTimeout()
      throws Exception {

    myServerSocketThread.setSimulateOneSecondPauseInChunkedEncoding(true);

    String message = // -
        "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r"
            + // -
            "EVN||200803051509|||||||||||||||||||||||||||||||||||||||||\r"
            + // -
            "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // -

    HohRawClientSimple client = new HohRawClientSimple("localhost", myPort, "/theUri");
    client.setSoTimeout(980);
    client.setKeepAlive(false);
    client.setResponseTimeout(2000);
    client.setAuthorizationCallback(new SingleCredentialClientCallback("hello", "hapiworld"));
    IReceivable<String> response = client.sendAndReceive(new RawSendable(message));

    ourLog.info("Received response");

    assertEquals(message, myServerSocketThread.getMessage());
    assertEquals(myServerSocketThread.getReply().encode(), response.getMessage());

    assertEquals(EncodingStyle.ER7.getContentType(), myServerSocketThread.getContentType());
    assertEquals(EncodingStyle.ER7, myServerSocketThread.getEncoding());
  }
  @Test
  public void testUnderstandsGzippedResponse() throws Exception {
    LogManager.getRootLogger().setLevel((Level) Level.TRACE);

    myServerSocketThread.setGZipResponse(true);

    String message = // -
        "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r"
            + // -
            "EVN||200803051509\r"
            + // -
            "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // -

    HohRawClientSimple client = new HohRawClientSimple("localhost", myPort, "/theUri");
    client.setAuthorizationCallback(new SingleCredentialClientCallback("hello", "hapiworld"));
    IReceivable<String> response = client.sendAndReceive(new RawSendable(message));

    ourLog.info("Received response");

    assertEquals(message, myServerSocketThread.getMessage());
    assertEquals(myServerSocketThread.getReply().encode(), response.getMessage());

    assertEquals(EncodingStyle.ER7.getContentType(), myServerSocketThread.getContentType());
    assertEquals(EncodingStyle.ER7, myServerSocketThread.getEncoding());
  }
  @Before
  public void before() throws InterruptedException {
    myExistingLogLevel = LogManager.getRootLogger().getLevel();

    myPort = RandomServerPortProvider.findFreePort();

    myLlp = new Hl7OverHttpLowerLayerProtocol(ServerRoleEnum.CLIENT);
    myLlp.setAuthorizationCallback(new SingleCredentialClientCallback("hello", "hapiworld"));
    ourServerCallback = new SingleCredentialServerCallback("hello", "hapiworld");

    myServerSocketThread = new ServerSocketThreadForTesting(myPort, ourServerCallback);
    myServerSocketThread.start();
    myServerSocketThread.getLatch().await();
  }
  @After
  public void after() throws InterruptedException {
    ourLog.info("Marking done as true");
    myServerSocketThread.done();

    // Restore the log level
    LogManager.getRootLogger().setLevel(myExistingLogLevel);
  }
  @Test
  public void testSendMessageAndRespectCloseHeaderInResponse() throws Exception {

    String message = // -
        "MSH|^~\\&|||||200803051508||ADT^A31|2|P|2.5\r"
            + // -
            "EVN||200803051509\r"
            + // -
            "PID|||ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103\r"; // -

    HohRawClientSimple client = new HohRawClientSimple("localhost", myPort, "/theUri");

    client.setAuthorizationCallback(new SingleCredentialClientCallback("hello", "hapiworld"));

    myServerSocketThread.setCloseNormallyWithHeaderAfterEachMessage();

    /*
     * Send one message
     */
    ourLog.info("*** Send message #1");

    IReceivable<String> response = client.sendAndReceive(new RawSendable(message));
    assertEquals(message, myServerSocketThread.getMessage());
    assertEquals(myServerSocketThread.getReply().encode(), response.getMessage());

    assertEquals(EncodingStyle.ER7.getContentType(), myServerSocketThread.getContentType());
    assertEquals(EncodingStyle.ER7, myServerSocketThread.getEncoding());
    assertEquals(1, myServerSocketThread.getConnectionCount());

    /*
     * Send a second message
     */
    ourLog.info("*** Send message #2");

    response = client.sendAndReceive(new RawSendable(message));
    assertEquals(message, myServerSocketThread.getMessage());
    assertEquals(myServerSocketThread.getReply().encode(), response.getMessage());

    assertEquals(EncodingStyle.ER7.getContentType(), myServerSocketThread.getContentType());
    assertEquals(EncodingStyle.ER7, myServerSocketThread.getEncoding());
    assertEquals(2, myServerSocketThread.getConnectionCount());

    Thread.sleep(1000);

    /*
     * Send a third message
     */
    ourLog.info("*** Send message #3");

    response = client.sendAndReceive(new RawSendable(message));
    assertEquals(message, myServerSocketThread.getMessage());
    assertEquals(myServerSocketThread.getReply().encode(), response.getMessage());

    assertEquals(EncodingStyle.ER7.getContentType(), myServerSocketThread.getContentType());
    assertEquals(EncodingStyle.ER7, myServerSocketThread.getEncoding());
    assertEquals(3, myServerSocketThread.getConnectionCount());
  }