123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- <?php
- namespace GuzzleHttp\Tests\Psr7;
- use GuzzleHttp\Psr7\ServerRequest;
- use GuzzleHttp\Psr7\UploadedFile;
- use GuzzleHttp\Psr7\Uri;
- /**
- * @covers GuzzleHttp\Psr7\ServerRequest
- */
- class ServerRequestTest extends BaseTest
- {
- public function dataNormalizeFiles()
- {
- return [
- 'Single file' => [
- [
- 'file' => [
- 'name' => 'MyFile.txt',
- 'type' => 'text/plain',
- 'tmp_name' => '/tmp/php/php1h4j1o',
- 'error' => '0',
- 'size' => '123'
- ]
- ],
- [
- 'file' => new UploadedFile(
- '/tmp/php/php1h4j1o',
- 123,
- UPLOAD_ERR_OK,
- 'MyFile.txt',
- 'text/plain'
- )
- ]
- ],
- 'Empty file' => [
- [
- 'image_file' => [
- 'name' => '',
- 'type' => '',
- 'tmp_name' => '',
- 'error' => '4',
- 'size' => '0'
- ]
- ],
- [
- 'image_file' => new UploadedFile(
- '',
- 0,
- UPLOAD_ERR_NO_FILE,
- '',
- ''
- )
- ]
- ],
- 'Already Converted' => [
- [
- 'file' => new UploadedFile(
- '/tmp/php/php1h4j1o',
- 123,
- UPLOAD_ERR_OK,
- 'MyFile.txt',
- 'text/plain'
- )
- ],
- [
- 'file' => new UploadedFile(
- '/tmp/php/php1h4j1o',
- 123,
- UPLOAD_ERR_OK,
- 'MyFile.txt',
- 'text/plain'
- )
- ]
- ],
- 'Already Converted array' => [
- [
- 'file' => [
- new UploadedFile(
- '/tmp/php/php1h4j1o',
- 123,
- UPLOAD_ERR_OK,
- 'MyFile.txt',
- 'text/plain'
- ),
- new UploadedFile(
- '',
- 0,
- UPLOAD_ERR_NO_FILE,
- '',
- ''
- )
- ],
- ],
- [
- 'file' => [
- new UploadedFile(
- '/tmp/php/php1h4j1o',
- 123,
- UPLOAD_ERR_OK,
- 'MyFile.txt',
- 'text/plain'
- ),
- new UploadedFile(
- '',
- 0,
- UPLOAD_ERR_NO_FILE,
- '',
- ''
- )
- ],
- ]
- ],
- 'Multiple files' => [
- [
- 'text_file' => [
- 'name' => 'MyFile.txt',
- 'type' => 'text/plain',
- 'tmp_name' => '/tmp/php/php1h4j1o',
- 'error' => '0',
- 'size' => '123'
- ],
- 'image_file' => [
- 'name' => '',
- 'type' => '',
- 'tmp_name' => '',
- 'error' => '4',
- 'size' => '0'
- ]
- ],
- [
- 'text_file' => new UploadedFile(
- '/tmp/php/php1h4j1o',
- 123,
- UPLOAD_ERR_OK,
- 'MyFile.txt',
- 'text/plain'
- ),
- 'image_file' => new UploadedFile(
- '',
- 0,
- UPLOAD_ERR_NO_FILE,
- '',
- ''
- )
- ]
- ],
- 'Nested files' => [
- [
- 'file' => [
- 'name' => [
- 0 => 'MyFile.txt',
- 1 => 'Image.png',
- ],
- 'type' => [
- 0 => 'text/plain',
- 1 => 'image/png',
- ],
- 'tmp_name' => [
- 0 => '/tmp/php/hp9hskjhf',
- 1 => '/tmp/php/php1h4j1o',
- ],
- 'error' => [
- 0 => '0',
- 1 => '0',
- ],
- 'size' => [
- 0 => '123',
- 1 => '7349',
- ],
- ],
- 'nested' => [
- 'name' => [
- 'other' => 'Flag.txt',
- 'test' => [
- 0 => 'Stuff.txt',
- 1 => '',
- ],
- ],
- 'type' => [
- 'other' => 'text/plain',
- 'test' => [
- 0 => 'text/plain',
- 1 => '',
- ],
- ],
- 'tmp_name' => [
- 'other' => '/tmp/php/hp9hskjhf',
- 'test' => [
- 0 => '/tmp/php/asifu2gp3',
- 1 => '',
- ],
- ],
- 'error' => [
- 'other' => '0',
- 'test' => [
- 0 => '0',
- 1 => '4',
- ],
- ],
- 'size' => [
- 'other' => '421',
- 'test' => [
- 0 => '32',
- 1 => '0',
- ]
- ]
- ],
- ],
- [
- 'file' => [
- 0 => new UploadedFile(
- '/tmp/php/hp9hskjhf',
- 123,
- UPLOAD_ERR_OK,
- 'MyFile.txt',
- 'text/plain'
- ),
- 1 => new UploadedFile(
- '/tmp/php/php1h4j1o',
- 7349,
- UPLOAD_ERR_OK,
- 'Image.png',
- 'image/png'
- ),
- ],
- 'nested' => [
- 'other' => new UploadedFile(
- '/tmp/php/hp9hskjhf',
- 421,
- UPLOAD_ERR_OK,
- 'Flag.txt',
- 'text/plain'
- ),
- 'test' => [
- 0 => new UploadedFile(
- '/tmp/php/asifu2gp3',
- 32,
- UPLOAD_ERR_OK,
- 'Stuff.txt',
- 'text/plain'
- ),
- 1 => new UploadedFile(
- '',
- 0,
- UPLOAD_ERR_NO_FILE,
- '',
- ''
- ),
- ]
- ]
- ]
- ]
- ];
- }
- /**
- * @dataProvider dataNormalizeFiles
- */
- public function testNormalizeFiles($files, $expected)
- {
- $result = ServerRequest::normalizeFiles($files);
- $this->assertEquals($expected, $result);
- }
- public function testNormalizeFilesRaisesException()
- {
- $this->expectException('InvalidArgumentException', 'Invalid value in files specification');
- ServerRequest::normalizeFiles(['test' => 'something']);
- }
- public function dataGetUriFromGlobals()
- {
- $server = [
- 'REQUEST_URI' => '/blog/article.php?id=10&user=foo',
- 'SERVER_PORT' => '443',
- 'SERVER_ADDR' => '217.112.82.20',
- 'SERVER_NAME' => 'www.example.org',
- 'SERVER_PROTOCOL' => 'HTTP/1.1',
- 'REQUEST_METHOD' => 'POST',
- 'QUERY_STRING' => 'id=10&user=foo',
- 'DOCUMENT_ROOT' => '/path/to/your/server/root/',
- 'HTTP_HOST' => 'www.example.org',
- 'HTTPS' => 'on',
- 'REMOTE_ADDR' => '193.60.168.69',
- 'REMOTE_PORT' => '5390',
- 'SCRIPT_NAME' => '/blog/article.php',
- 'SCRIPT_FILENAME' => '/path/to/your/server/root/blog/article.php',
- 'PHP_SELF' => '/blog/article.php',
- ];
- return [
- 'HTTPS request' => [
- 'https://www.example.org/blog/article.php?id=10&user=foo',
- $server,
- ],
- 'HTTPS request with different on value' => [
- 'https://www.example.org/blog/article.php?id=10&user=foo',
- array_merge($server, ['HTTPS' => '1']),
- ],
- 'HTTP request' => [
- 'http://www.example.org/blog/article.php?id=10&user=foo',
- array_merge($server, ['HTTPS' => 'off', 'SERVER_PORT' => '80']),
- ],
- 'HTTP_HOST missing -> fallback to SERVER_NAME' => [
- 'https://www.example.org/blog/article.php?id=10&user=foo',
- array_merge($server, ['HTTP_HOST' => null]),
- ],
- 'HTTP_HOST and SERVER_NAME missing -> fallback to SERVER_ADDR' => [
- 'https://217.112.82.20/blog/article.php?id=10&user=foo',
- array_merge($server, ['HTTP_HOST' => null, 'SERVER_NAME' => null]),
- ],
- 'Query string with ?' => [
- 'https://www.example.org/path?continue=https://example.com/path?param=1',
- array_merge($server, ['REQUEST_URI' => '/path?continue=https://example.com/path?param=1', 'QUERY_STRING' => '']),
- ],
- 'No query String' => [
- 'https://www.example.org/blog/article.php',
- array_merge($server, ['REQUEST_URI' => '/blog/article.php', 'QUERY_STRING' => '']),
- ],
- 'Host header with port' => [
- 'https://www.example.org:8324/blog/article.php?id=10&user=foo',
- array_merge($server, ['HTTP_HOST' => 'www.example.org:8324']),
- ],
- 'IPv6 local loopback address' => [
- 'https://[::1]:8000/blog/article.php?id=10&user=foo',
- array_merge($server, ['HTTP_HOST' => '[::1]:8000']),
- ],
- 'Invalid host' => [
- 'https://localhost/blog/article.php?id=10&user=foo',
- array_merge($server, ['HTTP_HOST' => 'a:b']),
- ],
- 'Different port with SERVER_PORT' => [
- 'https://www.example.org:8324/blog/article.php?id=10&user=foo',
- array_merge($server, ['SERVER_PORT' => '8324']),
- ],
- 'REQUEST_URI missing query string' => [
- 'https://www.example.org/blog/article.php?id=10&user=foo',
- array_merge($server, ['REQUEST_URI' => '/blog/article.php']),
- ],
- 'Empty server variable' => [
- 'http://localhost',
- [],
- ],
- ];
- }
- /**
- * @dataProvider dataGetUriFromGlobals
- */
- public function testGetUriFromGlobals($expected, $serverParams)
- {
- $_SERVER = $serverParams;
- $this->assertEquals(new Uri($expected), ServerRequest::getUriFromGlobals());
- }
- public function testFromGlobals()
- {
- $_SERVER = [
- 'REQUEST_URI' => '/blog/article.php?id=10&user=foo',
- 'SERVER_PORT' => '443',
- 'SERVER_ADDR' => '217.112.82.20',
- 'SERVER_NAME' => 'www.example.org',
- 'SERVER_PROTOCOL' => 'HTTP/1.1',
- 'REQUEST_METHOD' => 'POST',
- 'QUERY_STRING' => 'id=10&user=foo',
- 'DOCUMENT_ROOT' => '/path/to/your/server/root/',
- 'CONTENT_TYPE' => 'text/plain',
- 'HTTP_HOST' => 'www.example.org',
- 'HTTP_ACCEPT' => 'text/html',
- 'HTTP_REFERRER' => 'https://example.com',
- 'HTTP_USER_AGENT' => 'My User Agent',
- 'HTTPS' => 'on',
- 'REMOTE_ADDR' => '193.60.168.69',
- 'REMOTE_PORT' => '5390',
- 'SCRIPT_NAME' => '/blog/article.php',
- 'SCRIPT_FILENAME' => '/path/to/your/server/root/blog/article.php',
- 'PHP_SELF' => '/blog/article.php',
- ];
- $_COOKIE = [
- 'logged-in' => 'yes!'
- ];
- $_POST = [
- 'name' => 'Pesho',
- 'email' => 'pesho@example.com',
- ];
- $_GET = [
- 'id' => 10,
- 'user' => 'foo',
- ];
- $_FILES = [
- 'file' => [
- 'name' => 'MyFile.txt',
- 'type' => 'text/plain',
- 'tmp_name' => '/tmp/php/php1h4j1o',
- 'error' => UPLOAD_ERR_OK,
- 'size' => 123,
- ]
- ];
- $server = ServerRequest::fromGlobals();
- $this->assertSame('POST', $server->getMethod());
- $this->assertEquals([
- 'Host' => ['www.example.org'],
- 'Content-Type' => ['text/plain'],
- 'Accept' => ['text/html'],
- 'Referrer' => ['https://example.com'],
- 'User-Agent' => ['My User Agent'],
- ], $server->getHeaders());
- $this->assertSame('', (string) $server->getBody());
- $this->assertSame('1.1', $server->getProtocolVersion());
- $this->assertEquals($_COOKIE, $server->getCookieParams());
- $this->assertEquals($_POST, $server->getParsedBody());
- $this->assertEquals($_GET, $server->getQueryParams());
- $this->assertEquals(
- new Uri('https://www.example.org/blog/article.php?id=10&user=foo'),
- $server->getUri()
- );
- $expectedFiles = [
- 'file' => new UploadedFile(
- '/tmp/php/php1h4j1o',
- 123,
- UPLOAD_ERR_OK,
- 'MyFile.txt',
- 'text/plain'
- ),
- ];
- $this->assertEquals($expectedFiles, $server->getUploadedFiles());
- }
- public function testUploadedFiles()
- {
- $request1 = new ServerRequest('GET', '/');
- $files = [
- 'file' => new UploadedFile('test', 123, UPLOAD_ERR_OK)
- ];
- $request2 = $request1->withUploadedFiles($files);
- $this->assertNotSame($request2, $request1);
- $this->assertSame([], $request1->getUploadedFiles());
- $this->assertSame($files, $request2->getUploadedFiles());
- }
- public function testServerParams()
- {
- $params = ['name' => 'value'];
- $request = new ServerRequest('GET', '/', [], null, '1.1', $params);
- $this->assertSame($params, $request->getServerParams());
- }
- public function testCookieParams()
- {
- $request1 = new ServerRequest('GET', '/');
- $params = ['name' => 'value'];
- $request2 = $request1->withCookieParams($params);
- $this->assertNotSame($request2, $request1);
- $this->assertEmpty($request1->getCookieParams());
- $this->assertSame($params, $request2->getCookieParams());
- }
- public function testQueryParams()
- {
- $request1 = new ServerRequest('GET', '/');
- $params = ['name' => 'value'];
- $request2 = $request1->withQueryParams($params);
- $this->assertNotSame($request2, $request1);
- $this->assertEmpty($request1->getQueryParams());
- $this->assertSame($params, $request2->getQueryParams());
- }
- public function testParsedBody()
- {
- $request1 = new ServerRequest('GET', '/');
- $params = ['name' => 'value'];
- $request2 = $request1->withParsedBody($params);
- $this->assertNotSame($request2, $request1);
- $this->assertEmpty($request1->getParsedBody());
- $this->assertSame($params, $request2->getParsedBody());
- }
- public function testAttributes()
- {
- $request1 = new ServerRequest('GET', '/');
- $request2 = $request1->withAttribute('name', 'value');
- $request3 = $request2->withAttribute('other', 'otherValue');
- $request4 = $request3->withoutAttribute('other');
- $request5 = $request3->withoutAttribute('unknown');
- $this->assertNotSame($request2, $request1);
- $this->assertNotSame($request3, $request2);
- $this->assertNotSame($request4, $request3);
- $this->assertSame($request5, $request3);
- $this->assertSame([], $request1->getAttributes());
- $this->assertNull($request1->getAttribute('name'));
- $this->assertSame(
- 'something',
- $request1->getAttribute('name', 'something'),
- 'Should return the default value'
- );
- $this->assertSame('value', $request2->getAttribute('name'));
- $this->assertSame(['name' => 'value'], $request2->getAttributes());
- $this->assertEquals(['name' => 'value', 'other' => 'otherValue'], $request3->getAttributes());
- $this->assertSame(['name' => 'value'], $request4->getAttributes());
- }
- public function testNullAttribute()
- {
- $request = (new ServerRequest('GET', '/'))->withAttribute('name', null);
- $this->assertSame(['name' => null], $request->getAttributes());
- $this->assertNull($request->getAttribute('name', 'different-default'));
- $requestWithoutAttribute = $request->withoutAttribute('name');
- $this->assertSame([], $requestWithoutAttribute->getAttributes());
- $this->assertSame('different-default', $requestWithoutAttribute->getAttribute('name', 'different-default'));
- }
- }
|