ConnectionHandler implements Runnable { boolean processRequestResponsePair() throws ConnectionException { try { HttpRequest request = HttpRequest.readRequest(connection); if (request == null) { return false; } logRequest(request); preProcessRequest(request); HttpResponse response = passToHandler(request); response = prepareResponseForSending(response); sendResponse(response); manageConnection(); } catch (InvalidHttpMessageException e) { String msg = e.getMessage(); log.error(msg); log.debug("invalid HTTP request", e); sendResponse(new HttpResponse(HttpStatusCode.BAD_REQUEST, msg.getBytes())); } return true; } ConnectionHandler(HttpServer server, Connection connection); void run(); void handleRequests(); boolean isActive(); @Override String toString();  }
@Test public void processRequestResponsePair_ConnectionClosed() throws Exception { MockConnection mc = new MockConnection(0); mc.close(); assertTrue(mc.isClosed()); ConnectionHandler h = new ConnectionHandler(new MockHttpServer(), mc); boolean moreRequests = h.processRequestResponsePair(); assertFalse(moreRequests); }
@Test public void processRequestResponsePair_InputStreamContainsDiscardables() throws Exception { MockConnection mc = new MockConnection(0, " \r\n"); ConnectionHandler h = new ConnectionHandler(new MockHttpServer(), mc); boolean moreRequests = h.processRequestResponsePair(); assertFalse(moreRequests); }
@Test public void processRequestResponsePair_InvalidRequest() throws Exception { MockConnection mc = new MockConnection(0, "NO_SUCH_METHOD / HTTP/1.1\r\n\r\n"); ConnectionHandler h = new ConnectionHandler(new MockHttpServer(), mc); boolean moreRequests = h.processRequestResponsePair(); assertTrue(moreRequests); }
@Test public void processRequestResponsePair() throws Exception { MockConnection mc = new MockConnection(0, "HTTP/1.1 GET /\r\n\r\n"); ConnectionHandler h = new ConnectionHandler(new MockHttpServer(), mc); boolean moreRequests = h.processRequestResponsePair(); assertTrue(moreRequests); }