@Test
 public void httpConnectionRunning() throws Exception {
   HttpConnection connection = new HttpConnection(this.request, this.response);
   assertThat(connection.isOlderThan(100), equalTo(false));
   Thread.sleep(200);
   assertThat(connection.isOlderThan(100), equalTo(true));
 }
 @Test
 public void httpConnectionRespondWithStatus() throws Exception {
   HttpConnection connection = new HttpConnection(this.request, this.response);
   connection.waitForResponse();
   connection.respond(HttpStatus.I_AM_A_TEAPOT);
   assertThat(this.servletResponse.getStatus(), equalTo(418));
   assertThat(this.servletResponse.getContentLength(), equalTo(0));
 }
 @Test
 public void httpConnectionRespondWithPayload() throws Exception {
   HttpConnection connection = new HttpConnection(this.request, this.response);
   connection.waitForResponse();
   connection.respond(new HttpTunnelPayload(1, ByteBuffer.wrap("hello".getBytes())));
   assertThat(this.servletResponse.getStatus(), equalTo(200));
   assertThat(this.servletResponse.getContentAsString(), equalTo("hello"));
   assertThat(this.servletResponse.getHeader(SEQ_HEADER), equalTo("1"));
 }
 @Test
 public void httpConnectionAsync() throws Exception {
   ServerHttpAsyncRequestControl async = mock(ServerHttpAsyncRequestControl.class);
   ServerHttpRequest request = mock(ServerHttpRequest.class);
   given(request.getAsyncRequestControl(this.response)).willReturn(async);
   HttpConnection connection = new HttpConnection(request, this.response);
   connection.waitForResponse();
   verify(async).start();
   connection.respond(HttpStatus.NO_CONTENT);
   verify(async).complete();
 }
  private void testHttpConnectionNonAsync(long sleepBeforeResponse)
      throws IOException, InterruptedException {
    ServerHttpRequest request = mock(ServerHttpRequest.class);
    given(request.getAsyncRequestControl(this.response)).willThrow(new IllegalArgumentException());
    final HttpConnection connection = new HttpConnection(request, this.response);
    final AtomicBoolean responded = new AtomicBoolean();
    Thread connectionThread =
        new Thread() {

          @Override
          public void run() {
            connection.waitForResponse();
            responded.set(true);
          }
        };
    connectionThread.start();
    assertThat(responded.get(), equalTo(false));
    Thread.sleep(sleepBeforeResponse);
    connection.respond(HttpStatus.NO_CONTENT);
    connectionThread.join();
    assertThat(responded.get(), equalTo(true));
  }
 @Override
 protected void complete() {
   super.complete();
   getServletResponse().setCommitted(true);
 }