AsyncResponseSerializer { public InputStream serialize(final ODataResponse response) throws SerializerException { try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); appendStatusLine(response, buffer); appendResponseHeader(response, buffer); append(CRLF, buffer); appendBody(response, buffer); buffer.flush(); return new ByteArrayInputStream(buffer.toByteArray(), 0, buffer.size()); } catch (IOException e) { throw new SerializerException("Exception occurred during serialization of asynchronous response.", e, SerializerException.MessageKeys.IO_EXCEPTION); } }  InputStream serialize(final ODataResponse response);  }
@Test public void simpleResponse() throws Exception { ODataResponse response = new ODataResponse(); response.setStatusCode(HttpStatusCode.OK.getStatusCode()); response.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON.toContentTypeString()); response.setHeader(HttpHeader.CONTENT_LENGTH, String.valueOf(200)); response.setContent(IOUtils.toInputStream("Walter Winter" + CRLF)); AsyncResponseSerializer serializer = new AsyncResponseSerializer(); InputStream in = serializer.serialize(response); String result = IOUtils.toString(in); assertEquals("HTTP/1.1 200 OK" + CRLF + "Content-Type: application/json" + CRLF + "Content-Length: 200" + CRLF + CRLF + "Walter Winter" + CRLF, result); }
@Test public void biggerResponse() throws Exception { ODataResponse response = new ODataResponse(); response.setStatusCode(HttpStatusCode.ACCEPTED.getStatusCode()); response.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON.toContentTypeString()); response.setHeader(HttpHeader.CONTENT_LENGTH, String.valueOf(0)); String testData = testData(20000); response.setContent(IOUtils.toInputStream(testData)); AsyncResponseSerializer serializer = new AsyncResponseSerializer(); InputStream in = serializer.serialize(response); String result = IOUtils.toString(in); assertEquals("HTTP/1.1 202 Accepted" + CRLF + "Content-Type: application/json" + CRLF + "Content-Length: 0" + CRLF + CRLF + testData, result); }