/** * Makes sure that the response contains an unfiltered file contents. * * <p>This is used to test exclusions and passthroughs in the GzipFilter. * * <p>An example is to test that it is possible to configure GzipFilter to not recompress content * that shouldn't be compressed by the GzipFilter. * * @param requestedFilename the filename used to on the GET request,. * @param testResourceSha1Sum the sha1sum file that contains the SHA1SUM checksum that will be * used to verify that the response contents are what is intended. * @param expectedContentType */ public void assertIsResponseNotGzipFiltered( String requestedFilename, String testResourceSha1Sum, String expectedContentType) throws Exception { System.err.printf("[GzipTester] requesting /context/%s%n", requestedFilename); 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); request.setURI("/context/" + requestedFilename); // Issue the request ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes()); // Collect the response(s) ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff); response.parse(respBuff.asArray()); dumpHeaders(requestedFilename + " / Response Headers", response); // Assert the response headers String prefix = requestedFilename + " / Response"; Assert.assertThat(prefix + ".method", response.getMethod(), nullValue()); Assert.assertThat(prefix + ".status", response.getStatus(), is(HttpServletResponse.SC_OK)); Assert.assertThat( prefix + ".header[Content-Length]", response.getHeader("Content-Length"), notNullValue()); Assert.assertThat( prefix + ".header[Content-Encoding] (should not be recompressed by GzipFilter)", response.getHeader("Content-Encoding"), nullValue()); Assert.assertThat( prefix + ".header[Content-Type] (should have a Content-Type associated with it)", response.getHeader("Content-Type"), notNullValue()); Assert.assertThat( prefix + ".header[Content-Type]", response.getHeader("Content-Type"), is(expectedContentType)); ByteArrayInputStream bais = null; DigestOutputStream digester = null; try { MessageDigest digest = MessageDigest.getInstance("SHA1"); bais = new ByteArrayInputStream(response.getContentBytes()); digester = new DigestOutputStream(new NoOpOutputStream(), digest); IO.copy(bais, digester); String actualSha1Sum = Hex.asHex(digest.digest()); String expectedSha1Sum = loadExpectedSha1Sum(testResourceSha1Sum); Assert.assertEquals( requestedFilename + " / SHA1Sum of content", expectedSha1Sum, actualSha1Sum); } finally { IO.close(digester); IO.close(bais); } }
public void assertIsResponseGzipCompressed(String requestedFilename, String serverFilename) throws Exception { System.err.printf("[GzipTester] requesting /context/%s%n", requestedFilename); 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); request.setURI("/context/" + requestedFilename); // Issue the request ByteArrayBuffer reqsBuff = new ByteArrayBuffer(request.generate().getBytes()); // Collect the response(s) ByteArrayBuffer respBuff = servletTester.getResponses(reqsBuff); response.parse(respBuff.asArray()); // Assert the response headers Assert.assertThat("Response.method", response.getMethod(), nullValue()); // // 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)); // Assert that the decompressed contents are what we expect. File serverFile = testdir.getFile(serverFilename); String expected = IO.readToString(serverFile); String actual = null; ByteArrayInputStream bais = null; InputStream in = null; ByteArrayOutputStream out = null; try { bais = new ByteArrayInputStream(response.getContentBytes()); if (compressionType.equals(GzipFilter.GZIP)) { in = new GZIPInputStream(bais); } else if (compressionType.equals(GzipFilter.DEFLATE)) { in = new InflaterInputStream(bais, new Inflater(true)); } out = new ByteArrayOutputStream(); IO.copy(in, out); actual = out.toString(encoding); assertThat("Uncompressed contents", actual, equalTo(expected)); } finally { IO.close(out); IO.close(in); IO.close(bais); } }
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 void dumpHeaders(String prefix, HttpTester http) { System.out.println(prefix); @SuppressWarnings("unchecked") Enumeration<String> names = http.getHeaderNames(); while (names.hasMoreElements()) { String name = names.nextElement(); String value = http.getHeader(name); System.out.printf(" [%s] = %s%n", name, value); } }