]> git.wh0rd.org Git - tt-rss.git/commitdiff
add unit tests for rewrite_relative_url and fix a number of bugs in it
authorChristian Weiske <cweiske@cweiske.de>
Wed, 10 Nov 2010 22:09:03 +0000 (23:09 +0100)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Thu, 11 Nov 2010 06:44:05 +0000 (09:44 +0300)
functions.php
tests/FunctionsTest.php

index 96fa19224375c5c68339d4b1ece8d0051436a06e..a52dafddd24f92eb86a6b02bd0ed91925bd7054d 100644 (file)
                return $parts['scheme'] . "://" . $parts['host'] . $parts['path'];
        }
 
+       /**
+        * Converts a (possibly) relative URL to a absolute one.
+        *
+        * @param string $url     Base URL (i.e. from where the document is)
+        * @param string $rel_url Possibly relative URL in the document
+        *
+        * @return string Absolute URL
+        */
        function rewrite_relative_url($url, $rel_url) {
                if (strpos($rel_url, "://") !== false) {
                        return $rel_url;
 
                } else {
                        $parts = parse_url($url);
-
-                       $parts['path'] = dirname($parts['path']) . "/$rel_url";
+                       if (!isset($parts['path'])) {
+                               $parts['path'] = '/';
+                       }
+                       $dir = $parts['path'];
+                       if (substr($dir, -1) !== '/') {
+                               $dir = dirname($parts['path']);
+                               $dir !== '/' && $dir .= '/';
+                       }
+                       $parts['path'] = $dir . $rel_url;
 
                        return build_url($parts);
                }
index 163e985d0b3fe69cb1df3ab4a40c1375c83ccb9a..f1bfa74511cda63475e131f245657408ec08c6aa 100644 (file)
@@ -177,6 +177,69 @@ HTM
         );
         $this->assertFalse(url_is_html($this->tmpFile));
     }
+
+
+
+    /**
+     * Test rewrite_relative_url() with a relative path
+     */
+    public function testRewriteRelativeUrlRelative()
+    {
+        $this->assertEquals(
+            'http://tt-rss.org/foo/bar',
+            rewrite_relative_url('http://tt-rss.org', 'foo/bar')
+        );
+        $this->assertEquals(
+            'http://tt-rss.org/foo/bar',
+            rewrite_relative_url('http://tt-rss.org/', 'foo/bar')
+        );
+        $this->assertEquals(
+            'http://tt-rss.org/bar',
+            rewrite_relative_url('http://tt-rss.org/foo', 'bar')
+        );
+        $this->assertEquals(
+            'http://tt-rss.org/foo/bar',
+            rewrite_relative_url('http://tt-rss.org/foo/', 'bar')
+        );
+        $this->assertEquals(
+            'http://tt-rss.org/f/o/bar',
+            rewrite_relative_url('http://tt-rss.org/f/o/o', 'bar')
+        );
+        $this->assertEquals(
+            'http://tt-rss.org/f/o/o/bar',
+            rewrite_relative_url('http://tt-rss.org/f/o/o/', 'bar')
+        );
+    }
+
+    /**
+     * Test rewrite_relative_url() with an absolute path
+     */
+    public function testRewriteRelativeUrlAbsolutePath()
+    {
+        $this->assertEquals(
+            'http://tt-rss.org/bar/',
+            rewrite_relative_url('http://tt-rss.org/foo/', '/bar/')
+        );
+        $this->assertEquals(
+            'http://tt-rss.org/bar/',
+            rewrite_relative_url('http://tt-rss.org/so/what/is/next', '/bar/')
+        );
+        $this->assertEquals(
+            'http://tt-rss.org/bar/',
+            rewrite_relative_url('http://tt-rss.org/so/what/is/next/', '/bar/')
+        );
+    }
+
+    /**
+     * Test rewrite_relative_url() with an absolute URL
+     */
+    public function testRewriteRelativeUrlAbsoluteUrl()
+    {
+        $this->assertEquals(
+            'http://example.org/bar/',
+            rewrite_relative_url('http://tt-rss.org/foo/', 'http://example.org/bar/')
+        );
+    }
 }
 
 ?>
\ No newline at end of file