Add URL-to-path and clean path conversion

This commit is contained in:
Kijin Sung 2016-03-15 13:48:07 +09:00
parent 86e91d116e
commit 9d2fe0270b
4 changed files with 140 additions and 8 deletions

View file

@ -25,6 +25,17 @@ class URLTest extends \Codeception\TestCase\Test
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php?xe=sucks&l=ko', Rhymix\Framework\URL::getCurrentURL(array('l' => 'ko', 'foo' => null)));
}
public function testGetCurrentDomainURL()
{
$protocol = \RX_SSL ? 'https://' : 'http://';
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/', Rhymix\Framework\URL::getCurrentDomainURL());
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/', Rhymix\Framework\URL::getCurrentDomainURL('/'));
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/foo/bar', Rhymix\Framework\URL::getCurrentDomainURL('/foo/bar'));
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php?foo=bar', Rhymix\Framework\URL::getCurrentDomainURL('index.php?foo=bar'));
}
public function testGetCanonicalURL()
{
$protocol = \RX_SSL ? 'https://' : 'http://';
@ -82,6 +93,30 @@ class URLTest extends \Codeception\TestCase\Test
// This function is checked in Security::checkCSRF()
}
public function testURLFromServerPath()
{
$protocol = \RX_SSL ? 'https://' : 'http://';
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL, Rhymix\Framework\URL::fromServerPath(\RX_BASEDIR));
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php', Rhymix\Framework\URL::fromServerPath(\RX_BASEDIR . 'index.php'));
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'foo/bar', Rhymix\Framework\URL::fromServerPath(\RX_BASEDIR . '/foo/bar'));
$this->assertEquals(false, Rhymix\Framework\URL::fromServerPath('C:/Windows'));
}
public function testURLToServerPath()
{
$protocol = \RX_SSL ? 'https://' : 'http://';
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$this->assertEquals(\RX_BASEDIR . 'index.php', Rhymix\Framework\URL::toServerPath($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php'));
$this->assertEquals(\RX_BASEDIR . 'foo/bar', Rhymix\Framework\URL::toServerPath($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . '/foo/bar?arg=baz'));
$this->assertEquals(\RX_BASEDIR . 'foo/bar', Rhymix\Framework\URL::toServerPath('./foo/bar'));
$this->assertEquals(\RX_BASEDIR . 'foo/bar', Rhymix\Framework\URL::toServerPath('foo/bar/../bar'));
$this->assertEquals(false, Rhymix\Framework\URL::toServerPath('http://other.domain.com/'));
$this->assertEquals(false, Rhymix\Framework\URL::toServerPath('//other.domain.com/'));
}
public function testEncodeIdna()
{
$this->assertEquals('xn--9i1bl3b186bf9e.xn--3e0b707e', Rhymix\Framework\URL::encodeIdna('퓨니코드.한국'));

View file

@ -1,5 +1,7 @@
<?php
use Rhymix\Framework\Filters\FilenameFilter;
class FilenameFilterTest extends \Codeception\TestCase\Test
{
public function testFilenameFilterClean()
@ -35,8 +37,28 @@ class FilenameFilterTest extends \Codeception\TestCase\Test
foreach ($tests as $from => $to)
{
$result = Rhymix\Framework\Filters\FilenameFilter::clean($from);
$result = FilenameFilter::clean($from);
$this->assertEquals($to, $result);
}
}
public function testFilenameFilterCleanPath()
{
// Remove extra dots and slashes.
$this->assertEquals('/usr/share/foo/bar.jpg', FilenameFilter::cleanPath('/usr/share/foo//./baz/../bar.jpg'));
$this->assertEquals('/usr/share/foo/bar.jpg', FilenameFilter::cleanPath('/usr/share/foo/././baz/../../foo/bar.jpg'));
$this->assertEquals('/usr/share', FilenameFilter::cleanPath('/usr/share/foo/..'));
$this->assertEquals('/usr/share', FilenameFilter::cleanPath('/usr/share/foo/bar/../baz/../../'));
// Test Windows paths.
$this->assertEquals('C:/Windows/Notepad.exe', FilenameFilter::cleanPath('C:\\Windows\\System32\\..\\Notepad.exe'));
// Do not remove .. if there is no parent directory.
$this->assertEquals('C:/../foobar', FilenameFilter::cleanPath('C:\\..\foobar\\'));
$this->assertEquals('/../foobar', FilenameFilter::cleanPath('/../foobar/'));
// Remove query strings and URL fragments.
$this->assertEquals('index.php', FilenameFilter::cleanPath('index.php?foo=bar'));
$this->assertEquals('index.php', FilenameFilter::cleanPath('index.php#baz'));
}
}