Flesh out URI tests for 100% coverage

This commit is contained in:
Master Yoda 2018-07-24 11:27:47 -07:00
parent cab7303fb1
commit 9e165acca7
No known key found for this signature in database
GPG Key ID: CED549230775AD5B
2 changed files with 86 additions and 54 deletions

View File

@ -1,4 +1,5 @@
<?php namespace CodeIgniter\HTTP;
<?php
namespace CodeIgniter\HTTP;
use CodeIgniter\HTTP\Exceptions\HTTPException;
@ -493,7 +494,7 @@ class URI
{
return self::createURIString(
$this->getScheme(), $this->getAuthority(), $this->getPath(), // Absolute URIs should use a "/" for an empty path
$this->getQuery(), $this->getFragment()
$this->getQuery(), $this->getFragment()
);
}
@ -737,9 +738,7 @@ class URI
// This won't catch all cases, specifically
// changing ' ' to '+' has the same length
// but doesn't really matter for our cases here.
return strlen($decoded) < strlen($value)
? $decoded
: $value;
return strlen($decoded) < strlen($value) ? $decoded : $value;
}
/**
@ -898,7 +897,8 @@ class URI
// Encode characters
$path = preg_replace_callback(
'/(?:[^' . self::CHAR_UNRESERVED . ':@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/', function(array $matches) {
'/(?:[^' . self::CHAR_UNRESERVED . ':@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/', function(array $matches)
{
return rawurlencode($matches[0]);
}, $path
);
@ -951,13 +951,8 @@ class URI
{
if ( ! is_null($parts['port']))
{
// Valid port numbers are enforced by earlier parse_url or setPort()
$port = (int) $parts['port'];
if (1 > $port || 0xffff < $port)
{
throw HTTPException::forInvalidPort($port);
}
$this->port = $port;
}
}

View File

@ -1,5 +1,4 @@
<?php
namespace CodeIgniter\HTTP;
use CodeIgniter\HTTP\Exceptions\HTTPException;
@ -16,7 +15,7 @@ class URITest extends \CIUnitTestCase
public function tearDown()
{
}
//--------------------------------------------------------------------
@ -55,6 +54,16 @@ class URITest extends \CIUnitTestCase
//--------------------------------------------------------------------
public function testSegmentOutOfRange()
{
$this->expectException(HTTPException::class);
$url = "http://abc.com/a123/b/c";
$uri = new URI($url);
$uri->getSegment(22);
}
//--------------------------------------------------------------------
public function testCanCastAsString()
{
$url = 'http://username:password@hostname:9090/path?arg=value#anchor';
@ -84,10 +93,19 @@ class URITest extends \CIUnitTestCase
{
$url = '';
$uri = new URI($url);
$this->assertEquals('http://'.$url, (string) $uri);
$this->assertEquals('http://' . $url, (string) $uri);
$url = '/';
$uri = new URI($url);
$this->assertEquals('http://'.$url, (string) $uri);
$this->assertEquals('http://' . $url, (string) $uri);
}
//--------------------------------------------------------------------
public function testMalformedUri()
{
$this->expectException(HTTPException::class);
$url = "http://abc:a123";
$uri = new URI($url);
}
//--------------------------------------------------------------------
@ -107,9 +125,9 @@ class URITest extends \CIUnitTestCase
public function testSchemeSub()
{
$url = 'example.com';
$uri = new URI('http://'.$url);
$uri = new URI('http://' . $url);
$uri->setScheme('x');
$this->assertEquals('x://'.$url, (string) $uri);
$this->assertEquals('x://' . $url, (string) $uri);
}
//--------------------------------------------------------------------
@ -230,6 +248,16 @@ class URITest extends \CIUnitTestCase
//--------------------------------------------------------------------
public function testCatchesBadPort()
{
$this->expectException(HTTPException::class);
$url = 'http://username:password@hostname:90909/path?arg=value#anchor';
$uri = new URI();
$uri->setURI($url);
}
//--------------------------------------------------------------------
public function testSetPathSetsValue()
{
$url = 'http://example.com/path';
@ -427,11 +455,11 @@ class URITest extends \CIUnitTestCase
['g/', 'http://a/b/c/g/'],
['/g', 'http://a/g'],
['#s', 'http://a/b/c/d#s'],
['http://abc.com/x', 'http://abc.com/x'],
['?fruit=banana', 'http://a/b/c/d?fruit=banana'],
];
}
//--------------------------------------------------------------------
/**
* @dataProvider defaultResolutions
*/
@ -446,36 +474,34 @@ class URITest extends \CIUnitTestCase
$this->assertEquals($expected, (string) $new);
}
/**
* @dataProvider defaultResolutions
* @group single
*/
public function testResolveRelativeURIHTTPS($rel, $expected)
{
$base = 'https://a/b/c/d';
$expected = str_replace('http:', 'https:', $expected);
$uri = new URI($base);
$new = $uri->resolveRelativeURI($rel);
$this->assertEquals($expected, (string) $new);
}
//--------------------------------------------------------------------
/**
* @dataProvider defaultResolutions
* @group single
*/
public function testResolveRelativeURIHTTPS($rel, $expected)
{
$base = 'https://a/b/c/d';
$expected = str_replace('http:', 'https:', $expected);
$uri = new URI($base);
$new = $uri->resolveRelativeURI($rel);
$this->assertEquals($expected, (string) $new);
}
//--------------------------------------------------------------------
public function testAddQueryVar()
{
$base = 'http://example.com/foo';
$base = 'http://example.com/foo';
$uri = new URI($base);
$uri->addQuery('bar', 'baz');
$this->assertEquals('http://example.com/foo?bar=baz', (string)$uri);
$this->assertEquals('http://example.com/foo?bar=baz', (string) $uri);
}
//--------------------------------------------------------------------
@ -505,7 +531,7 @@ class URITest extends \CIUnitTestCase
$uri->addQuery('baz', 'foz');
$this->assertEquals('http://example.com/foo?bar=baz&baz=foz', (string)$uri);
$this->assertEquals('http://example.com/foo?bar=baz&baz=foz', (string) $uri);
}
//--------------------------------------------------------------------
@ -518,7 +544,7 @@ class URITest extends \CIUnitTestCase
$uri->stripQuery('bar', 'baz');
$this->assertEquals('http://example.com/foo?foo=bar', (string)$uri);
$this->assertEquals('http://example.com/foo?foo=bar', (string) $uri);
}
//--------------------------------------------------------------------
@ -531,7 +557,18 @@ class URITest extends \CIUnitTestCase
$uri->keepQuery('bar', 'baz');
$this->assertEquals('http://example.com/foo?bar=baz&baz=foz', (string)$uri);
$this->assertEquals('http://example.com/foo?bar=baz&baz=foz', (string) $uri);
}
//--------------------------------------------------------------------
public function testEmptyQueryVars()
{
$base = 'http://example.com/foo';
$uri = new URI($base);
$uri->setQuery('foo=&bar=baz&baz=foz');
$this->assertEquals('http://example.com/foo?foo=&bar=baz&baz=foz', (string) $uri);
}
//--------------------------------------------------------------------
@ -558,15 +595,15 @@ class URITest extends \CIUnitTestCase
//--------------------------------------------------------------------
/**
* @see https://github.com/bcit-ci/CodeIgniter4/issues/331
* @group single
*/
public function testNoExtraSlashes()
{
$this->assertEquals('http://entirely.different.com/subfolder', (string)(new URI('entirely.different.com/subfolder')));
$this->assertEquals('http://localhost/subfolder', (string)(new URI('localhost/subfolder')));
$this->assertEquals('http://localtest.me/subfolder', (string)(new URI('localtest.me/subfolder')));
}
/**
* @see https://github.com/bcit-ci/CodeIgniter4/issues/331
* @group single
*/
public function testNoExtraSlashes()
{
$this->assertEquals('http://entirely.different.com/subfolder', (string) (new URI('entirely.different.com/subfolder')));
$this->assertEquals('http://localhost/subfolder', (string) (new URI('localhost/subfolder')));
$this->assertEquals('http://localtest.me/subfolder', (string) (new URI('localtest.me/subfolder')));
}
}