Skip to content
Extraits de code Groupes Projets
Valider 0c051152 rédigé par mattab's avatar mattab
Parcourir les fichiers

Accept URLs formatted as //domain/path

parent a0d3bf94
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -101,8 +101,8 @@ class UrlHelper ...@@ -101,8 +101,8 @@ class UrlHelper
*/ */
public static function isLookLikeUrl($url) public static function isLookLikeUrl($url)
{ {
return preg_match('~^(ftp|news|http|https)?://(.*)$~D', $url, $matches) !== 0 return preg_match('~^((ftp|news|http|https)?:)?//(.*)$~D', $url, $matches) !== 0
&& strlen($matches[2]) > 0; && strlen($matches[3]) > 0;
} }
/** /**
......
...@@ -28,9 +28,17 @@ class Core_UrlHelperTest extends \PHPUnit_Framework_TestCase ...@@ -28,9 +28,17 @@ class Core_UrlHelperTest extends \PHPUnit_Framework_TestCase
array('news://www.pi-wik.org', true), array('news://www.pi-wik.org', true),
array('https://www.tëteâ.org', true), array('https://www.tëteâ.org', true),
array('http://汉语/漢語.cn', true), //chinese array('http://汉语/漢語.cn', true), //chinese
// valid network-path reference RFC3986
array('//piwik.org', true),
array('//piwik/hello?world=test&test', true),
array('//piwik.org/hello?world=test&test', true),
// invalid urls // invalid urls
array('it doesnt look like url', false), array('it doesnt look like url', false),
array('/index?page=test', false), array('/index?page=test', false),
array('http:/index?page=test', false),
array('http/index?page=test', false),
array('test.html', false), array('test.html', false),
array('/\/\/\/\/\/\\\http://test.com////', false), array('/\/\/\/\/\/\\\http://test.com////', false),
array('jmleslangues.php', false), array('jmleslangues.php', false),
...@@ -46,7 +54,7 @@ class Core_UrlHelperTest extends \PHPUnit_Framework_TestCase ...@@ -46,7 +54,7 @@ class Core_UrlHelperTest extends \PHPUnit_Framework_TestCase
*/ */
public function testIsUrl($url, $isValid) public function testIsUrl($url, $isValid)
{ {
$this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url)); $this->assertEquals($isValid, UrlHelper::isLookLikeUrl($url), "$url failed test");
} }
/** /**
...@@ -196,6 +204,8 @@ class Core_UrlHelperTest extends \PHPUnit_Framework_TestCase ...@@ -196,6 +204,8 @@ class Core_UrlHelperTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('', UrlHelper::getHostFromUrl(null)); $this->assertEquals('', UrlHelper::getHostFromUrl(null));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('http://localhost')); $this->assertEquals('localhost', UrlHelper::getHostFromUrl('http://localhost'));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('http://localhost/path')); $this->assertEquals('localhost', UrlHelper::getHostFromUrl('http://localhost/path'));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('//localhost/path'));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('//localhost/path?test=test2'));
$this->assertEquals('localhost', UrlHelper::getHostFromUrl('localhost/path')); $this->assertEquals('localhost', UrlHelper::getHostFromUrl('localhost/path'));
$this->assertEquals('sub.localhost', UrlHelper::getHostFromUrl('sub.localhost/path')); $this->assertEquals('sub.localhost', UrlHelper::getHostFromUrl('sub.localhost/path'));
$this->assertEquals('sub.localhost', UrlHelper::getHostFromUrl('http://sub.localhost/path/?query=test')); $this->assertEquals('sub.localhost', UrlHelper::getHostFromUrl('http://sub.localhost/path/?query=test'));
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter