Added test case to show how to handle zip content in AsyncMiddleManServlet. (#9986)
Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
parent
7352653440
commit
af9d8d3a32
|
@ -13,6 +13,7 @@
|
|||
|
||||
package org.eclipse.jetty.proxy;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
@ -35,11 +36,15 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
|
@ -1551,6 +1556,82 @@ public class AsyncMiddleManServletTest
|
|||
assertTrue(response.getHeaders().contains(PROXIED_HEADER));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDownstreamTransformationZipped() throws Exception
|
||||
{
|
||||
byte[] content1 = new byte[1024 * 1024];
|
||||
ThreadLocalRandom.current().nextBytes(content1);
|
||||
byte[] content2 = new byte[512 * 1024];
|
||||
ThreadLocalRandom.current().nextBytes(content2);
|
||||
|
||||
startServer(new HttpServlet()
|
||||
{
|
||||
@Override
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||
{
|
||||
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "deflate");
|
||||
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
|
||||
zipOut.putNextEntry(new ZipEntry("server-file1.rnd"));
|
||||
zipOut.write(content1);
|
||||
zipOut.closeEntry();
|
||||
zipOut.putNextEntry(new ZipEntry("server-file2.rnd"));
|
||||
zipOut.write(content2);
|
||||
zipOut.closeEntry();
|
||||
zipOut.finish();
|
||||
}
|
||||
});
|
||||
startProxy(new AsyncMiddleManServlet()
|
||||
{
|
||||
@Override
|
||||
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse)
|
||||
{
|
||||
return new AfterContentTransformer()
|
||||
{
|
||||
@Override
|
||||
public boolean transform(Source source, Sink sink) throws IOException
|
||||
{
|
||||
// Un-zip and re-zip.
|
||||
|
||||
ZipInputStream zipIn = new ZipInputStream(source.getInputStream());
|
||||
ZipOutputStream zipOut = new ZipOutputStream(sink.getOutputStream());
|
||||
|
||||
ZipEntry zipEntry;
|
||||
while ((zipEntry = zipIn.getNextEntry()) != null)
|
||||
{
|
||||
zipOut.putNextEntry(zipEntry);
|
||||
IO.copy(zipIn, zipOut);
|
||||
zipOut.closeEntry();
|
||||
}
|
||||
|
||||
zipOut.finish();
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
startClient();
|
||||
|
||||
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort())
|
||||
.timeout(5, TimeUnit.SECONDS)
|
||||
.send();
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
|
||||
ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(response.getContent()));
|
||||
ZipEntry zipEntry1 = zipIn.getNextEntry();
|
||||
assertNotNull(zipEntry1);
|
||||
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
|
||||
IO.copy(zipIn, out1);
|
||||
assertArrayEquals(content1, out1.toByteArray());
|
||||
ZipEntry zipEntry2 = zipIn.getNextEntry();
|
||||
assertNotNull(zipEntry2);
|
||||
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
|
||||
IO.copy(zipIn, out2);
|
||||
assertArrayEquals(content2, out2.toByteArray());
|
||||
assertNull(zipIn.getNextEntry());
|
||||
}
|
||||
|
||||
private void sleep(long delay)
|
||||
{
|
||||
try
|
||||
|
|
Loading…
Reference in New Issue