ConnectionHandler implements Runnable { HttpResponse prepareResponseForSending(HttpResponse response) { if (response.getHeader(HttpEntityHeader.CONTENT_LENGTH).isEmpty()) { response.addHeader(HttpEntityHeader.CONTENT_LENGTH, "0"); } if (response.getHeader(HttpResponseHeader.SERVER).isEmpty()) { response.addHeader(HttpResponseHeader.SERVER, server.getServerType()); } if (!connection.isPersistent()) { response.addHeader(HttpGeneralHeader.CONNECTION, "close"); } return response; } ConnectionHandler(HttpServer server, Connection connection); void run(); void handleRequests(); boolean isActive(); @Override String toString();  }
@Test public void prepareResponseForSending() throws Exception { MockConnection mc = new MockConnection(); ConnectionHandler ch = new ConnectionHandler(new MockHttpServer(), mc); assertTrue(mc.isPersistent()); HttpResponse r = new HttpResponse(); assertTrue(r.getHeader(HttpEntityHeader.CONTENT_LENGTH).isEmpty()); assertTrue(r.getHeader(HttpResponseHeader.SERVER).isEmpty()); HttpResponse r2 = ch.prepareResponseForSending(r); assertTrue(r2 == r); List<HttpHeader> hs = r.getHeader(HttpEntityHeader.CONTENT_LENGTH); assertEquals(1, hs.size()); HttpHeader h = hs.get(0); assertEquals("0", h.getFieldBody()); hs = r.getHeader(HttpResponseHeader.SERVER); assertEquals(1, hs.size()); h = hs.get(0); assertEquals("Mock", h.getFieldBody()); assertTrue(r.getHeader(HttpGeneralHeader.CONNECTION).isEmpty()); }
@Test public void prepareResponseForSending_NonPersistentConnection() throws Exception { MockConnection mc = new MockConnection(); ConnectionHandler ch = new ConnectionHandler(new MockHttpServer(), mc); mc.setPersistent(false); HttpResponse r = new HttpResponse(); HttpResponse r2 = ch.prepareResponseForSending(r); List<HttpHeader> hs = r2.getHeader(HttpGeneralHeader.CONNECTION); assertEquals(1, hs.size()); HttpHeader h = hs.get(0); assertEquals("close", h.getFieldBody()); }