Merge pull request #378 from kijin/pr/url-conversion

경로↔URL 변환 및 정리 함수 추가
This commit is contained in:
Kijin Sung 2016-03-16 16:09:38 +09:00
commit c23a1949cc
14 changed files with 284 additions and 88 deletions

View file

@ -100,11 +100,9 @@ class SecurityTest extends \Codeception\TestCase\Test
$_SERVER['REQUEST_METHOD'] = 'POST';
$this->assertTrue(Rhymix\Framework\Security::checkCSRF());
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$_SERVER['HTTP_REFERER'] = 'http://www.foobar.com/';
$this->assertFalse(Rhymix\Framework\Security::checkCSRF());
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$this->assertTrue(Rhymix\Framework\Security::checkCSRF('http://www.rhymix.org/'));
}

View file

@ -4,37 +4,42 @@ class URLTest extends \Codeception\TestCase\Test
{
public function testGetCurrentURL()
{
$protocol = \RX_SSL ? 'https://' : 'http://';
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$_SERVER['REQUEST_URI'] = '/index.php?foo=bar&xe=sucks';
$full_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$old_request_uri = $_SERVER['REQUEST_URI'];
$_SERVER['REQUEST_URI'] = '/rhymix/index.php?foo=bar&xe=sucks';
// Getting the current URL
$this->assertEquals($full_url, Rhymix\Framework\URL::getCurrentURL());
$this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar&xe=sucks', Rhymix\Framework\URL::getCurrentURL());
// Adding items to the query string
$this->assertEquals($full_url . '&var=1&arr%5B0%5D=2&arr%5B1%5D=3', Rhymix\Framework\URL::getCurrentURL(array('var' => '1', 'arr' => array(2, 3))));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar&xe=sucks&var=1&arr%5B0%5D=2&arr%5B1%5D=3', Rhymix\Framework\URL::getCurrentURL(array('var' => '1', 'arr' => array(2, 3))));
// Removing item from the query string
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php?xe=sucks', Rhymix\Framework\URL::getCurrentURL(array('foo' => null)));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php?xe=sucks', Rhymix\Framework\URL::getCurrentURL(array('foo' => null)));
// Removing all items from the query string
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php', Rhymix\Framework\URL::getCurrentURL(array('foo' => null, 'xe' => null)));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php', Rhymix\Framework\URL::getCurrentURL(array('foo' => null, 'xe' => null)));
// Adding and removing parameters at the same time
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php?xe=sucks&l=ko', Rhymix\Framework\URL::getCurrentURL(array('l' => 'ko', 'foo' => null)));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php?xe=sucks&l=ko', Rhymix\Framework\URL::getCurrentURL(array('l' => 'ko', 'foo' => null)));
$_SERVER['REQUEST_URI'] = $old_request_uri;
}
public function testGetCurrentDomainURL()
{
$this->assertEquals('https://www.rhymix.org/', Rhymix\Framework\URL::getCurrentDomainURL());
$this->assertEquals('https://www.rhymix.org/', Rhymix\Framework\URL::getCurrentDomainURL('/'));
$this->assertEquals('https://www.rhymix.org/foo/bar', Rhymix\Framework\URL::getCurrentDomainURL('/foo/bar'));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar', Rhymix\Framework\URL::getCurrentDomainURL('rhymix/index.php?foo=bar'));
}
public function testGetCanonicalURL()
{
$protocol = \RX_SSL ? 'https://' : 'http://';
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$tests = array(
'foo/bar' => $protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'foo/bar',
'./foo/bar' => $protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'foo/bar',
'/foo/bar' => $protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'foo/bar',
'//www.example.com/foo' => $protocol . 'www.example.com/foo',
'foo/bar' => 'https://www.rhymix.org/rhymix/foo/bar',
'./foo/bar' => 'https://www.rhymix.org/rhymix/foo/bar',
'/foo/bar' => 'https://www.rhymix.org/rhymix/foo/bar',
'//www.example.com/foo' => 'https://www.example.com/foo',
'http://xn--cg4bkiv2oina.com/' => 'http://삼성전자.com/',
);
@ -60,21 +65,17 @@ class URLTest extends \Codeception\TestCase\Test
public function testModifyURL()
{
$protocol = \RX_SSL ? 'https://' : 'http://';
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
$url = $protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php?foo=bar';
// Conversion to absolute
$this->assertEquals($url, Rhymix\Framework\URL::modifyURL('./index.php?foo=bar'));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar', $url = Rhymix\Framework\URL::modifyURL('./index.php?foo=bar'));
// Adding items to the query string
$this->assertEquals($url . '&var=1&arr%5B0%5D=2&arr%5B1%5D=3', Rhymix\Framework\URL::modifyURL($url, array('var' => '1', 'arr' => array(2, 3))));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php?foo=bar&var=1&arr%5B0%5D=2&arr%5B1%5D=3', Rhymix\Framework\URL::modifyURL($url, array('var' => '1', 'arr' => array(2, 3))));
// Removing item from the query string
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php', Rhymix\Framework\URL::modifyURL($url, array('foo' => null)));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php', Rhymix\Framework\URL::modifyURL($url, array('foo' => null)));
// Adding and removing parameters at the same time
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php?l=ko', Rhymix\Framework\URL::modifyURL($url, array('l' => 'ko', 'foo' => null)));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php?l=ko', Rhymix\Framework\URL::modifyURL($url, array('l' => 'ko', 'foo' => null)));
}
public function testIsInternalURL()
@ -82,6 +83,25 @@ class URLTest extends \Codeception\TestCase\Test
// This function is checked in Security::checkCSRF()
}
public function testURLFromServerPath()
{
$this->assertEquals('https://www.rhymix.org/rhymix/', Rhymix\Framework\URL::fromServerPath(\RX_BASEDIR));
$this->assertEquals('https://www.rhymix.org/rhymix/index.php', Rhymix\Framework\URL::fromServerPath(\RX_BASEDIR . 'index.php'));
$this->assertEquals('https://www.rhymix.org/rhymix/foo/bar', Rhymix\Framework\URL::fromServerPath(\RX_BASEDIR . '/foo/bar'));
$this->assertEquals(false, Rhymix\Framework\URL::fromServerPath(dirname(dirname(\RX_BASEDIR))));
$this->assertEquals(false, Rhymix\Framework\URL::fromServerPath('C:/Windows'));
}
public function testURLToServerPath()
{
$this->assertEquals(\RX_BASEDIR . 'index.php', Rhymix\Framework\URL::toServerPath('http://www.rhymix.org/rhymix/index.php'));
$this->assertEquals(\RX_BASEDIR . 'foo/bar', Rhymix\Framework\URL::toServerPath('http://www.rhymix.org/rhymix/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,37 @@ 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 internal paths.
$this->assertEquals(\RX_BASEDIR . 'common/js/debug.js', FilenameFilter::cleanPath('common/js/debug.js'));
$this->assertEquals(\RX_BASEDIR . 'common/js/debug.js', FilenameFilter::cleanPath('./common/js/debug.js'));
// Test Windows paths.
$this->assertEquals('C:/Windows/Notepad.exe', FilenameFilter::cleanPath('C:\\Windows\\System32\\..\\Notepad.exe'));
$this->assertEquals('//vboxsrv/hello/world', FilenameFilter::cleanPath('\\\\vboxsrv\\hello\\world'));
// Test absolute URLs.
$this->assertEquals('https://www.rhymix.org/foo/bar', FilenameFilter::cleanPath('https://www.rhymix.org/foo/.//bar'));
$this->assertEquals('//www.rhymix.org/foo/bar', FilenameFilter::cleanPath('//www.rhymix.org/foo/.//bar'));
// 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(\RX_BASEDIR . 'index.php', FilenameFilter::cleanPath('index.php?foo=bar'));
$this->assertEquals(\RX_BASEDIR . 'index.php', FilenameFilter::cleanPath('index.php#baz'));
}
}