/** * Construct an HttpResponse from a builder (called by HttpResponseBuilder.create). */ HttpResponse(HttpResponseBuilder builder) { httpStatusCode = builder.getHttpStatusCode(); Multimap<String, String> headerCopy = HttpResponse.newHeaderMultimap(); // Always safe, HttpResponseBuilder won't modify the body. responseBytes = builder.getResponse(); // Copy headers after builder.getResponse(), since that can modify Content-Type. headerCopy.putAll(builder.getHeaders()); Map<String, String> metadataCopy = Maps.newHashMap(builder.getMetadata()); metadata = Collections.unmodifiableMap(metadataCopy); // We want to modify the headers to ensure that the proper Content-Type and Date headers // have been set. This allows us to avoid these expensive calculations from the cache. date = getAndUpdateDate(headerCopy); encoding = getAndUpdateEncoding(headerCopy, responseBytes); headers = Multimaps.unmodifiableMultimap(headerCopy); }
/** * Construct an HttpResponse from a builder (called by HttpResponseBuilder.create). */ HttpResponse(HttpResponseBuilder builder) { httpStatusCode = builder.getHttpStatusCode(); Multimap<String, String> headerCopy = HttpResponse.newHeaderMultimap(); // Always safe, HttpResponseBuilder won't modify the body. responseBytes = builder.getResponse(); // Copy headers after builder.getResponse(), since that can modify Content-Type. headerCopy.putAll(builder.getHeaders()); Map<String, String> metadataCopy = Maps.newHashMap(builder.getMetadata()); metadata = Collections.unmodifiableMap(metadataCopy); // We want to modify the headers to ensure that the proper Content-Type and Date headers // have been set. This allows us to avoid these expensive calculations from the cache. date = getAndUpdateDate(headerCopy); encoding = getAndUpdateEncoding(headerCopy, responseBytes); headers = Multimaps.unmodifiableMultimap(headerCopy); }
@Test public void testResize_acceptServeHugeImages() throws Exception { byte[] imageBytes = getImageBytes(HUGE_IMAGE); HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, imageBytes); HttpRequest request = getMockRequest(null, null, null, false); mockControl.replay(); rewriter.rewrite(request, response, null); mockControl.verify(); assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode()); assertTrue(Arrays.equals(imageBytes, IOUtils.toByteArray(response.getContentBytes()))); }
@Test public void testResize_acceptServeHugeImages() throws Exception { byte[] imageBytes = getImageBytes(HUGE_IMAGE); HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, imageBytes); HttpRequest request = getMockRequest(null, null, null, false); mockControl.replay(); rewriter.rewrite(request, response, null); mockControl.verify(); assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode()); assertTrue(Arrays.equals(imageBytes, IOUtils.toByteArray(response.getContentBytes()))); }
@Test public void testResize_acceptServeHugeImages() throws Exception { byte[] imageBytes = getImageBytes(HUGE_IMAGE); HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, imageBytes); HttpRequest request = getMockRequest(null, null, null, false); mockControl.replay(); rewriter.rewrite(request, response); mockControl.verify(); assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode()); assertTrue(Arrays.equals(imageBytes, IOUtils.toByteArray(response.getContentBytes()))); }
@Test public void testRewriteInvalidImageContentWithValidFileExtn() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_BOGUS, "This is not an image".getBytes()); rewriter.rewrite(new HttpRequest(Uri.parse("some.png")), response, null); assertEquals(HttpResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getHttpStatusCode()); assertEquals(CONTENT_TYPE_AND_EXTENSION_MISMATCH, response.create().getResponseAsString()); }
@Test public void testRewriteInvalidImageContentWithValidFileExtn() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_BOGUS, "This is not an image".getBytes()); rewriter.rewrite(new HttpRequest(Uri.parse("some.png")), response); assertEquals(HttpResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getHttpStatusCode()); assertEquals(CONTENT_TYPE_AND_EXTENSION_MISMATCH, response.create().getResponseAsString()); }
@Test public void testRewriteInvalidImageContentWithValidMime() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_PNG, "This is not a PNG".getBytes()); rewriter.rewrite(new HttpRequest(Uri.parse("some.junk")), response, null); assertEquals(HttpResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getHttpStatusCode()); assertEquals(CONTENT_TYPE_AND_MIME_MISMATCH, response.create().getResponseAsString()); }
@Test public void testRewriteInvalidImageContentWithValidFileExtn() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_BOGUS, "This is not an image".getBytes()); rewriter.rewrite(new HttpRequest(Uri.parse("some.png")), response, null); assertEquals(HttpResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getHttpStatusCode()); assertEquals(CONTENT_TYPE_AND_EXTENSION_MISMATCH, response.create().getResponseAsString()); }
@Test public void testRewriteInvalidImageContentWithValidMime() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_PNG, "This is not a PNG".getBytes()); rewriter.rewrite(new HttpRequest(Uri.parse("some.junk")), response); assertEquals(HttpResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getHttpStatusCode()); assertEquals(CONTENT_TYPE_AND_MIME_MISMATCH, response.create().getResponseAsString()); }
@Test public void testRewriteInvalidImageContentWithValidMime() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_PNG, "This is not a PNG".getBytes()); rewriter.rewrite(new HttpRequest(Uri.parse("some.junk")), response, null); assertEquals(HttpResponse.SC_UNSUPPORTED_MEDIA_TYPE, response.getHttpStatusCode()); assertEquals(CONTENT_TYPE_AND_MIME_MISMATCH, response.create().getResponseAsString()); }
@Test public void testResize_refuseHugeInputImages() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, getImageBytes(HUGE_IMAGE)); HttpRequest request = getMockRequest(120, 60, null, false); mockControl.replay(); rewriter.rewrite(request, response); mockControl.verify(); assertEquals(HttpResponse.SC_FORBIDDEN, response.getHttpStatusCode()); }
@Test public void testResize_refuseHugeInputImages() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, getImageBytes(HUGE_IMAGE)); HttpRequest request = getMockRequest(120, 60, null, false); mockControl.replay(); rewriter.rewrite(request, response, null); mockControl.verify(); assertEquals(HttpResponse.SC_FORBIDDEN, response.getHttpStatusCode()); }
@Test public void testResize_refuseHugeInputImages() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, getImageBytes(HUGE_IMAGE)); HttpRequest request = getMockRequest(120, 60, null, false); mockControl.replay(); rewriter.rewrite(request, response, null); mockControl.verify(); assertEquals(HttpResponse.SC_FORBIDDEN, response.getHttpStatusCode()); }
@Test public void testRewriteValidImageWithInvalidMimeAndFileExtn() throws Exception { byte[] bytes = getImageBytes("org/apache/shindig/gadgets/rewrite/image/inefficient.png"); HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_BOGUS, bytes); int originalContentLength = response.getContentLength(); rewriter.rewrite(new HttpRequest(Uri.parse("some.junk")), response); assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode()); assertEquals(response.getContentLength(), originalContentLength); }
@Test public void testRewriteValidImageWithValidMimeAndExtn() throws Exception { byte[] bytes = getImageBytes("org/apache/shindig/gadgets/rewrite/image/inefficient.png"); HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_PNG, bytes); int originalContentLength = response.getContentLength(); rewriter.rewrite(new HttpRequest(Uri.parse("some.png")), response, null); assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode()); assertTrue(response.getContentLength() < originalContentLength); }
@Test public void testRewriteValidImageWithInvalidMimeAndFileExtn() throws Exception { byte[] bytes = getImageBytes("org/apache/shindig/gadgets/rewrite/image/inefficient.png"); HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_BOGUS, bytes); int originalContentLength = response.getContentLength(); rewriter.rewrite(new HttpRequest(Uri.parse("some.junk")), response, null); assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode()); assertEquals(response.getContentLength(), originalContentLength); }
@Test public void testRewriteValidImageWithValidMimeAndExtn() throws Exception { byte[] bytes = getImageBytes("org/apache/shindig/gadgets/rewrite/image/inefficient.png"); HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_PNG, bytes); int originalContentLength = response.getContentLength(); rewriter.rewrite(new HttpRequest(Uri.parse("some.png")), response); assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode()); assertTrue(response.getContentLength() < originalContentLength); }
@Test public void testRewriteValidImageWithValidMimeAndExtn() throws Exception { byte[] bytes = getImageBytes("org/apache/shindig/gadgets/rewrite/image/inefficient.png"); HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_PNG, bytes); int originalContentLength = response.getContentLength(); rewriter.rewrite(new HttpRequest(Uri.parse("some.png")), response, null); assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode()); assertTrue(response.getContentLength() < originalContentLength); }
@Test public void testRewriteValidImageWithInvalidMimeAndFileExtn() throws Exception { byte[] bytes = getImageBytes("org/apache/shindig/gadgets/rewrite/image/inefficient.png"); HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_BOGUS, bytes); int originalContentLength = response.getContentLength(); rewriter.rewrite(new HttpRequest(Uri.parse("some.junk")), response, null); assertEquals(HttpResponse.SC_OK, response.getHttpStatusCode()); assertEquals(response.getContentLength(), originalContentLength); }