RequestTransformerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http;
  16. use Cake\Core\Configure;
  17. use Cake\Http\RequestTransformer;
  18. use Cake\Http\ServerRequestFactory;
  19. use Cake\Network\Session;
  20. use Cake\TestSuite\TestCase;
  21. use Zend\Diactoros\Stream;
  22. /**
  23. * Test for RequestTransformer
  24. */
  25. class RequestTransformerTest extends TestCase
  26. {
  27. /**
  28. * Test transforming GET params.
  29. *
  30. * @return void
  31. */
  32. public function testToCakeGetParams()
  33. {
  34. $psr = ServerRequestFactory::fromGlobals(null, ['a' => 'b', 'c' => ['d' => 'e']]);
  35. $cake = RequestTransformer::toCake($psr);
  36. $this->assertEquals('b', $cake->query('a'));
  37. $this->assertEquals(['d' => 'e'], $cake->query('c'));
  38. $this->assertEmpty($cake->data);
  39. $this->assertEmpty($cake->cookies);
  40. }
  41. /**
  42. * Test transforming POST params.
  43. *
  44. * @return void
  45. */
  46. public function testToCakePostParams()
  47. {
  48. $psr = ServerRequestFactory::fromGlobals(null, null, ['title' => 'first post', 'some' => 'data']);
  49. $cake = RequestTransformer::toCake($psr);
  50. $this->assertEquals('first post', $cake->data('title'));
  51. $this->assertEquals('data', $cake->data('some'));
  52. $this->assertEmpty($cake->query);
  53. $this->assertEmpty($cake->cookies);
  54. }
  55. /**
  56. * Test transforming COOKIE params.
  57. *
  58. * @return void
  59. */
  60. public function testToCakeCookies()
  61. {
  62. $psr = ServerRequestFactory::fromGlobals(null, null, null, ['gtm' => 'watchingyou']);
  63. $cake = RequestTransformer::toCake($psr);
  64. $this->assertEmpty($cake->query);
  65. $this->assertEmpty($cake->data);
  66. $this->assertEquals('watchingyou', $cake->cookie('gtm'));
  67. }
  68. /**
  69. * Test transforming header and server params.
  70. *
  71. * @return void
  72. */
  73. public function testToCakeHeadersAndEnvironment()
  74. {
  75. $server = [
  76. 'HTTPS' => 'on',
  77. 'HTTP_HOST' => 'example.com',
  78. 'REQUEST_METHOD' => 'PATCH',
  79. 'HTTP_ACCEPT' => 'application/json',
  80. 'SERVER_PROTOCOL' => '1.1',
  81. 'SERVER_PORT' => 443,
  82. ];
  83. $psr = ServerRequestFactory::fromGlobals($server);
  84. $psr = $psr->withHeader('Api-Token', 'abc123')
  85. ->withAddedHeader('X-thing', 'one')
  86. ->withAddedHeader('X-thing', 'two');
  87. $cake = RequestTransformer::toCake($psr);
  88. $this->assertEmpty($cake->query);
  89. $this->assertEmpty($cake->data);
  90. $this->assertEmpty($cake->cookie);
  91. $this->assertSame('application/json', $cake->header('accept'));
  92. $this->assertSame('abc123', $cake->header('Api-Token'));
  93. $this->assertSame('one,two', $cake->header('X-thing'));
  94. $this->assertSame('PATCH', $cake->method());
  95. $this->assertSame('https', $cake->scheme());
  96. $this->assertSame(443, $cake->port());
  97. $this->assertSame('example.com', $cake->host());
  98. }
  99. /**
  100. * Test transforming with no routing parameters
  101. * still has the required keys.
  102. *
  103. * @return void
  104. */
  105. public function testToCakeParamsEmpty()
  106. {
  107. $psr = ServerRequestFactory::fromGlobals();
  108. $cake = RequestTransformer::toCake($psr);
  109. $this->assertArrayHasKey('controller', $cake->params);
  110. $this->assertArrayHasKey('action', $cake->params);
  111. $this->assertArrayHasKey('plugin', $cake->params);
  112. $this->assertArrayHasKey('_ext', $cake->params);
  113. $this->assertArrayHasKey('pass', $cake->params);
  114. }
  115. /**
  116. * Test transforming with non-empty params.
  117. *
  118. * @return void
  119. */
  120. public function testToCakeParamsPopulated()
  121. {
  122. $psr = ServerRequestFactory::fromGlobals();
  123. $psr = $psr->withAttribute('params', ['controller' => 'Articles', 'action' => 'index']);
  124. $cake = RequestTransformer::toCake($psr);
  125. $this->assertEmpty($cake->query);
  126. $this->assertEmpty($cake->data);
  127. $this->assertEmpty($cake->cookie);
  128. $this->assertSame('Articles', $cake->param('controller'));
  129. $this->assertSame('index', $cake->param('action'));
  130. $this->assertArrayHasKey('plugin', $cake->params);
  131. $this->assertArrayHasKey('_ext', $cake->params);
  132. $this->assertArrayHasKey('pass', $cake->params);
  133. }
  134. /**
  135. * Test transforming uploaded files
  136. *
  137. * @return void
  138. */
  139. public function testToCakeUploadedFiles()
  140. {
  141. $files = [
  142. 'no_file' => [
  143. 'name' => ['file' => ''],
  144. 'type' => ['file' => ''],
  145. 'tmp_name' => ['file' => ''],
  146. 'error' => ['file' => UPLOAD_ERR_NO_FILE],
  147. 'size' => ['file' => 0]
  148. ],
  149. 'image_main' => [
  150. 'name' => ['file' => 'born on.txt'],
  151. 'type' => ['file' => 'text/plain'],
  152. 'tmp_name' => ['file' => __FILE__],
  153. 'error' => ['file' => 0],
  154. 'size' => ['file' => 17178]
  155. ],
  156. 0 => [
  157. 'name' => ['image' => 'scratch.text'],
  158. 'type' => ['image' => 'text/plain'],
  159. 'tmp_name' => ['image' => __FILE__],
  160. 'error' => ['image' => 0],
  161. 'size' => ['image' => 1490]
  162. ],
  163. 'pictures' => [
  164. 'name' => [
  165. 0 => ['file' => 'a-file.png'],
  166. 1 => ['file' => 'a-moose.png']
  167. ],
  168. 'type' => [
  169. 0 => ['file' => 'image/png'],
  170. 1 => ['file' => 'image/jpg']
  171. ],
  172. 'tmp_name' => [
  173. 0 => ['file' => __FILE__],
  174. 1 => ['file' => __FILE__]
  175. ],
  176. 'error' => [
  177. 0 => ['file' => 0],
  178. 1 => ['file' => 0]
  179. ],
  180. 'size' => [
  181. 0 => ['file' => 17188],
  182. 1 => ['file' => 2010]
  183. ],
  184. ]
  185. ];
  186. $post = [
  187. 'pictures' => [
  188. 0 => ['name' => 'A cat'],
  189. 1 => ['name' => 'A moose']
  190. ],
  191. 0 => [
  192. 'name' => 'A dog'
  193. ]
  194. ];
  195. $psr = ServerRequestFactory::fromGlobals(null, null, $post, null, $files);
  196. $request = RequestTransformer::toCake($psr);
  197. $expected = [
  198. 'image_main' => [
  199. 'file' => [
  200. 'name' => 'born on.txt',
  201. 'type' => 'text/plain',
  202. 'tmp_name' => __FILE__,
  203. 'error' => 0,
  204. 'size' => 17178,
  205. ]
  206. ],
  207. 'no_file' => [
  208. 'file' => [
  209. 'name' => '',
  210. 'type' => '',
  211. 'tmp_name' => '',
  212. 'error' => UPLOAD_ERR_NO_FILE,
  213. 'size' => 0,
  214. ]
  215. ],
  216. 'pictures' => [
  217. 0 => [
  218. 'name' => 'A cat',
  219. 'file' => [
  220. 'name' => 'a-file.png',
  221. 'type' => 'image/png',
  222. 'tmp_name' => __FILE__,
  223. 'error' => 0,
  224. 'size' => 17188,
  225. ]
  226. ],
  227. 1 => [
  228. 'name' => 'A moose',
  229. 'file' => [
  230. 'name' => 'a-moose.png',
  231. 'type' => 'image/jpg',
  232. 'tmp_name' => __FILE__,
  233. 'error' => 0,
  234. 'size' => 2010,
  235. ]
  236. ]
  237. ],
  238. 0 => [
  239. 'name' => 'A dog',
  240. 'image' => [
  241. 'name' => 'scratch.text',
  242. 'type' => 'text/plain',
  243. 'tmp_name' => __FILE__,
  244. 'error' => 0,
  245. 'size' => 1490
  246. ]
  247. ]
  248. ];
  249. $this->assertEquals($expected, $request->data);
  250. }
  251. /**
  252. * Test that the transformed request sets the session path
  253. * as expected.
  254. *
  255. * @return void
  256. */
  257. public function testToCakeBaseSessionPath()
  258. {
  259. Configure::write('App.baseUrl', false);
  260. $server = [
  261. 'DOCUMENT_ROOT' => '/cake/repo/branches',
  262. 'PHP_SELF' => '/thisapp/webroot/index.php',
  263. 'REQUEST_URI' => '/posts/view/1',
  264. ];
  265. $psr = ServerRequestFactory::fromGlobals($server);
  266. $cake = RequestTransformer::toCake($psr);
  267. $this->assertEquals('/thisapp/', ini_get('session.cookie_path'));
  268. }
  269. /**
  270. * Test transforming session objects
  271. *
  272. * @return void
  273. */
  274. public function testToCakeSession()
  275. {
  276. $psr = ServerRequestFactory::fromGlobals();
  277. $session = new Session(['defaults' => 'php']);
  278. $session->write('test', 'value');
  279. $psr = $psr->withAttribute('session', $session);
  280. $cake = RequestTransformer::toCake($psr);
  281. $this->assertSame($session, $cake->session());
  282. }
  283. /**
  284. * Test transforming request bodies
  285. *
  286. * @return void
  287. */
  288. public function testToCakeRequestBody()
  289. {
  290. $psr = ServerRequestFactory::fromGlobals();
  291. $stream = new Stream('php://memory', 'rw');
  292. $stream->write('{"hello":"world"}');
  293. $stream->rewind();
  294. $psr = $psr->withBody($stream);
  295. $cake = RequestTransformer::toCake($psr);
  296. $this->assertSame(['hello' => 'world'], $cake->input('json_decode', true));
  297. }
  298. }