ServerRequestTest.php 19 KB

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