aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Becker2012-04-25 07:08:47 (EDT)
committerThomas Becker2012-04-25 07:08:47 (EDT)
commit8e4f2bb4121966b3e3b73d2b1ac64a3d6bfd87ff (patch)
tree5334e9108c4625ab24794f2abb595cb6709a3eeb
parentfbac246a41656a4bdeb8d74d6e9d7c4deca9bb3b (diff)
downloadorg.eclipse.jetty.project-8e4f2bb4121966b3e3b73d2b1ac64a3d6bfd87ff.zip
org.eclipse.jetty.project-8e4f2bb4121966b3e3b73d2b1ac64a3d6bfd87ff.tar.gz
org.eclipse.jetty.project-8e4f2bb4121966b3e3b73d2b1ac64a3d6bfd87ff.tar.bz2
New test for GzipFilter that tests writing a text message to the outputstream and setting the status code afterwardsrefs/changes/59/5659/1
-rw-r--r--jetty-http/src/main/java/org/eclipse/jetty/http/gzip/AbstractCompressedStream.java2
-rw-r--r--jetty-http/src/main/java/org/eclipse/jetty/http/gzip/CompressedResponseWrapper.java6
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java45
-rw-r--r--jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java132
4 files changed, 133 insertions, 52 deletions
diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/AbstractCompressedStream.java b/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/AbstractCompressedStream.java
index 77ac1fe..39aaa99 100644
--- a/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/AbstractCompressedStream.java
+++ b/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/AbstractCompressedStream.java
@@ -260,7 +260,7 @@ public abstract class AbstractCompressedStream extends ServletOutputStream
public void doNotCompress() throws IOException
{
if (_compressedOutputStream != null)
- throw new IllegalStateException();
+ throw new IllegalStateException("Compressed output stream is already assigned.");
if (_out == null || _bOut != null)
{
_doNotCompress = true;
diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/CompressedResponseWrapper.java b/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/CompressedResponseWrapper.java
index 1431f38..e439a9d 100644
--- a/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/CompressedResponseWrapper.java
+++ b/jetty-http/src/main/java/org/eclipse/jetty/http/gzip/CompressedResponseWrapper.java
@@ -124,7 +124,7 @@ public abstract class CompressedResponseWrapper extends HttpServletResponseWrapp
public void setStatus(int sc)
{
super.setStatus(sc);
- if (sc<200 || sc==204 || sc==205 ||sc>=300)
+ if (sc<200 || sc==204 || sc==205 || sc>=300)
noCompression();
}
@@ -344,7 +344,7 @@ public abstract class CompressedResponseWrapper extends HttpServletResponseWrapp
else if (_writer!=null)
throw new IllegalStateException("getWriter() called");
- return (ServletOutputStream)_compressedStream;
+ return _compressedStream;
}
/* ------------------------------------------------------------ */
@@ -366,7 +366,7 @@ public abstract class CompressedResponseWrapper extends HttpServletResponseWrapp
}
_compressedStream=newCompressedStream(_request,(HttpServletResponse)getResponse(),_contentLength,_bufferSize,_minCompressSize);
- _writer=newWriter((OutputStream)_compressedStream,getCharacterEncoding());
+ _writer=newWriter(_compressedStream,getCharacterEncoding());
}
return _writer;
}
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java
index 05f53c9..8fd8a03 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java
@@ -45,11 +45,8 @@ public class GzipFilterDefaultTest
this.compressionType = compressionType;
}
-
public static class HttpStatusServlet extends HttpServlet
{
- private static final long serialVersionUID = 1L;
-
private int _status = 204;
public HttpStatusServlet()
@@ -57,18 +54,31 @@ public class GzipFilterDefaultTest
super();
}
- public void setStatus (int status)
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ resp.setStatus(_status);
+ }
+
+ }
+
+ public static class HttpErrorServlet extends HttpServlet
+ {
+ private int _status = 400;
+
+ public HttpErrorServlet()
{
- _status = status;
+ super();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
+ resp.getOutputStream().write("error message".getBytes());
resp.setStatus(_status);
}
-
}
+
@Rule
public TestingDir testingdir = new TestingDir();
@@ -153,7 +163,7 @@ public class GzipFilterDefaultTest
try
{
tester.start();
- tester.assertIsResponseNotGzipCompressed(null, -1, 204);
+ tester.assertIsResponseNotGzipCompressed(-1, 204);
}
finally
{
@@ -161,6 +171,27 @@ public class GzipFilterDefaultTest
}
}
+
+ @Test
+ public void testIsNotGzipCompressedHttpBadRequestStatus() throws Exception
+ {
+ GzipTester tester = new GzipTester(testingdir, compressionType);
+
+ // Test error code 400
+ FilterHolder holder = tester.setContentServlet(HttpErrorServlet.class);
+ holder.setInitParameter("mimeTypes","text/plain");
+
+ try
+ {
+ tester.start();
+ tester.assertIsResponseNotGzipCompressedAndEqualToExpectedString("error message", -1, 400);
+ }
+ finally
+ {
+ tester.stop();
+ }
+
+ }
@Test
public void testUserAgentExclusion() throws Exception
diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java
index 331975c..9fb1720 100644
--- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java
+++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java
@@ -14,6 +14,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.util.Enumeration;
@@ -81,7 +82,7 @@ public class GzipTester
// Assert the response headers
Assert.assertThat("Response.method",response.getMethod(),nullValue());
- Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK));
+// Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK));
Assert.assertThat("Response.header[Content-Length]",response.getHeader("Content-Length"),notNullValue());
Assert.assertThat("Response.header[Content-Encoding]",response.getHeader("Content-Encoding"),containsString(compressionType));
@@ -222,67 +223,116 @@ public class GzipTester
*/
public void assertIsResponseNotGzipCompressed(String filename, int expectedFilesize, int status) throws Exception
{
- System.err.printf("[GzipTester] requesting /context/%s%n",filename);
+ String uri = "/context/"+filename;
+ HttpTester response = executeRequest(uri);
+ assertResponseHeaders(expectedFilesize,status,response);
+
+ // Assert that the contents are what we expect.
+ if (filename != null)
+ {
+ File serverFile = testdir.getFile(filename);
+ String expectedResponse = IO.readToString(serverFile);
+
+ String actual = readResponse(response);
+ Assert.assertEquals("Expected response equals actual response",expectedResponse,actual);
+ }
+ }
+
+ /**
+ * Asserts that the request results in a properly structured GzipFilter response, where the content is
+ * not compressed, and the content-length is returned appropriately.
+ *
+ * @param expectedResponse
+ * the expected response body string
+ * @param expectedFilesize
+ * the expected filesize to be specified on the Content-Length portion of the response headers. (note:
+ * passing -1 will disable the Content-Length assertion)
+ * @throws Exception
+ */
+ public void assertIsResponseNotGzipCompressedAndEqualToExpectedString(String expectedResponse, int expectedFilesize, int status) throws Exception
+ {
+ String uri = "/context/";
+ HttpTester response = executeRequest(uri);
+ assertResponseHeaders(expectedFilesize,status,response);
+
+ String actual = readResponse(response);
+ Assert.assertEquals("Expected response equals actual response",expectedResponse,actual);
+ }
+
+ /**
+ * Asserts that the request results in a properly structured GzipFilter response, where the content is
+ * not compressed, and the content-length is returned appropriately.
+ *
+ * @param expectedFilesize
+ * the expected filesize to be specified on the Content-Length portion of the response headers. (note:
+ * passing -1 will disable the Content-Length assertion)
+ * @throws Exception
+ */
+ public void assertIsResponseNotGzipCompressed(int expectedFilesize, int status) throws Exception
+ {
+ String uri = "/context/";
+ HttpTester response = executeRequest(uri);
+ assertResponseHeaders(expectedFilesize,status,response);
+ }
+
+ private void assertResponseHeaders(int expectedFilesize, int status, HttpTester response)
+ {
+ Assert.assertThat("Response.method",response.getMethod(),nullValue());
+ Assert.assertThat("Response.status",response.getStatus(),is(status));
+ if (expectedFilesize != (-1))
+ {
+ Assert.assertThat("Response.header[Content-Length]",response.getHeader("Content-Length"),notNullValue());
+ int serverLength = Integer.parseInt(response.getHeader("Content-Length"));
+ Assert.assertThat("Response.header[Content-Length]",serverLength,is(expectedFilesize));
+ }
+ Assert.assertThat("Response.header[Content-Encoding]",response.getHeader("Content-Encoding"),not(containsString(compressionType)));
+ }
+
+ private HttpTester executeRequest(String uri) throws IOException, Exception
+ {
+ System.err.printf("[GzipTester] requesting %s%n",uri);
HttpTester request = new HttpTester();
HttpTester response = new HttpTester();
-
+
request.setMethod("GET");
request.setVersion("HTTP/1.0");
request.setHeader("Host","tester");
request.setHeader("Accept-Encoding",compressionType);
if (this.userAgent != null)
request.setHeader("User-Agent", this.userAgent);
- if (filename == null)
- request.setURI("/context/");
- else
- request.setURI("/context/"+filename);
-
+
+ request.setURI(uri);
+
// Issue the request
ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes());
// Collect the response(s)
ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff);
response.parse(respBuff.asArray());
+ return response;
+ }
- // Assert the response headers
- Assert.assertThat("Response.method",response.getMethod(),nullValue());
- Assert.assertThat("Response.status",response.getStatus(),is(status));
- if (expectedFilesize != (-1))
+ private String readResponse(HttpTester response) throws IOException, UnsupportedEncodingException
+ {
+ String actual = null;
+ InputStream in = null;
+ ByteArrayOutputStream out = null;
+ try
{
- Assert.assertThat("Response.header[Content-Length]",response.getHeader("Content-Length"),notNullValue());
- int serverLength = Integer.parseInt(response.getHeader("Content-Length"));
- Assert.assertThat("Response.header[Content-Length]",serverLength,is(expectedFilesize));
- }
- Assert.assertThat("Response.header[Content-Encoding]",response.getHeader("Content-Encoding"),not(containsString(compressionType)));
+ in = new ByteArrayInputStream(response.getContentBytes());
+ out = new ByteArrayOutputStream();
+ IO.copy(in,out);
- // Assert that the contents are what we expect.
- if (filename != null)
+ actual = out.toString(encoding);
+ }
+ finally
{
- File serverFile = testdir.getFile(filename);
- String expected = IO.readToString(serverFile);
- String actual = null;
-
- InputStream in = null;
- ByteArrayOutputStream out = null;
- try
- {
- in = new ByteArrayInputStream(response.getContentBytes());
- out = new ByteArrayOutputStream();
- IO.copy(in,out);
-
- actual = out.toString(encoding);
- Assert.assertEquals("Server contents",expected,actual);
- }
- finally
- {
- IO.close(out);
- IO.close(in);
- }
+ IO.close(out);
+ IO.close(in);
}
+ return actual;
}
-
-
/**
* Generate string content of arbitrary length.
*