1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Tests\Psr7\Factory;
use PHPUnit\Framework\TestCase as UriFactoryTestCase;
//use Interop\Http\Factory\UriFactoryTestCase;
use Slim\Psr7\Environment;
use Slim\Psr7\Factory\UriFactory;
/**
* @group excluded
*/
#[\PHPUnit\Framework\Attributes\Group('excluded')]
class UriFactoryTest extends UriFactoryTestCase
{
protected function createUriFactory(): UriFactory
{
return new UriFactory();
}
public function testGetAuthorityWithUsername()
{
$uri = $this->createUriFactory()->createUri('https://josh@example.com/foo/bar?abc=123#section3');
$this->assertEquals('josh@example.com', $uri->getAuthority());
}
public function testGetAuthority()
{
$uri = $this->createUriFactory()->createUri('https://example.com/foo/bar?abc=123#section3');
$this->assertEquals('example.com', $uri->getAuthority());
}
public function testGetAuthorityWithNonStandardPort()
{
$uri = $this->createUriFactory()->createUri('https://example.com:400/foo/bar?abc=123#section3');
$this->assertEquals('example.com:400', $uri->getAuthority());
}
public function testGetUserInfoWithUsernameAndPassword()
{
$uri = $this->createUriFactory()->createUri('https://josh:sekrit@example.com:443/foo/bar?abc=123#section3');
$this->assertEquals('josh:sekrit', $uri->getUserInfo());
}
public function testGetUserInfoWithUsernameAndPasswordEncodesCorrectly()
{
$uri = $this
->createUriFactory()
->createUri('https://bob@example.com:pass:word@example.com:443/foo/bar?abc=123#section3');
$this->assertEquals('bob%40example.com:pass%3Aword', $uri->getUserInfo());
}
public function testGetUserInfoWithUsername()
{
$uri = $this->createUriFactory()->createUri('http://josh@example.com/foo/bar?abc=123#section3');
$this->assertEquals('josh', $uri->getUserInfo());
}
public function testGetUserInfoNone()
{
$uri = $this->createUriFactory()->createUri('https://example.com/foo/bar?abc=123#section3');
$this->assertEquals('', $uri->getUserInfo());
}
public function testCreateFromString()
{
$uri = $this->createUriFactory()->createUri('https://example.com:8080/foo/bar?abc=123');
$this->assertEquals('https', $uri->getScheme());
$this->assertEquals('example.com', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
}
public function testCreateFromGlobals()
{
$globals = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar?baz=1',
'PHP_AUTH_USER' => 'josh',
'PHP_AUTH_PW' => 'sekrit',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => 'example.com:8080',
'SERVER_PORT' => 8080,
]);
$uri = $this->createUriFactory()->createFromGlobals($globals);
$this->assertEquals('josh:sekrit', $uri->getUserInfo());
$this->assertEquals('example.com', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());
}
public function testCreateFromGlobalsWithHttps()
{
$globals = Environment::mock(
[
'HTTPS' => 'on',
'HTTP_HOST' => 'example.com'
]
);
// Make the 'SERVER_PORT' empty as we want to test if the default server port gets set correctly.
$globals['SERVER_PORT'] = '';
$uri = $this->createUriFactory()->createFromGlobals($globals);
$this->assertEquals('https', $uri->getScheme());
$this->assertEquals('example.com', $uri->getHost());
// The port is expected to be NULL as the server port is the default standard (443 in case of https).
$this->assertNull($uri->getPort());
}
public function testCreateFromGlobalsUsesServerNameAsHostIfHostHeaderIsNotPresent()
{
$globals = Environment::mock([
'SERVER_NAME' => 'example.com',
]);
$uri = $this->createUriFactory()->createFromGlobals($globals);
$this->assertEquals('example.com', $uri->getHost());
}
public function testCreateFromGlobalWithIPv6HostNoPort()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'PHP_AUTH_USER' => 'josh',
'PHP_AUTH_PW' => 'sekrit',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => '[2001:db8::1]',
'REMOTE_ADDR' => '2001:db8::1',
'SERVER_PORT' => 8080,
]);
$uri = $this->createUriFactory()->createFromGlobals($environment);
$this->assertEquals('josh:sekrit', $uri->getUserInfo());
$this->assertEquals('[2001:db8::1]', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());
}
public function testCreateFromGlobalsWithIPv6HostWithPort()
{
$globals = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'PHP_AUTH_USER' => 'josh',
'PHP_AUTH_PW' => 'sekrit',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => '[2001:db8::1]:8080',
'REMOTE_ADDR' => '2001:db8::1',
'SERVER_PORT' => 8080,
]);
$uri = $this->createUriFactory()->createFromGlobals($globals);
$this->assertEquals('josh:sekrit', $uri->getUserInfo());
$this->assertEquals('[2001:db8::1]', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());
}
public function testCreateFromGlobalsWithBasePathContainingSpace()
{
$globals = Environment::mock([
'SCRIPT_NAME' => "/f'oo bar/index.php",
'REQUEST_URI' => "/f%27oo%20bar/baz",
]);
$uri = $this->createUriFactory()->createFromGlobals($globals);
$this->assertEquals('/f%27oo%20bar/baz', $uri->getPath());
}
public function testWithPathWhenBaseRootIsEmpty()
{
$globals = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/bar',
]);
$uri = $this->createUriFactory()->createFromGlobals($globals);
$this->assertEquals('http://localhost/test', (string) $uri->withPath('test'));
}
/**
* When the URL is /foo/index.php/bar/baz, we need the baseURL to be
* /foo/index.php so that routing works correctly.
*
* @ticket 1639 as a fix to 1590 broke this.
*/
public function testRequestURIContainsIndexDotPhp()
{
$uri = $this->createUriFactory()->createFromGlobals(
Environment::mock(
[
'SCRIPT_NAME' => '/foo/index.php',
'REQUEST_URI' => '/foo/index.php/bar/baz',
]
)
);
$this->assertSame('/foo/index.php/bar/baz', $uri->getPath());
}
public function testRequestURICanContainParams()
{
$uri = $this->createUriFactory()->createFromGlobals(
Environment::mock(
[
'REQUEST_URI' => '/foo?abc=123',
]
)
);
$this->assertEquals('abc=123', $uri->getQuery());
}
public function testUriDistinguishZeroFromEmptyString()
{
$expected = 'https://0:0@0:1/0?0#0';
$this->assertSame($expected, (string) $this->createUriFactory()->createUri($expected));
}
}
|