UrlRulesTestpublic class UrlRulesTest extends android.test.AndroidTestCase Test loading and matching URL rewrite rules for UrlRules. |
Methods Summary |
---|
public void | testAmbiguousMatch()
// Rule is the longest match wins.
UrlRules rules = new UrlRules(new Rule[] {
new Rule("1", "http://xyz/one rewrite http://rewrite/"),
new Rule("123", "http://xyz/onetwothree rewrite http://rewrite/"),
new Rule("12", "http://xyz/onetwo rewrite http://rewrite/"),
});
assertEquals("1", rules.matchRule("http://xyz/one").mName);
assertEquals("1", rules.matchRule("http://xyz/one...").mName);
assertEquals("12", rules.matchRule("http://xyz/onetwo...").mName);
assertEquals("123", rules.matchRule("http://xyz/onetwothree...").mName);
| public void | testBlockRule()
Rule rule = new Rule("test_rule",
"http://foo.bar/ block");
assertEquals("test_rule", rule.mName);
assertEquals("http://foo.bar/", rule.mPrefix);
assertTrue(rule.mRewrite == null);
assertTrue(rule.mBlock);
assertTrue(rule.apply("http://foo.bar/bat") == null);
| public void | testEmptyRules()
UrlRules rules = new UrlRules(new Rule[] { });
assertTrue(rules.matchRule("http://foo.bar/") == Rule.DEFAULT);
| public void | testGservicesRules()
// TODO: use a MockContentProvider/MockContentResolver instead.
ContentResolver r = getContext().getContentResolver();
// Update the digest, so the UrlRules cache is reloaded.
Settings.Gservices.putString(r, "digest", "testGservicesRules");
Settings.Gservices.putString(r, "url:blank_test", "");
Settings.Gservices.putString(r, "url:test",
"http://foo.bar/ rewrite http://bar.foo/");
UrlRules rules = UrlRules.getRules(r); // Don't crash, please. :)
assertTrue(rules.matchRule("http://bar.foo/") == Rule.DEFAULT);
Rule rule = rules.matchRule("http://foo.bar/bat");
assertEquals("test", rule.mName);
assertEquals("http://foo.bar/", rule.mPrefix);
assertEquals("http://bar.foo/", rule.mRewrite);
assertFalse(rule.mBlock);
| public void | testInvalidRule()
try {
new Rule("rule", "foo bar");
} catch (Exception e) {
// Re-throw any exception except the one we're looking for.
if (!e.toString().contains("Illegal rule: foo bar")) throw e;
}
| public void | testMatchRule()
UrlRules rules = new UrlRules(new Rule[] {
new Rule("12", "http://one.two/ rewrite http://buckle.my.shoe/"),
new Rule("34", "http://three.four/ rewrite http://close.the.door/"),
new Rule("56", "http://five.six/ rewrite http://pick.up.sticks/"),
});
assertTrue(rules.matchRule("https://one.two/") == Rule.DEFAULT);
assertTrue(rules.matchRule("http://one.two") == Rule.DEFAULT);
assertEquals("12", rules.matchRule("http://one.two/foo").mName);
String u = "http://five.six/bar";
assertEquals("http://pick.up.sticks/bar", rules.matchRule(u).apply(u));
| public void | testRewriteRule()
Rule rule = new Rule("test_rule",
"http://foo.bar/ rewrite http://bar.foo/");
assertEquals("test_rule", rule.mName);
assertEquals("http://foo.bar/", rule.mPrefix);
assertEquals("http://bar.foo/", rule.mRewrite);
assertFalse(rule.mBlock);
assertEquals("http://bar.foo/bat", rule.apply("http://foo.bar/bat"));
|
|