@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()))); }
private BufferedImage getResizedHttpResponseContent(String sourceContentType, String targetContentType, String imageName, Integer width, Integer height, Integer quality, boolean noExpand) throws Exception { HttpResponseBuilder response = getImageResponse(sourceContentType, getImageBytes(imageName)); HttpRequest request = getMockRequest(width, height, quality, noExpand); mockControl.replay(); rewriter.rewrite(request, response); mockControl.verify(); assertEquals(targetContentType, response.getHeader(CONTENT_TYPE_HEADER)); return ImageIO.read(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 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); 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 testRewriteUnAnimatedGIF() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, getImageBytes("org/apache/shindig/gadgets/rewrite/image/large.gif")); rewriter.rewrite(new HttpRequest(Uri.parse("large.gif")), response, null); assertEquals(CONTENT_TYPE_PNG, response.getHeader(CONTENT_TYPE_HEADER)); }
@Test public void testRewriteUnAnimatedGIF() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, getImageBytes("org/apache/shindig/gadgets/rewrite/image/large.gif")); rewriter.rewrite(new HttpRequest(Uri.parse("large.gif")), response); assertEquals(CONTENT_TYPE_PNG, response.getHeader(CONTENT_TYPE_HEADER)); }
@Test public void testNoRewriteAnimatedGIF() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, getImageBytes("org/apache/shindig/gadgets/rewrite/image/animated.gif")); int changesBefore = response.getNumChanges(); rewriter.rewrite(new HttpRequest(Uri.parse("animated.gif")), response); assertEquals(changesBefore, response.getNumChanges()); }
@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 testNoRewriteAnimatedGIF() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, getImageBytes("org/apache/shindig/gadgets/rewrite/image/animated.gif")); int changesBefore = response.getNumChanges(); rewriter.rewrite(new HttpRequest(Uri.parse("animated.gif")), response, null); assertEquals(changesBefore, response.getNumChanges()); }
@Test public void testNoRewriteAnimatedGIF() throws Exception { HttpResponseBuilder response = getImageResponse(CONTENT_TYPE_GIF, getImageBytes("org/apache/shindig/gadgets/rewrite/image/animated.gif")); int changesBefore = response.getNumChanges(); rewriter.rewrite(new HttpRequest(Uri.parse("animated.gif")), response, null); assertEquals(changesBefore, response.getNumChanges()); }
@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 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); 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 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); }