Skip to content

Commit

Permalink
encodeHtmlDataUri to avoid malformed URIs in tests
Browse files Browse the repository at this point in the history
BUG=


Review URL: https://chromiumcodereview.appspot.com/12079009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@179229 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
aruslan@chromium.org committed Jan 29, 2013
1 parent c9c2060 commit 71a4586
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import org.chromium.base.PathUtils;

import junit.framework.Assert;

/**
* Collection of URL utilities.
*/
Expand All @@ -14,10 +16,30 @@ public class UrlUtils {

/**
* Construct a suitable URL for loading a test data file.
*
* @param path Pathname relative to external/chrome/testing/data
*/
public static String getTestFileUrl(String path) {
return "file://" + PathUtils.getExternalStorageDirectory() + DATA_DIR + path;
}

/**
* Construct a data:text/html URI for loading from an inline HTML.
* @param html An unencoded HTML
* @return String An URI that contains the given HTML
*/
public static String encodeHtmlDataUri(String html) {
try {
// URLEncoder encodes into application/x-www-form-encoded, so
// ' '->'+' needs to be undone and replaced with ' '->'%20'
// to match the Data URI requirements.
String encoded =
"data:text/html;utf-8," +
java.net.URLEncoder.encode(html, "UTF-8");
encoded = encoded.replace("+", "%20");
return encoded;
} catch (java.io.UnsupportedEncodingException e) {
Assert.fail("Unsupported encoding: " + e.getMessage());
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.content.browser;

import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import org.chromium.base.test.util.UrlUtils;

import java.net.URI;
import java.net.URLDecoder;

public class EncodeHtmlDataUriTest extends InstrumentationTestCase {
private static final String DATA_URI_PREFIX = "data:text/html;utf-8,";

private String getData(String dataUri) {
assertNotNull("Data URI is null", dataUri);
assertTrue("Incorrect HTML Data URI prefix", dataUri.startsWith(DATA_URI_PREFIX));
return dataUri.substring(DATA_URI_PREFIX.length());
}

private String decode(String dataUri) throws java.io.UnsupportedEncodingException {
String data = getData(dataUri);
return URLDecoder.decode(data, "UTF-8");
}

@SmallTest
public void testDelimitersEncoding() throws java.io.UnsupportedEncodingException {
String testString = "><#%\"'";
String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
String decodedUri = decode(encodedUri);
assertEquals("Delimiters are not properly encoded", decodedUri, testString);
}

@SmallTest
public void testUnwiseCharactersEncoding() throws java.io.UnsupportedEncodingException {
String testString = "{}|\\^[]`";
String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
String decodedUri = decode(encodedUri);
assertEquals("Unwise characters are not properly encoded", decodedUri, testString);
}

@SmallTest
public void testWhitespaceEncoding() throws java.io.UnsupportedEncodingException {
String testString = " \n\t";
String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
String decodedUri = decode(encodedUri);
assertEquals("Whitespace characters are not properly encoded", decodedUri, testString);
}

@SmallTest
public void testReturnsValidUri()
throws java.net.URISyntaxException, java.io.UnsupportedEncodingException {
String testString = "<html><body onload=\"alert('Hello \\\"world\\\"');\"></body></html>";
String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
String decodedUri = decode(encodedUri);
// Verify that the encoded URI is valid.
new URI(encodedUri);
// Verify that something sensible was encoded.
assertEquals("Simple HTML is not properly encoded", decodedUri, testString);
}
}

0 comments on commit 71a4586

Please sign in to comment.