This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Merge pull request #33 from sripathikrishnan/master

Issue #32 : Fixes getImageURIString to handle images with query parameters
  • Loading branch information...
nzakas committed Aug 21, 2011
2 parents 6727493 + 493501d commit 6111b05e52c2a771e1ef835e77245170bac1564e
@@ -320,13 +320,10 @@ public void embedImages(Writer out, String root) throws IOException {
* @return The appropriate data URI to use.
* @throws java.io.IOException
*/
private String getImageURIString(String url, String originalUrl) throws IOException {
//check the extension - only encode for images
String fileType = url.substring(url.lastIndexOf(".") + 1);
String getImageURIString(String url, String originalUrl) throws IOException {
//it's an image, so encode it
if (imageTypes.contains(fileType)){
if (isImage(url)){
DataURIGenerator.setVerbose(verbose);
@@ -392,8 +389,27 @@ private String getImageURIString(String url, String originalUrl) throws IOExcept
}
}
private String getFilename(String path){
/*
* Detects if the given url represents an image
* This method simply checks the file extension.
* A better way to detect an image is via content type response headers or by content sniffing,
* but both are expensive approaches. We can do without them for now.
*/
static boolean isImage(String url) {
int startPos = url.lastIndexOf(".") + 1;
/*
* Some images are of the form some-image.png?parameter=value
*/
int endPos = url.lastIndexOf("?");
if(endPos == -1 || endPos <= startPos) {
endPos = url.length();
}
String fileType = url.substring(startPos, endPos);
return imageTypes.contains(fileType);
}
private String getFilename(String path){
if (path.indexOf("/") > -1){
return path.substring(path.lastIndexOf("/")+1);
} else if (path.indexOf("\\") > -1){
@@ -196,7 +196,7 @@ public void testAbsoluteLocalFileOverMaxImageSize() throws IOException {
@Test
public void testReadFromAndWriteToSameFile() throws IOException {
String filename = CSSURLEmbedderTest.class.getResource("samefiletest.css").getPath().replace("%20", " ");
String filename = this.getClass().getClassLoader().getResource("samefiletest.css").getPath().replace("%20", " ");
File file = new File(filename);
Reader in = new InputStreamReader(new FileInputStream(file));
@@ -232,4 +232,51 @@ public void testRegularUrlWithMhtml() throws IOException {
String result = writer.toString();
assertEquals("background: url(folder.txt);", result);
}
@Test
public void testRemoteUrlWithQueryString() throws IOException {
final String expectedUrl = "http://some-http-server.com/image/with/query/parameters/image.png?a=b&c=d";
String code = "background : url(/image/with/query/parameters/image.png?a=b&c=d)";
StringWriter writer = new StringWriter();
embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 0, 200) {
/*
* Override method to prevent a network call during unit tests
*/
@Override
String getImageURIString(String url, String originalUrl) throws IOException {
if(url.equals("")) {
throw new IllegalArgumentException("Expected URL " + expectedUrl + ", but found " + url);
}
return "data:image/gif;base64,AAAABBBBCCCCDDDD";
}
};
embedder.embedImages(writer, "http://some-http-server.com/");
String result = writer.toString();
assertEquals("background : url(data:image/gif;base64,AAAABBBBCCCCDDDD)", result);
}
@Test
public void testImageDetection() {
String tests[] = {
"file://path/to/image.png",
"http://some.server.com/image.png",
"http://some.server.com/image.png?param=legalvalue&anotherparam=anothervalue",
"http://some.server.com/image.png?param=illegal.value.with.period"
};
boolean expectedImage[] = {
true, true, true, false
};
for(int i=0; i<tests.length; i++) {
if(expectedImage[i]) {
assertTrue("Expected " + tests[i] + " to resolve to an image", CSSURLEmbedder.isImage(tests[i]));
}
else {
assertFalse("Did NOT expect " + tests[i] + " to resolve as an image", CSSURLEmbedder.isImage(tests[i]));
}
}
}
}

0 comments on commit 6111b05

Please sign in to comment.