ChannelWriter extends AtmosphereInterceptorWriter { protected String constructStatusAndHeaders(AtmosphereResponse response, int contentLength) { StringBuffer b = new StringBuffer("HTTP/1.1") .append(" ") .append(response.getStatus()) .append(" ") .append(response.getStatusMessage()) .append(CRLF); Map<String, String> headers = response.headers(); String contentType = response.getContentType(); b.append("Content-Type").append(":").append(headers.get("Content-Type") == null ? contentType : headers.get("Content-Type")).append(CRLF); if (contentLength != -1) { b.append("Content-Length").append(":").append(contentLength).append(CRLF); } for (String s : headers.keySet()) { if (!s.equalsIgnoreCase("Content-Type")) { b.append(s).append(":").append(headers.get(s)).append(CRLF); } } b.append(CRLF); return b.toString(); } ChannelWriter(Channel channel, boolean writeHeader, boolean keepAlive); boolean isClosed(); @Override AsyncIOWriter writeError(AtmosphereResponse response, int errorCode, String message); @Override AsyncIOWriter write(AtmosphereResponse response, String data); @Override AsyncIOWriter write(AtmosphereResponse response, byte[] data); @Override AsyncIOWriter write(AtmosphereResponse response, byte[] data, int offset, int length); long lastTick(); abstract AsyncIOWriter asyncWrite(AtmosphereResponse response, byte[] data, int offset, int length);  }
@Test public void testHeaders() throws Exception { AtmosphereResponse response = setupResponse(EMPTY_HEADERS); int contentLength = -1; String result = sut.constructStatusAndHeaders(response, contentLength); assertEquals("HTTP/1.1 200 OK\r\n" + "Content-Type:text/plain\r\n" + "\r\n", result); verifyInteractions(response); }
@Test public void testHeadersWithLength() throws Exception { AtmosphereResponse response = setupResponse(EMPTY_HEADERS); int contentLength = 10; String result = sut.constructStatusAndHeaders(response, contentLength); assertEquals("HTTP/1.1 200 OK\r\n" + "Content-Type:text/plain\r\n" + "Content-Length:" + contentLength + "\r\n" + "\r\n", result); verifyInteractions(response); }
@Test public void testCustomHeaders() throws Exception { Map<String, String> headers = new HashMap<String, String>(); headers.put("X-Custom-Header", "value"); AtmosphereResponse response = setupResponse(headers); int contentLength = 100; String result = sut.constructStatusAndHeaders(response, contentLength); assertEquals("HTTP/1.1 200 OK\r\n" + "Content-Type:text/plain\r\n" + "Content-Length:" + contentLength + "\r\n" + "X-Custom-Header:value\r\n" + "\r\n", result); verifyInteractions(response); }
@Test public void testCustomContentType() throws Exception { String contentType = "application/json"; Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", contentType); headers.put("X-Custom-Header", "value"); AtmosphereResponse response = setupResponse(headers); int contentLength = 100; String result = sut.constructStatusAndHeaders(response, contentLength); assertEquals("HTTP/1.1 200 OK\r\n" + "Content-Type:" + contentType + "\r\n" + "Content-Length:" + contentLength + "\r\n" + "X-Custom-Header:value\r\n" + "\r\n", result); verifyInteractions(response); }