Search in sources :

Example 1 with Response

use of org.apache.coyote.Response in project tomcat by apache.

the class TestGzipOutputFilter method testFlushingWithGzip.

/*
     * Test the interaction between gzip and flushing. The idea is to: 1. create
     * a internal output buffer, response, and attach an active gzipoutputfilter
     * to the output buffer 2. set the output stream of the internal buffer to
     * be a ByteArrayOutputStream so we can inspect the output bytes 3. write a
     * chunk out using the gzipoutputfilter and invoke a flush on the
     * InternalOutputBuffer 4. read from the ByteArrayOutputStream to find out
     * what's being written out (flushed) 5. find out what's expected by writing
     * to GZIPOutputStream and close it (to force flushing) 6. Compare the size
     * of the two arrays, they should be close (instead of one being much
     * shorter than the other one)
     *
     * @throws Exception
     */
@Test
public void testFlushingWithGzip() throws Exception {
    // set up response, InternalOutputBuffer, and ByteArrayOutputStream
    Response res = new Response();
    TesterOutputBuffer tob = new TesterOutputBuffer(res, 8 * 1024);
    res.setOutputBuffer(tob);
    // set up GzipOutputFilter to attach to the TesterOutputBuffer
    GzipOutputFilter gf = new GzipOutputFilter();
    tob.addFilter(gf);
    tob.addActiveFilter(gf);
    // write a chunk out
    byte[] d = "Hello there tomcat developers, there is a bug in JDK".getBytes();
    tob.doWrite(ByteBuffer.wrap(d));
    // flush the InternalOutputBuffer
    tob.flush();
    // read from the ByteArrayOutputStream to find out what's being written
    // out (flushed)
    byte[] dataFound = tob.toByteArray();
    // find out what's expected by writing to GZIPOutputStream and close it
    // (to force flushing)
    ByteArrayOutputStream gbos = new ByteArrayOutputStream(1024);
    GZIPOutputStream gos = new GZIPOutputStream(gbos);
    gos.write(d);
    gos.close();
    // read the expected data
    byte[] dataExpected = gbos.toByteArray();
    // most of the data should have been flushed out
    assertTrue(dataFound.length >= (dataExpected.length - 20));
}
Also used : Response(org.apache.coyote.Response) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Response (org.apache.coyote.Response)1 Test (org.junit.Test)1