Search in sources :

Example 1 with Response

use of org.eclipse.jetty.http.HttpTester.Response in project jetty.project by eclipse.

the class GzipTester method assertIsResponseNotModified.

public HttpTester.Response assertIsResponseNotModified(String method, String requestedFilename, long ifmodifiedsince) throws Exception {
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod(method);
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setHeader("Accept-Encoding", compressionType);
    if (ifmodifiedsince > 0)
        request.setHeader(HttpHeader.IF_MODIFIED_SINCE.asString(), DateGenerator.formatDate(ifmodifiedsince));
    if (this.userAgent != null)
        request.setHeader("User-Agent", this.userAgent);
    request.setURI("/context/" + requestedFilename);
    // Issue the request
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    Assert.assertThat(response.getStatus(), Matchers.equalTo(304));
    Assert.assertThat(response.get("ETag"), Matchers.startsWith("W/"));
    return response;
}
Also used : Response(org.eclipse.jetty.http.HttpTester.Response) HttpTester(org.eclipse.jetty.http.HttpTester)

Example 2 with Response

use of org.eclipse.jetty.http.HttpTester.Response in project jetty.project by eclipse.

the class GzipTester method assertIsResponseGzipCompressed.

public HttpTester.Response assertIsResponseGzipCompressed(String method, String requestedFilename, String serverFilename, long ifmodifiedsince) throws Exception {
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod(method);
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setHeader("Accept-Encoding", compressionType);
    if (ifmodifiedsince > 0)
        request.setHeader(HttpHeader.IF_MODIFIED_SINCE.asString(), DateGenerator.formatDate(ifmodifiedsince));
    if (this.userAgent != null)
        request.setHeader("User-Agent", this.userAgent);
    request.setURI("/context/" + requestedFilename);
    // Issue the request
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    // Assert the response headers
    // Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK));
    // Response headers should have either a Transfer-Encoding indicating chunked OR a Content-Length
    /*
         * TODO need to check for the 3rd option of EOF content. To do this properly you might need to look at both HTTP/1.1 and HTTP/1.0 requests String
         * contentLength = response.get("Content-Length"); String transferEncoding = response.get("Transfer-Encoding");
         * 
         * boolean chunked = (transferEncoding != null) && (transferEncoding.indexOf("chunk") >= 0); if(!chunked) {
         * Assert.assertThat("Response.header[Content-Length]",contentLength,notNullValue()); } else {
         * Assert.assertThat("Response.header[Transfer-Encoding]",transferEncoding,notNullValue()); }
         */
    int qindex = compressionType.indexOf(";");
    if (qindex < 0)
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType));
    else
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType.substring(0, qindex)));
    Assert.assertThat(response.get("ETag"), Matchers.startsWith("W/"));
    // Assert that the decompressed contents are what we expect.
    File serverFile = testdir.getPathFile(serverFilename).toFile();
    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.startsWith(GzipHandler.GZIP)) {
            in = new GZIPInputStream(bais);
        } else if (compressionType.startsWith(GzipHandler.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);
    }
    return response;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) Response(org.eclipse.jetty.http.HttpTester.Response) HttpTester(org.eclipse.jetty.http.HttpTester)

Example 3 with Response

use of org.eclipse.jetty.http.HttpTester.Response in project jetty.project by eclipse.

the class GzipTester method assertIsResponseNotGzipFiltered.

/**
     * Makes sure that the response contains an unfiltered file contents.
     * <p>
     * This is used to test exclusions and passthroughs in the GzipHandler.
     * <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 the expected content type
     * @param expectedContentEncoding
     *            can be non-null in some circumstances, eg when dealing with pre-gzipped .svgz files
     * @throws Exception on test failure
     */
public void assertIsResponseNotGzipFiltered(String requestedFilename, String testResourceSha1Sum, String expectedContentType, String expectedContentEncoding) throws Exception {
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    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
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    dumpHeaders(requestedFilename + " / Response Headers", response);
    // Assert the response headers
    String prefix = requestedFilename + " / Response";
    Assert.assertThat(prefix + ".status", response.getStatus(), is(HttpServletResponse.SC_OK));
    Assert.assertThat(prefix + ".header[Content-Length]", response.get("Content-Length"), notNullValue());
    Assert.assertThat(prefix + ".header[Content-Encoding] (should not be recompressed by GzipHandler)", response.get("Content-Encoding"), expectedContentEncoding == null ? nullValue() : notNullValue());
    if (expectedContentEncoding != null)
        Assert.assertThat(prefix + ".header[Content-Encoding]", response.get("Content-Encoding"), is(expectedContentEncoding));
    Assert.assertThat(prefix + ".header[Content-Type] (should have a Content-Type associated with it)", response.get("Content-Type"), notNullValue());
    Assert.assertThat(prefix + ".header[Content-Type]", response.get("Content-Type"), is(expectedContentType));
    Assert.assertThat(response.get("ETAG"), Matchers.startsWith("W/"));
    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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageDigest(java.security.MessageDigest) Response(org.eclipse.jetty.http.HttpTester.Response) HttpTester(org.eclipse.jetty.http.HttpTester)

Example 4 with Response

use of org.eclipse.jetty.http.HttpTester.Response in project jetty.project by eclipse.

the class GzipTester method assertNonStaticContentIsResponseGzipCompressed.

public HttpTester.Response assertNonStaticContentIsResponseGzipCompressed(String method, String path, String expected) throws Exception {
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod(method);
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setHeader("Accept-Encoding", accept);
    if (this.userAgent != null)
        request.setHeader("User-Agent", this.userAgent);
    request.setURI("/context/" + path);
    // Issue the request
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    int qindex = compressionType.indexOf(";");
    if (qindex < 0)
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType));
    else
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType.substring(0, qindex)));
    ByteArrayInputStream bais = null;
    InputStream in = null;
    ByteArrayOutputStream out = null;
    String actual = null;
    try {
        bais = new ByteArrayInputStream(response.getContentBytes());
        if (compressionType.startsWith(GzipHandler.GZIP)) {
            in = new GZIPInputStream(bais);
        } else if (compressionType.startsWith(GzipHandler.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);
    }
    return response;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Response(org.eclipse.jetty.http.HttpTester.Response) HttpTester(org.eclipse.jetty.http.HttpTester)

Aggregations

HttpTester (org.eclipse.jetty.http.HttpTester)4 Response (org.eclipse.jetty.http.HttpTester.Response)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 Inflater (java.util.zip.Inflater)2 InflaterInputStream (java.util.zip.InflaterInputStream)2 File (java.io.File)1 DigestOutputStream (java.security.DigestOutputStream)1 MessageDigest (java.security.MessageDigest)1