From dd914db3cfa1766e0d9c786ca0c74767b6271009 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 22 Jul 2014 20:55:18 +0200 Subject: [PATCH] Reorganized GZIP tests. --- .../jetty/client/HttpClientGZIPTest.java | 77 ++++++++++++++++--- .../client/http/HttpReceiverOverHTTPTest.java | 43 ----------- 2 files changed, 65 insertions(+), 55 deletions(-) diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientGZIPTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientGZIPTest.java index e8b8cfa60b3..98aaf9966c6 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientGZIPTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientGZIPTest.java @@ -95,18 +95,6 @@ public class HttpClientGZIPTest extends AbstractHttpClientServerTest sleep(100); } } - - private void sleep(long ms) throws IOException - { - try - { - TimeUnit.MILLISECONDS.sleep(ms); - } - catch (InterruptedException x) - { - throw new InterruptedIOException(); - } - } }); ContentResponse response = client.newRequest("localhost", connector.getLocalPort()) @@ -153,4 +141,69 @@ public class HttpClientGZIPTest extends AbstractHttpClientServerTest System.arraycopy(data, 0, expected, data.length, data.length); Assert.assertArrayEquals(expected, response.getContent()); } + + @Test + public void testGZIPContentFragmentedBeforeTrailer() throws Exception + { + // There are 8 trailer bytes to gzip encoding. + testGZIPContentFragmented(9); + } + + @Test + public void testGZIPContentFragmentedAtTrailer() throws Exception + { + // There are 8 trailer bytes to gzip encoding. + testGZIPContentFragmented(1); + } + + private void testGZIPContentFragmented(final int fragment) throws Exception + { + final byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + start(new AbstractHandler() + { + @Override + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { + baseRequest.setHandled(true); + response.setHeader("Content-Encoding", "gzip"); + + ByteArrayOutputStream gzipData = new ByteArrayOutputStream(); + GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData); + gzipOutput.write(data); + gzipOutput.finish(); + + byte[] gzipBytes = gzipData.toByteArray(); + byte[] chunk1 = Arrays.copyOfRange(gzipBytes, 0, gzipBytes.length - fragment); + byte[] chunk2 = Arrays.copyOfRange(gzipBytes, gzipBytes.length - fragment, gzipBytes.length); + + ServletOutputStream output = response.getOutputStream(); + output.write(chunk1); + output.flush(); + + sleep(500); + + output.write(chunk2); + output.flush(); + } + }); + + ContentResponse response = client.newRequest("localhost", connector.getLocalPort()) + .scheme(scheme) + .send(); + + Assert.assertEquals(200, response.getStatus()); + Assert.assertArrayEquals(data, response.getContent()); + } + + private static void sleep(long ms) throws IOException + { + try + { + TimeUnit.MILLISECONDS.sleep(ms); + } + catch (InterruptedException x) + { + throw new InterruptedIOException(); + } + } } diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpReceiverOverHTTPTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpReceiverOverHTTPTest.java index 32db8dd5eaf..e7564b5e641 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpReceiverOverHTTPTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpReceiverOverHTTPTest.java @@ -18,22 +18,18 @@ package org.eclipse.jetty.client.http; -import java.io.ByteArrayOutputStream; import java.io.EOFException; -import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import java.util.zip.GZIPOutputStream; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpExchange; import org.eclipse.jetty.client.HttpRequest; import org.eclipse.jetty.client.HttpResponseException; import org.eclipse.jetty.client.Origin; -import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.util.FutureResponseListener; import org.eclipse.jetty.http.HttpFields; @@ -204,43 +200,4 @@ public class HttpReceiverOverHTTPTest Assert.assertTrue(e.getCause() instanceof HttpResponseException); } } - - @Test - public void test_Receive_GZIPResponseContent_Fragmented() throws Exception - { - byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (GZIPOutputStream gzipOutput = new GZIPOutputStream(baos)) - { - gzipOutput.write(data); - } - byte[] gzip = baos.toByteArray(); - - endPoint.setInput("" + - "HTTP/1.1 200 OK\r\n" + - "Content-Length: " + gzip.length + "\r\n" + - "Content-Encoding: gzip\r\n" + - "\r\n"); - HttpExchange exchange = newExchange(); - FutureResponseListener listener = (FutureResponseListener)exchange.getResponseListeners().get(0); - connection.getHttpChannel().receive(); - endPoint.reset(); - - ByteBuffer buffer = ByteBuffer.wrap(gzip); - int fragment = buffer.limit() - 1; - buffer.limit(fragment); - endPoint.setInput(buffer); - connection.getHttpChannel().receive(); - endPoint.reset(); - - buffer.limit(gzip.length); - buffer.position(fragment); - endPoint.setInput(buffer); - connection.getHttpChannel().receive(); - - ContentResponse response = listener.get(5, TimeUnit.SECONDS); - Assert.assertNotNull(response); - Assert.assertEquals(200, response.getStatus()); - Assert.assertArrayEquals(data, response.getContent()); - } }