diff --git a/src/main/java/com/commafeed/backend/feed/FeedUtils.java b/src/main/java/com/commafeed/backend/feed/FeedUtils.java index c236a29b..0999e85e 100644 --- a/src/main/java/com/commafeed/backend/feed/FeedUtils.java +++ b/src/main/java/com/commafeed/backend/feed/FeedUtils.java @@ -430,12 +430,8 @@ public class FeedUtils { } public static boolean isRelative(final String url) { - // the regex means "doesn't start with 'scheme://'" - if ((url != null) && (url.startsWith("/") == false) && (!url.matches("^\\w+\\:\\/\\/.*")) && !(url.startsWith("#"))) { - return true; - } else { - return false; - } + // the regex means "start with 'scheme://'" + return url.startsWith("/") || url.startsWith("#") || !url.matches("^\\w+\\:\\/\\/.*"); } public static String getFaviconUrl(FeedSubscription subscription, String publicUrl) { diff --git a/src/test/java/com/commafeed/backend/feed/FeedUtilsTest.java b/src/test/java/com/commafeed/backend/feed/FeedUtilsTest.java index e7dd4710..de41be4b 100644 --- a/src/test/java/com/commafeed/backend/feed/FeedUtilsTest.java +++ b/src/test/java/com/commafeed/backend/feed/FeedUtilsTest.java @@ -44,11 +44,18 @@ public class FeedUtilsTest { @Test public void testToAbsoluteUrl() { String expected = "http://a.com/blog/entry/1"; + + // usual cases Assert.assertEquals(expected, FeedUtils.toAbsoluteUrl("http://a.com/blog/entry/1", "http://a.com/feed/", "http://a.com/feed/")); Assert.assertEquals(expected, FeedUtils.toAbsoluteUrl("http://a.com/blog/entry/1", "http://a.com/feed", "http://a.com/feed")); + + // relative links Assert.assertEquals(expected, FeedUtils.toAbsoluteUrl("../blog/entry/1", "http://a.com/feed/", "http://a.com/feed/")); Assert.assertEquals(expected, FeedUtils.toAbsoluteUrl("../blog/entry/1", "feed.xml", "http://a.com/feed/feed.xml")); + // root-relative links + Assert.assertEquals(expected, FeedUtils.toAbsoluteUrl("/blog/entry/1", "/feed", "http://a.com/feed")); + Assert.assertEquals("http://ergoemacs.org/emacs/elisp_all_about_lines.html", FeedUtils.toAbsoluteUrl("elisp_all_about_lines.html", "blog.xml", "http://ergoemacs.org/emacs/blog.xml"));