@Test public void testResize_height() throws Exception { BufferedImage image = getResizedHttpResponseContent( CONTENT_TYPE_GIF, CONTENT_TYPE_JPG, SCALE_IMAGE, null, 100, null); assertEquals(100, image.getWidth()); assertEquals(100, image.getHeight()); }
/** Extracts an image by its resource name and converts it into a byte array. */ private byte[] getImageBytes(String imageResourceName) throws IOException { ClassLoader classLoader = getClass().getClassLoader(); byte[] imageBytes = IOUtils.toByteArray(classLoader.getResourceAsStream(imageResourceName)); assertNotNull(imageBytes); return imageBytes; }
@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 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 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()); }
private BufferedImage getResizedHttpResponseContent(String sourceContentType, String targetContentType, String imageName, Integer width, Integer height, Integer quality) throws Exception { return getResizedHttpResponseContent( sourceContentType, targetContentType, imageName, width, height, quality, false); }
@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 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 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()); }
private BufferedImage getResizedHttpResponseContent(String sourceContentType, String targetContentType, String imageName, Integer width, Integer height, Integer quality) throws Exception { return getResizedHttpResponseContent( sourceContentType, targetContentType, imageName, width, height, quality, false); }
@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_wideImage() throws Exception { BufferedImage image = getResizedHttpResponseContent( CONTENT_TYPE_GIF, CONTENT_TYPE_JPG, SCALE_IMAGE, 100, 50, null); assertEquals(100, image.getWidth()); assertEquals(50, image.getHeight()); }
@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 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()); }
private BufferedImage getResizedHttpResponseContent(String sourceContentType, String targetContentType, String imageName, Integer width, Integer height, Integer quality) throws Exception { return getResizedHttpResponseContent( sourceContentType, targetContentType, imageName, width, height, quality, false); }
/** Extracts an image by its resource name and converts it into a byte array. */ private byte[] getImageBytes(String imageResourceName) throws IOException { ClassLoader classLoader = getClass().getClassLoader(); byte[] imageBytes = IOUtils.toByteArray(classLoader.getResourceAsStream(imageResourceName)); assertNotNull(imageBytes); return imageBytes; }
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 testResize_all() throws Exception { // The quality hint apparently has no effect on the result here BufferedImage image = getResizedHttpResponseContent( CONTENT_TYPE_GIF, CONTENT_TYPE_JPG, SCALE_IMAGE, 100, 100, 10); assertEquals(100, image.getWidth()); assertEquals(100, image.getHeight()); }
@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 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()); }