Projekt von de.halbekunst.juplo nach de.juplo verschoben und aufgeräumt
[percentcodec] / accelerator / src / test / java / de / juplo / accelerator / RequestSizeTest.java
diff --git a/accelerator/src/test/java/de/juplo/accelerator/RequestSizeTest.java b/accelerator/src/test/java/de/juplo/accelerator/RequestSizeTest.java
new file mode 100644 (file)
index 0000000..6b94d2b
--- /dev/null
@@ -0,0 +1,237 @@
+package de.juplo.accelerator;
+
+import com.meterware.httpunit.WebResponse;
+import de.juplo.testingtools.HttpTestCase;
+import de.juplo.testingtools.LoggingHttpServletResponseWrapper;
+import java.net.URLEncoder;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import java.text.SimpleDateFormat;
+import java.util.Locale;
+import org.junit.Assert;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+
+
+/**
+ *
+ * @author kai
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = {
+  "classpath:/config.xml"
+})
+public class RequestSizeTest extends HttpTestCase {
+  private final static Logger log = LoggerFactory.getLogger(RequestSizeTest.class);
+
+
+  public RequestSizeTest() {
+    super("src/test/resources/web.xml");
+  }
+
+
+  @Test
+  public void testSimpleRequestWithGzip() throws Exception {
+
+    log.info("-------- Test: gzipped simple request");
+
+    client.getClientProperties().setAcceptGzip(true);
+
+    for (int i=0; i<33; i++) {
+      /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
+      WebResponse response = executeRequest("http://localhost/request-size?n=" + i*128);
+      Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
+      Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
+      if (i==0)
+        Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
+      else
+        Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
+      Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
+      Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
+      Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
+      Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
+      SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
+      long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
+      long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
+      Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
+    }
+  }
+
+  @Test
+  public void testSimpleRequestWithoutGzip() throws Exception {
+
+    log.info("-------- Test: uncompressed simple request");
+
+    client.getClientProperties().setAcceptGzip(false);
+
+    for (int i=0; i<33; i++) {
+      /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
+      WebResponse response = executeRequest("http://localhost/request-size?n=" + i*128);
+      Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
+      Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
+      Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
+      Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
+      Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
+      Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
+      Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
+      SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
+      long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
+      long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
+      Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
+    }
+  }
+
+  @Test
+  public void testForwardWithGzip() throws Exception {
+
+    log.info("-------- Test: gzipped request with forward");
+
+    client.getClientProperties().setAcceptGzip(true);
+
+    /**
+     * Auf den Fehler bei einem Forward nach Überschreitung der Puffer-Größe
+     * des ursprünglichen Requests wird hier nicht geprüft, weil der Puffer
+     * durch die Komprimierung bei den hier gewählten Test-Parametern nie
+     * vollgeschrieben wird, so dass er stets ohne Fehler zurückgesetzt
+     * werden kann...
+     *
+     * Dafür wird hier zusätzlich geprüft, ob die Komprimierung korrekt nur
+     * dann unterdrückt wird, wenn die gesamte Antwort leer ist (und nicht nur
+     * der initiale Request, der geforwarded wird).
+     */
+    for (int i=0; i<33; i++) {
+      /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
+      StringBuilder uri = new StringBuilder();
+      uri.append("http://localhost/request-size");
+      uri.append("?n=");
+      uri.append(i%7*128);
+      uri.append("&f=");
+      uri.append(URLEncoder.encode("/forwarded?n=" + i*128, "UTF-8"));
+      WebResponse response = executeRequest(uri.toString());
+      Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
+      Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
+      if (i==0)
+        Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
+      else
+        Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
+      Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
+      Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
+      Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
+      Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
+      SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
+      long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
+      long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
+      Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
+    }
+  }
+
+  @Test
+  public void testForwardWithoutGzip() throws Exception {
+
+    log.info("-------- Test: uncompressed request with forward");
+
+    client.getClientProperties().setAcceptGzip(false);
+
+    for (int i=0; i<33; i++) {
+      /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
+      StringBuilder uri = new StringBuilder();
+      uri.append("http://localhost/request-size");
+      uri.append("?n=");
+      uri.append(i*128);
+      uri.append("&f=");
+      uri.append(URLEncoder.encode("/forwarded?n=" + i*128, "UTF-8"));
+      try {
+        WebResponse response = executeRequest(uri.toString());
+        if (i*128 > LoggingHttpServletResponseWrapper.DEFAULT_BUFFER_SIZE)
+          Assert.fail("Error expected while forwarding after " + i*128 + " bytes written!");
+        Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
+        Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
+        Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
+        Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
+        Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
+        Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
+        Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
+        SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
+        long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
+        long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
+        Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
+      }
+      catch (IllegalStateException e) {
+        if (i*128 > LoggingHttpServletResponseWrapper.DEFAULT_BUFFER_SIZE)
+          log.debug("Expected error while forwarding after {} bytes written: {}", i*128, e.getMessage());
+        else
+          Assert.fail("Unexpected error while forwarding after " + i*128 + " bytes written: " + e.getMessage());
+      }
+    }
+  }
+
+  @Test
+  public void testIncludeWithGzip() throws Exception {
+
+    log.info("-------- Test: gzipped request with includes");
+
+    client.getClientProperties().setAcceptGzip(true);
+
+    for (int i=0; i<33; i++) {
+      /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
+      StringBuilder uri = new StringBuilder();
+      uri.append("http://localhost/request-size");
+      uri.append("?n=");
+      uri.append(i%7*128);
+      for (int j=0; j < i%4+1; j++) {
+        uri.append("&i=");
+        uri.append(URLEncoder.encode("/included?n=" + i*32*(4-j), "UTF-8"));
+      }
+      WebResponse response = executeRequest(uri.toString());
+      Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
+      Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
+      if (i==0)
+        Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
+      else
+        Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
+      Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
+      Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
+      Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
+      Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
+      SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
+      long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
+      long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
+      Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
+    }
+  }
+
+  @Test
+  public void testIncludeWithoutGzip() throws Exception {
+
+    log.info("-------- Test: uncompressed request with includes");
+
+    client.getClientProperties().setAcceptGzip(false);
+
+    for (int i=0; i<33; i++) {
+      /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
+      StringBuilder uri = new StringBuilder();
+      uri.append("http://localhost/request-size");
+      uri.append("?n=");
+      uri.append(i%7*128);
+      for (int j=0; j < i%4+1; j++) {
+        uri.append("&i=");
+        uri.append(URLEncoder.encode("/included?n=" + i*32*(4-j), "UTF-8"));
+      }
+      WebResponse response = executeRequest(uri.toString());
+      Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
+      Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
+      Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
+      Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
+      Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
+      Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
+      Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
+      SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
+      long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
+      long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
+      Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
+    }
+  }
+}