| 1 |
<?php |
| 2 |
namespace GuzzleHttp\Tests\Psr7; |
| 3 |
|
| 4 |
use GuzzleHttp\Psr7\ServerRequest; |
| 5 |
use GuzzleHttp\Psr7\UploadedFile; |
| 6 |
use GuzzleHttp\Psr7\Uri; |
| 7 |
|
| 8 |
/** |
| 9 |
* @covers GuzzleHttp\Psr7\ServerRequest |
| 10 |
*/ |
| 11 |
class ServerRequestTest extends BaseTest |
| 12 |
{ |
| 13 |
public function dataNormalizeFiles() |
| 14 |
{ |
| 15 |
return [ |
| 16 |
'Single file' => [ |
| 17 |
[ |
| 18 |
'file' => [ |
| 19 |
'name' => 'MyFile.txt', |
| 20 |
'type' => 'text/plain', |
| 21 |
'tmp_name' => '/tmp/php/php1h4j1o', |
| 22 |
'error' => '0', |
| 23 |
'size' => '123' |
| 24 |
] |
| 25 |
], |
| 26 |
[ |
| 27 |
'file' => new UploadedFile( |
| 28 |
'/tmp/php/php1h4j1o', |
| 29 |
123, |
| 30 |
UPLOAD_ERR_OK, |
| 31 |
'MyFile.txt', |
| 32 |
'text/plain' |
| 33 |
) |
| 34 |
] |
| 35 |
], |
| 36 |
'Empty file' => [ |
| 37 |
[ |
| 38 |
'image_file' => [ |
| 39 |
'name' => '', |
| 40 |
'type' => '', |
| 41 |
'tmp_name' => '', |
| 42 |
'error' => '4', |
| 43 |
'size' => '0' |
| 44 |
] |
| 45 |
], |
| 46 |
[ |
| 47 |
'image_file' => new UploadedFile( |
| 48 |
'', |
| 49 |
0, |
| 50 |
UPLOAD_ERR_NO_FILE, |
| 51 |
'', |
| 52 |
'' |
| 53 |
) |
| 54 |
] |
| 55 |
], |
| 56 |
'Already Converted' => [ |
| 57 |
[ |
| 58 |
'file' => new UploadedFile( |
| 59 |
'/tmp/php/php1h4j1o', |
| 60 |
123, |
| 61 |
UPLOAD_ERR_OK, |
| 62 |
'MyFile.txt', |
| 63 |
'text/plain' |
| 64 |
) |
| 65 |
], |
| 66 |
[ |
| 67 |
'file' => new UploadedFile( |
| 68 |
'/tmp/php/php1h4j1o', |
| 69 |
123, |
| 70 |
UPLOAD_ERR_OK, |
| 71 |
'MyFile.txt', |
| 72 |
'text/plain' |
| 73 |
) |
| 74 |
] |
| 75 |
], |
| 76 |
'Already Converted array' => [ |
| 77 |
[ |
| 78 |
'file' => [ |
| 79 |
new UploadedFile( |
| 80 |
'/tmp/php/php1h4j1o', |
| 81 |
123, |
| 82 |
UPLOAD_ERR_OK, |
| 83 |
'MyFile.txt', |
| 84 |
'text/plain' |
| 85 |
), |
| 86 |
new UploadedFile( |
| 87 |
'', |
| 88 |
0, |
| 89 |
UPLOAD_ERR_NO_FILE, |
| 90 |
'', |
| 91 |
'' |
| 92 |
) |
| 93 |
], |
| 94 |
], |
| 95 |
[ |
| 96 |
'file' => [ |
| 97 |
new UploadedFile( |
| 98 |
'/tmp/php/php1h4j1o', |
| 99 |
123, |
| 100 |
UPLOAD_ERR_OK, |
| 101 |
'MyFile.txt', |
| 102 |
'text/plain' |
| 103 |
), |
| 104 |
new UploadedFile( |
| 105 |
'', |
| 106 |
0, |
| 107 |
UPLOAD_ERR_NO_FILE, |
| 108 |
'', |
| 109 |
'' |
| 110 |
) |
| 111 |
], |
| 112 |
] |
| 113 |
], |
| 114 |
'Multiple files' => [ |
| 115 |
[ |
| 116 |
'text_file' => [ |
| 117 |
'name' => 'MyFile.txt', |
| 118 |
'type' => 'text/plain', |
| 119 |
'tmp_name' => '/tmp/php/php1h4j1o', |
| 120 |
'error' => '0', |
| 121 |
'size' => '123' |
| 122 |
], |
| 123 |
'image_file' => [ |
| 124 |
'name' => '', |
| 125 |
'type' => '', |
| 126 |
'tmp_name' => '', |
| 127 |
'error' => '4', |
| 128 |
'size' => '0' |
| 129 |
] |
| 130 |
], |
| 131 |
[ |
| 132 |
'text_file' => new UploadedFile( |
| 133 |
'/tmp/php/php1h4j1o', |
| 134 |
123, |
| 135 |
UPLOAD_ERR_OK, |
| 136 |
'MyFile.txt', |
| 137 |
'text/plain' |
| 138 |
), |
| 139 |
'image_file' => new UploadedFile( |
| 140 |
'', |
| 141 |
0, |
| 142 |
UPLOAD_ERR_NO_FILE, |
| 143 |
'', |
| 144 |
'' |
| 145 |
) |
| 146 |
] |
| 147 |
], |
| 148 |
'Nested files' => [ |
| 149 |
[ |
| 150 |
'file' => [ |
| 151 |
'name' => [ |
| 152 |
0 => 'MyFile.txt', |
| 153 |
1 => 'Image.png', |
| 154 |
], |
| 155 |
'type' => [ |
| 156 |
0 => 'text/plain', |
| 157 |
1 => 'image/png', |
| 158 |
], |
| 159 |
'tmp_name' => [ |
| 160 |
0 => '/tmp/php/hp9hskjhf', |
| 161 |
1 => '/tmp/php/php1h4j1o', |
| 162 |
], |
| 163 |
'error' => [ |
| 164 |
0 => '0', |
| 165 |
1 => '0', |
| 166 |
], |
| 167 |
'size' => [ |
| 168 |
0 => '123', |
| 169 |
1 => '7349', |
| 170 |
], |
| 171 |
], |
| 172 |
'nested' => [ |
| 173 |
'name' => [ |
| 174 |
'other' => 'Flag.txt', |
| 175 |
'test' => [ |
| 176 |
0 => 'Stuff.txt', |
| 177 |
1 => '', |
| 178 |
], |
| 179 |
], |
| 180 |
'type' => [ |
| 181 |
'other' => 'text/plain', |
| 182 |
'test' => [ |
| 183 |
0 => 'text/plain', |
| 184 |
1 => '', |
| 185 |
], |
| 186 |
], |
| 187 |
'tmp_name' => [ |
| 188 |
'other' => '/tmp/php/hp9hskjhf', |
| 189 |
'test' => [ |
| 190 |
0 => '/tmp/php/asifu2gp3', |
| 191 |
1 => '', |
| 192 |
], |
| 193 |
], |
| 194 |
'error' => [ |
| 195 |
'other' => '0', |
| 196 |
'test' => [ |
| 197 |
0 => '0', |
| 198 |
1 => '4', |
| 199 |
], |
| 200 |
], |
| 201 |
'size' => [ |
| 202 |
'other' => '421', |
| 203 |
'test' => [ |
| 204 |
0 => '32', |
| 205 |
1 => '0', |
| 206 |
] |
| 207 |
] |
| 208 |
], |
| 209 |
], |
| 210 |
[ |
| 211 |
'file' => [ |
| 212 |
0 => new UploadedFile( |
| 213 |
'/tmp/php/hp9hskjhf', |
| 214 |
123, |
| 215 |
UPLOAD_ERR_OK, |
| 216 |
'MyFile.txt', |
| 217 |
'text/plain' |
| 218 |
), |
| 219 |
1 => new UploadedFile( |
| 220 |
'/tmp/php/php1h4j1o', |
| 221 |
7349, |
| 222 |
UPLOAD_ERR_OK, |
| 223 |
'Image.png', |
| 224 |
'image/png' |
| 225 |
), |
| 226 |
], |
| 227 |
'nested' => [ |
| 228 |
'other' => new UploadedFile( |
| 229 |
'/tmp/php/hp9hskjhf', |
| 230 |
421, |
| 231 |
UPLOAD_ERR_OK, |
| 232 |
'Flag.txt', |
| 233 |
'text/plain' |
| 234 |
), |
| 235 |
'test' => [ |
| 236 |
0 => new UploadedFile( |
| 237 |
'/tmp/php/asifu2gp3', |
| 238 |
32, |
| 239 |
UPLOAD_ERR_OK, |
| 240 |
'Stuff.txt', |
| 241 |
'text/plain' |
| 242 |
), |
| 243 |
1 => new UploadedFile( |
| 244 |
'', |
| 245 |
0, |
| 246 |
UPLOAD_ERR_NO_FILE, |
| 247 |
'', |
| 248 |
'' |
| 249 |
), |
| 250 |
] |
| 251 |
] |
| 252 |
] |
| 253 |
] |
| 254 |
]; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* @dataProvider dataNormalizeFiles |
| 259 |
*/ |
| 260 |
public function testNormalizeFiles($files, $expected) |
| 261 |
{ |
| 262 |
$result = ServerRequest::normalizeFiles($files); |
| 263 |
|
| 264 |
$this->assertEquals($expected, $result); |
| 265 |
} |
| 266 |
|
| 267 |
public function testNormalizeFilesRaisesException() |
| 268 |
{ |
| 269 |
$this->expectException('InvalidArgumentException', 'Invalid value in files specification'); |
| 270 |
ServerRequest::normalizeFiles(['test' => 'something']); |
| 271 |
} |
| 272 |
|
| 273 |
public function dataGetUriFromGlobals() |
| 274 |
{ |
| 275 |
$server = [ |
| 276 |
'REQUEST_URI' => '/blog/article.php?id=10&user=foo', |
| 277 |
'SERVER_PORT' => '443', |
| 278 |
'SERVER_ADDR' => '217.112.82.20', |
| 279 |
'SERVER_NAME' => 'www.example.org', |
| 280 |
'SERVER_PROTOCOL' => 'HTTP/1.1', |
| 281 |
'REQUEST_METHOD' => 'POST', |
| 282 |
'QUERY_STRING' => 'id=10&user=foo', |
| 283 |
'DOCUMENT_ROOT' => '/path/to/your/server/root/', |
| 284 |
'HTTP_HOST' => 'www.example.org', |
| 285 |
'HTTPS' => 'on', |
| 286 |
'REMOTE_ADDR' => '193.60.168.69', |
| 287 |
'REMOTE_PORT' => '5390', |
| 288 |
'SCRIPT_NAME' => '/blog/article.php', |
| 289 |
'SCRIPT_FILENAME' => '/path/to/your/server/root/blog/article.php', |
| 290 |
'PHP_SELF' => '/blog/article.php', |
| 291 |
]; |
| 292 |
|
| 293 |
return [ |
| 294 |
'HTTPS request' => [ |
| 295 |
'https://www.example.org/blog/article.php?id=10&user=foo', |
| 296 |
$server, |
| 297 |
], |
| 298 |
'HTTPS request with different on value' => [ |
| 299 |
'https://www.example.org/blog/article.php?id=10&user=foo', |
| 300 |
array_merge($server, ['HTTPS' => '1']), |
| 301 |
], |
| 302 |
'HTTP request' => [ |
| 303 |
'http://www.example.org/blog/article.php?id=10&user=foo', |
| 304 |
array_merge($server, ['HTTPS' => 'off', 'SERVER_PORT' => '80']), |
| 305 |
], |
| 306 |
'HTTP_HOST missing -> fallback to SERVER_NAME' => [ |
| 307 |
'https://www.example.org/blog/article.php?id=10&user=foo', |
| 308 |
array_merge($server, ['HTTP_HOST' => null]), |
| 309 |
], |
| 310 |
'HTTP_HOST and SERVER_NAME missing -> fallback to SERVER_ADDR' => [ |
| 311 |
'https://217.112.82.20/blog/article.php?id=10&user=foo', |
| 312 |
array_merge($server, ['HTTP_HOST' => null, 'SERVER_NAME' => null]), |
| 313 |
], |
| 314 |
'Query string with ?' => [ |
| 315 |
'https://www.example.org/path?continue=https://example.com/path?param=1', |
| 316 |
array_merge($server, ['REQUEST_URI' => '/path?continue=https://example.com/path?param=1', 'QUERY_STRING' => '']), |
| 317 |
], |
| 318 |
'No query String' => [ |
| 319 |
'https://www.example.org/blog/article.php', |
| 320 |
array_merge($server, ['REQUEST_URI' => '/blog/article.php', 'QUERY_STRING' => '']), |
| 321 |
], |
| 322 |
'Host header with port' => [ |
| 323 |
'https://www.example.org:8324/blog/article.php?id=10&user=foo', |
| 324 |
array_merge($server, ['HTTP_HOST' => 'www.example.org:8324']), |
| 325 |
], |
| 326 |
'IPv6 local loopback address' => [ |
| 327 |
'https://[::1]:8000/blog/article.php?id=10&user=foo', |
| 328 |
array_merge($server, ['HTTP_HOST' => '[::1]:8000']), |
| 329 |
], |
| 330 |
'Invalid host' => [ |
| 331 |
'https://localhost/blog/article.php?id=10&user=foo', |
| 332 |
array_merge($server, ['HTTP_HOST' => 'a:b']), |
| 333 |
], |
| 334 |
'Different port with SERVER_PORT' => [ |
| 335 |
'https://www.example.org:8324/blog/article.php?id=10&user=foo', |
| 336 |
array_merge($server, ['SERVER_PORT' => '8324']), |
| 337 |
], |
| 338 |
'REQUEST_URI missing query string' => [ |
| 339 |
'https://www.example.org/blog/article.php?id=10&user=foo', |
| 340 |
array_merge($server, ['REQUEST_URI' => '/blog/article.php']), |
| 341 |
], |
| 342 |
'Empty server variable' => [ |
| 343 |
'http://localhost', |
| 344 |
[], |
| 345 |
], |
| 346 |
]; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* @dataProvider dataGetUriFromGlobals |
| 351 |
*/ |
| 352 |
public function testGetUriFromGlobals($expected, $serverParams) |
| 353 |
{ |
| 354 |
$_SERVER = $serverParams; |
| 355 |
|
| 356 |
$this->assertEquals(new Uri($expected), ServerRequest::getUriFromGlobals()); |
| 357 |
} |
| 358 |
|
| 359 |
public function testFromGlobals() |
| 360 |
{ |
| 361 |
$_SERVER = [ |
| 362 |
'REQUEST_URI' => '/blog/article.php?id=10&user=foo', |
| 363 |
'SERVER_PORT' => '443', |
| 364 |
'SERVER_ADDR' => '217.112.82.20', |
| 365 |
'SERVER_NAME' => 'www.example.org', |
| 366 |
'SERVER_PROTOCOL' => 'HTTP/1.1', |
| 367 |
'REQUEST_METHOD' => 'POST', |
| 368 |
'QUERY_STRING' => 'id=10&user=foo', |
| 369 |
'DOCUMENT_ROOT' => '/path/to/your/server/root/', |
| 370 |
'CONTENT_TYPE' => 'text/plain', |
| 371 |
'HTTP_HOST' => 'www.example.org', |
| 372 |
'HTTP_ACCEPT' => 'text/html', |
| 373 |
'HTTP_REFERRER' => 'https://example.com', |
| 374 |
'HTTP_USER_AGENT' => 'My User Agent', |
| 375 |
'HTTPS' => 'on', |
| 376 |
'REMOTE_ADDR' => '193.60.168.69', |
| 377 |
'REMOTE_PORT' => '5390', |
| 378 |
'SCRIPT_NAME' => '/blog/article.php', |
| 379 |
'SCRIPT_FILENAME' => '/path/to/your/server/root/blog/article.php', |
| 380 |
'PHP_SELF' => '/blog/article.php', |
| 381 |
]; |
| 382 |
|
| 383 |
$_COOKIE = [ |
| 384 |
'logged-in' => 'yes!' |
| 385 |
]; |
| 386 |
|
| 387 |
$_POST = [ |
| 388 |
'name' => 'Pesho', |
| 389 |
'email' => 'pesho@example.com', |
| 390 |
]; |
| 391 |
|
| 392 |
$_GET = [ |
| 393 |
'id' => 10, |
| 394 |
'user' => 'foo', |
| 395 |
]; |
| 396 |
|
| 397 |
$_FILES = [ |
| 398 |
'file' => [ |
| 399 |
'name' => 'MyFile.txt', |
| 400 |
'type' => 'text/plain', |
| 401 |
'tmp_name' => '/tmp/php/php1h4j1o', |
| 402 |
'error' => UPLOAD_ERR_OK, |
| 403 |
'size' => 123, |
| 404 |
] |
| 405 |
]; |
| 406 |
|
| 407 |
$server = ServerRequest::fromGlobals(); |
| 408 |
|
| 409 |
$this->assertSame('POST', $server->getMethod()); |
| 410 |
$this->assertEquals([ |
| 411 |
'Host' => ['www.example.org'], |
| 412 |
'Content-Type' => ['text/plain'], |
| 413 |
'Accept' => ['text/html'], |
| 414 |
'Referrer' => ['https://example.com'], |
| 415 |
'User-Agent' => ['My User Agent'], |
| 416 |
], $server->getHeaders()); |
| 417 |
$this->assertSame('', (string) $server->getBody()); |
| 418 |
$this->assertSame('1.1', $server->getProtocolVersion()); |
| 419 |
$this->assertEquals($_COOKIE, $server->getCookieParams()); |
| 420 |
$this->assertEquals($_POST, $server->getParsedBody()); |
| 421 |
$this->assertEquals($_GET, $server->getQueryParams()); |
| 422 |
|
| 423 |
$this->assertEquals( |
| 424 |
new Uri('https://www.example.org/blog/article.php?id=10&user=foo'), |
| 425 |
$server->getUri() |
| 426 |
); |
| 427 |
|
| 428 |
$expectedFiles = [ |
| 429 |
'file' => new UploadedFile( |
| 430 |
'/tmp/php/php1h4j1o', |
| 431 |
123, |
| 432 |
UPLOAD_ERR_OK, |
| 433 |
'MyFile.txt', |
| 434 |
'text/plain' |
| 435 |
), |
| 436 |
]; |
| 437 |
|
| 438 |
$this->assertEquals($expectedFiles, $server->getUploadedFiles()); |
| 439 |
} |
| 440 |
|
| 441 |
public function testUploadedFiles() |
| 442 |
{ |
| 443 |
$request1 = new ServerRequest('GET', '/'); |
| 444 |
|
| 445 |
$files = [ |
| 446 |
'file' => new UploadedFile('test', 123, UPLOAD_ERR_OK) |
| 447 |
]; |
| 448 |
|
| 449 |
$request2 = $request1->withUploadedFiles($files); |
| 450 |
|
| 451 |
$this->assertNotSame($request2, $request1); |
| 452 |
$this->assertSame([], $request1->getUploadedFiles()); |
| 453 |
$this->assertSame($files, $request2->getUploadedFiles()); |
| 454 |
} |
| 455 |
|
| 456 |
public function testServerParams() |
| 457 |
{ |
| 458 |
$params = ['name' => 'value']; |
| 459 |
|
| 460 |
$request = new ServerRequest('GET', '/', [], null, '1.1', $params); |
| 461 |
$this->assertSame($params, $request->getServerParams()); |
| 462 |
} |
| 463 |
|
| 464 |
public function testCookieParams() |
| 465 |
{ |
| 466 |
$request1 = new ServerRequest('GET', '/'); |
| 467 |
|
| 468 |
$params = ['name' => 'value']; |
| 469 |
|
| 470 |
$request2 = $request1->withCookieParams($params); |
| 471 |
|
| 472 |
$this->assertNotSame($request2, $request1); |
| 473 |
$this->assertEmpty($request1->getCookieParams()); |
| 474 |
$this->assertSame($params, $request2->getCookieParams()); |
| 475 |
} |
| 476 |
|
| 477 |
public function testQueryParams() |
| 478 |
{ |
| 479 |
$request1 = new ServerRequest('GET', '/'); |
| 480 |
|
| 481 |
$params = ['name' => 'value']; |
| 482 |
|
| 483 |
$request2 = $request1->withQueryParams($params); |
| 484 |
|
| 485 |
$this->assertNotSame($request2, $request1); |
| 486 |
$this->assertEmpty($request1->getQueryParams()); |
| 487 |
$this->assertSame($params, $request2->getQueryParams()); |
| 488 |
} |
| 489 |
|
| 490 |
public function testParsedBody() |
| 491 |
{ |
| 492 |
$request1 = new ServerRequest('GET', '/'); |
| 493 |
|
| 494 |
$params = ['name' => 'value']; |
| 495 |
|
| 496 |
$request2 = $request1->withParsedBody($params); |
| 497 |
|
| 498 |
$this->assertNotSame($request2, $request1); |
| 499 |
$this->assertEmpty($request1->getParsedBody()); |
| 500 |
$this->assertSame($params, $request2->getParsedBody()); |
| 501 |
} |
| 502 |
|
| 503 |
public function testAttributes() |
| 504 |
{ |
| 505 |
$request1 = new ServerRequest('GET', '/'); |
| 506 |
|
| 507 |
$request2 = $request1->withAttribute('name', 'value'); |
| 508 |
$request3 = $request2->withAttribute('other', 'otherValue'); |
| 509 |
$request4 = $request3->withoutAttribute('other'); |
| 510 |
$request5 = $request3->withoutAttribute('unknown'); |
| 511 |
|
| 512 |
$this->assertNotSame($request2, $request1); |
| 513 |
$this->assertNotSame($request3, $request2); |
| 514 |
$this->assertNotSame($request4, $request3); |
| 515 |
$this->assertSame($request5, $request3); |
| 516 |
|
| 517 |
$this->assertSame([], $request1->getAttributes()); |
| 518 |
$this->assertNull($request1->getAttribute('name')); |
| 519 |
$this->assertSame( |
| 520 |
'something', |
| 521 |
$request1->getAttribute('name', 'something'), |
| 522 |
'Should return the default value' |
| 523 |
); |
| 524 |
|
| 525 |
$this->assertSame('value', $request2->getAttribute('name')); |
| 526 |
$this->assertSame(['name' => 'value'], $request2->getAttributes()); |
| 527 |
$this->assertEquals(['name' => 'value', 'other' => 'otherValue'], $request3->getAttributes()); |
| 528 |
$this->assertSame(['name' => 'value'], $request4->getAttributes()); |
| 529 |
} |
| 530 |
|
| 531 |
public function testNullAttribute() |
| 532 |
{ |
| 533 |
$request = (new ServerRequest('GET', '/'))->withAttribute('name', null); |
| 534 |
|
| 535 |
$this->assertSame(['name' => null], $request->getAttributes()); |
| 536 |
$this->assertNull($request->getAttribute('name', 'different-default')); |
| 537 |
|
| 538 |
$requestWithoutAttribute = $request->withoutAttribute('name'); |
| 539 |
|
| 540 |
$this->assertSame([], $requestWithoutAttribute->getAttributes()); |
| 541 |
$this->assertSame('different-default', $requestWithoutAttribute->getAttribute('name', 'different-default')); |
| 542 |
} |
| 543 |
} |
| 544 |
|