RequestHandlerComponentTest.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\RequestHandlerComponent;
  18. use Cake\Event\Event;
  19. use Cake\Http\Response;
  20. use Cake\Http\ServerRequest;
  21. use Cake\Routing\DispatcherFactory;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use TestApp\Controller\RequestHandlerTestController;
  25. use Zend\Diactoros\Stream;
  26. /**
  27. * RequestHandlerComponentTest class
  28. */
  29. class RequestHandlerComponentTest extends TestCase
  30. {
  31. /**
  32. * Controller property
  33. *
  34. * @var RequestHandlerTestController
  35. */
  36. public $Controller;
  37. /**
  38. * RequestHandler property
  39. *
  40. * @var RequestHandlerComponent
  41. */
  42. public $RequestHandler;
  43. /**
  44. * @var ServerRequest
  45. */
  46. public $request;
  47. /**
  48. * Backup of $_SERVER
  49. *
  50. * @var array
  51. */
  52. protected $server = [];
  53. /**
  54. * setUp method
  55. *
  56. * @return void
  57. */
  58. public function setUp()
  59. {
  60. parent::setUp();
  61. $this->server = $_SERVER;
  62. static::setAppNamespace();
  63. DispatcherFactory::add('Routing');
  64. DispatcherFactory::add('ControllerFactory');
  65. $this->_init();
  66. }
  67. /**
  68. * init method
  69. *
  70. * @return void
  71. */
  72. protected function _init()
  73. {
  74. $request = new ServerRequest('controller_posts/index');
  75. $response = $this->getMockBuilder('Cake\Http\Response')
  76. ->setMethods(['_sendHeader', 'stop'])
  77. ->getMock();
  78. $this->Controller = new RequestHandlerTestController($request, $response);
  79. $this->RequestHandler = $this->Controller->components()->load('RequestHandler');
  80. $this->request = $request;
  81. Router::scope('/', function ($routes) {
  82. $routes->setExtensions('json');
  83. $routes->fallbacks('InflectedRoute');
  84. });
  85. }
  86. /**
  87. * tearDown method
  88. *
  89. * @return void
  90. */
  91. public function tearDown()
  92. {
  93. parent::tearDown();
  94. DispatcherFactory::clear();
  95. Router::reload();
  96. Router::$initialized = false;
  97. $_SERVER = $this->server;
  98. unset($this->RequestHandler, $this->Controller);
  99. }
  100. /**
  101. * Test that the constructor sets the config.
  102. *
  103. * @return void
  104. */
  105. public function testConstructorConfig()
  106. {
  107. $config = [
  108. 'viewClassMap' => ['json' => 'MyPlugin.MyJson']
  109. ];
  110. $controller = $this->getMockBuilder('Cake\Controller\Controller')
  111. ->setMethods(['redirect'])
  112. ->getMock();
  113. $collection = new ComponentRegistry($controller);
  114. $requestHandler = new RequestHandlerComponent($collection, $config);
  115. $this->assertEquals(['json' => 'MyPlugin.MyJson'], $requestHandler->getConfig('viewClassMap'));
  116. }
  117. /**
  118. * testInitializeCallback method
  119. *
  120. * @return void
  121. */
  122. public function testInitializeCallback()
  123. {
  124. $this->assertNull($this->RequestHandler->ext);
  125. $this->Controller->request = $this->Controller->getRequest()->withParam('_ext', 'rss');
  126. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  127. $this->assertEquals('rss', $this->RequestHandler->ext);
  128. }
  129. /**
  130. * test that a mapped Accept-type header will set $this->ext correctly.
  131. *
  132. * @return void
  133. */
  134. public function testInitializeContentTypeSettingExt()
  135. {
  136. Router::reload();
  137. Router::$initialized = true;
  138. $this->Controller->request = $this->request->withHeader('Accept', 'application/json');
  139. $this->RequestHandler->ext = null;
  140. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  141. $this->assertEquals('json', $this->RequestHandler->ext);
  142. }
  143. /**
  144. * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers.
  145. *
  146. * @return void
  147. */
  148. public function testInitializeContentTypeWithjQueryAccept()
  149. {
  150. Router::reload();
  151. Router::$initialized = true;
  152. $this->Controller->request = $this->request
  153. ->withHeader('Accept', 'application/json, application/javascript, */*; q=0.01')
  154. ->withHeader('X-Requested-With', 'XMLHttpRequest');
  155. $this->RequestHandler->ext = null;
  156. Router::extensions('json', false);
  157. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  158. $this->assertEquals('json', $this->RequestHandler->ext);
  159. }
  160. /**
  161. * Test that RequestHandler does not set extension to csv for text/plain mimetype
  162. *
  163. * @return void
  164. */
  165. public function testInitializeContentTypeWithjQueryTextPlainAccept()
  166. {
  167. Router::reload();
  168. Router::$initialized = true;
  169. $this->Controller->request = $this->request->withHeader('Accept', 'text/plain, */*; q=0.01');
  170. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  171. $this->assertNull($this->RequestHandler->ext);
  172. }
  173. /**
  174. * Test that RequestHandler sets $this->ext when jQuery sends its wonky-ish headers
  175. * and the application is configured to handle multiple extensions
  176. *
  177. * @return void
  178. */
  179. public function testInitializeContentTypeWithjQueryAcceptAndMultiplesExtensions()
  180. {
  181. Router::reload();
  182. Router::$initialized = true;
  183. $this->Controller->request = $this->request->withHeader('Accept', 'application/json, application/javascript, */*; q=0.01');
  184. $this->RequestHandler->ext = null;
  185. Router::extensions(['rss', 'json'], false);
  186. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  187. $this->assertEquals('json', $this->RequestHandler->ext);
  188. }
  189. /**
  190. * Test that RequestHandler does not set $this->ext when multiple accepts are sent.
  191. *
  192. * @return void
  193. */
  194. public function testInitializeNoContentTypeWithSingleAccept()
  195. {
  196. Router::reload();
  197. Router::$initialized = true;
  198. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01';
  199. $this->assertNull($this->RequestHandler->ext);
  200. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  201. $this->assertNull($this->RequestHandler->ext);
  202. }
  203. /**
  204. * Test that ext is set to the first listed extension with multiple accepted
  205. * content types.
  206. * Having multiple types accepted with same weight, means the client lets the
  207. * server choose the returned content type.
  208. *
  209. * @return void
  210. */
  211. public function testInitializeNoContentTypeWithMultipleAcceptedTypes()
  212. {
  213. $this->Controller->request = $this->request->withHeader(
  214. 'Accept',
  215. 'application/json, application/javascript, application/xml, */*; q=0.01'
  216. );
  217. $this->RequestHandler->ext = null;
  218. Router::extensions(['xml', 'json'], false);
  219. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  220. $this->assertEquals('xml', $this->RequestHandler->ext);
  221. $this->RequestHandler->ext = null;
  222. Router::extensions(['json', 'xml'], false);
  223. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  224. $this->assertEquals('json', $this->RequestHandler->ext);
  225. }
  226. /**
  227. * Test that ext is set to type with highest weight
  228. *
  229. * @return void
  230. */
  231. public function testInitializeContentTypeWithMultipleAcceptedTypes()
  232. {
  233. Router::reload();
  234. Router::$initialized = true;
  235. $this->Controller->request = $this->request->withHeader(
  236. 'Accept',
  237. 'text/csv;q=1.0, application/json;q=0.8, application/xml;q=0.7'
  238. );
  239. $this->RequestHandler->ext = null;
  240. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  241. $this->assertEquals('json', $this->RequestHandler->ext);
  242. }
  243. /**
  244. * Test that ext is not set with confusing android accepts headers.
  245. *
  246. * @return void
  247. */
  248. public function testInitializeAmbiguousAndroidAccepts()
  249. {
  250. Router::reload();
  251. Router::$initialized = true;
  252. $this->request = $this->request->withEnv(
  253. 'HTTP_ACCEPT',
  254. 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  255. );
  256. $this->RequestHandler->ext = null;
  257. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  258. $this->assertNull($this->RequestHandler->ext);
  259. }
  260. /**
  261. * Test that the headers sent by firefox are not treated as XML requests.
  262. *
  263. * @return void
  264. */
  265. public function testInititalizeFirefoxHeaderNotXml()
  266. {
  267. $_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
  268. Router::extensions(['xml', 'json'], false);
  269. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  270. $this->assertNull($this->RequestHandler->ext);
  271. }
  272. /**
  273. * Test that a type mismatch doesn't incorrectly set the ext
  274. *
  275. * @return void
  276. */
  277. public function testInitializeContentTypeAndExtensionMismatch()
  278. {
  279. $this->assertNull($this->RequestHandler->ext);
  280. $extensions = Router::extensions();
  281. Router::extensions('xml', false);
  282. $this->Controller->request = $this->getMockBuilder('Cake\Http\ServerRequest')
  283. ->setMethods(['accepts'])
  284. ->getMock();
  285. $this->Controller->getRequest()->expects($this->any())
  286. ->method('accepts')
  287. ->will($this->returnValue(['application/json']));
  288. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  289. $this->assertNull($this->RequestHandler->ext);
  290. Router::extensions($extensions, false);
  291. }
  292. /**
  293. * testViewClassMap
  294. *
  295. * @return void
  296. */
  297. public function testViewClassMap()
  298. {
  299. $this->RequestHandler->setConfig(['viewClassMap' => ['json' => 'CustomJson']]);
  300. $result = $this->RequestHandler->getConfig('viewClassMap');
  301. $expected = [
  302. 'json' => 'CustomJson',
  303. 'xml' => 'Xml',
  304. 'ajax' => 'Ajax'
  305. ];
  306. $this->assertEquals($expected, $result);
  307. $this->RequestHandler->setConfig(['viewClassMap' => ['xls' => 'Excel.Excel']]);
  308. $result = $this->RequestHandler->getConfig('viewClassMap');
  309. $expected = [
  310. 'json' => 'CustomJson',
  311. 'xml' => 'Xml',
  312. 'ajax' => 'Ajax',
  313. 'xls' => 'Excel.Excel'
  314. ];
  315. $this->assertEquals($expected, $result);
  316. $this->RequestHandler->renderAs($this->Controller, 'json');
  317. $this->assertEquals('TestApp\View\CustomJsonView', $this->Controller->viewBuilder()->getClassName());
  318. }
  319. /**
  320. * test addInputType method
  321. *
  322. * @group deprecated
  323. * @return void
  324. */
  325. public function testDeprecatedAddInputType()
  326. {
  327. $this->deprecated(function () {
  328. $this->RequestHandler->addInputType('csv', ['str_getcsv']);
  329. $result = $this->RequestHandler->getConfig('inputTypeMap');
  330. $this->assertArrayHasKey('csv', $result);
  331. });
  332. }
  333. /**
  334. * testViewClassMap method
  335. *
  336. * @group deprecated
  337. * @return void
  338. */
  339. public function testViewClassMapMethod()
  340. {
  341. $this->deprecated(function () {
  342. $this->RequestHandler->setConfig(['viewClassMap' => ['json' => 'CustomJson']]);
  343. $this->RequestHandler->initialize([]);
  344. $result = $this->RequestHandler->viewClassMap();
  345. $expected = [
  346. 'json' => 'CustomJson',
  347. 'xml' => 'Xml',
  348. 'ajax' => 'Ajax'
  349. ];
  350. $this->assertEquals($expected, $result);
  351. $result = $this->RequestHandler->viewClassMap('xls', 'Excel.Excel');
  352. $expected = [
  353. 'json' => 'CustomJson',
  354. 'xml' => 'Xml',
  355. 'ajax' => 'Ajax',
  356. 'xls' => 'Excel.Excel'
  357. ];
  358. $this->assertEquals($expected, $result);
  359. $this->RequestHandler->renderAs($this->Controller, 'json');
  360. $this->assertEquals('TestApp\View\CustomJsonView', $this->Controller->viewClass);
  361. });
  362. }
  363. /**
  364. * Verify that isAjax is set on the request params for ajax requests
  365. *
  366. * @return void
  367. * @triggers Controller.startup $this->Controller
  368. */
  369. public function testIsAjaxParams()
  370. {
  371. $this->Controller->request = $this->request->withHeader('X-Requested-With', 'XMLHttpRequest');
  372. $event = new Event('Controller.startup', $this->Controller);
  373. $this->RequestHandler->initialize([]);
  374. $this->Controller->beforeFilter($event);
  375. $this->RequestHandler->startup($event);
  376. $this->assertTrue($this->Controller->getRequest()->getParam('isAjax'));
  377. }
  378. /**
  379. * testAutoAjaxLayout method
  380. *
  381. * @return void
  382. * @triggers Controller.startup $this->Controller
  383. */
  384. public function testAutoAjaxLayout()
  385. {
  386. $event = new Event('Controller.startup', $this->Controller);
  387. $this->Controller->request = $this->request->withHeader('X-Requested-With', 'XMLHttpRequest');
  388. $this->RequestHandler->initialize([]);
  389. $this->RequestHandler->startup($event);
  390. $event = new Event('Controller.beforeRender', $this->Controller);
  391. $this->RequestHandler->beforeRender($event);
  392. $this->assertEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
  393. $view = $this->Controller->createView();
  394. $this->assertEquals('ajax', $view->getLayout());
  395. $this->_init();
  396. $this->Controller->request = $this->Controller->getRequest()->withParam('_ext', 'js');
  397. $this->RequestHandler->initialize([]);
  398. $this->RequestHandler->startup($event);
  399. $this->assertNotEquals($this->Controller->viewClass, 'Cake\View\AjaxView');
  400. }
  401. /**
  402. * test custom JsonView class is loaded and correct.
  403. *
  404. * @return void
  405. * @triggers Controller.startup $this->Controller
  406. */
  407. public function testJsonViewLoaded()
  408. {
  409. Router::extensions(['json', 'xml', 'ajax'], false);
  410. $this->Controller->request = $this->Controller->getRequest()->withParam('_ext', 'json');
  411. $event = new Event('Controller.startup', $this->Controller);
  412. $this->RequestHandler->initialize([]);
  413. $this->RequestHandler->startup($event);
  414. $event = new Event('Controller.beforeRender', $this->Controller);
  415. $this->RequestHandler->beforeRender($event);
  416. $this->assertEquals('Cake\View\JsonView', $this->Controller->viewClass);
  417. $view = $this->Controller->createView();
  418. $this->assertEquals('json', $view->getLayoutPath());
  419. $this->assertEquals('json', $view->subDir);
  420. }
  421. /**
  422. * test custom XmlView class is loaded and correct.
  423. *
  424. * @return void
  425. * @triggers Controller.startup $this->Controller
  426. */
  427. public function testXmlViewLoaded()
  428. {
  429. Router::extensions(['json', 'xml', 'ajax'], false);
  430. $this->Controller->request = $this->Controller->getRequest()->withParam('_ext', 'xml');
  431. $event = new Event('Controller.startup', $this->Controller);
  432. $this->RequestHandler->initialize([]);
  433. $this->RequestHandler->startup($event);
  434. $event = new Event('Controller.beforeRender', $this->Controller);
  435. $this->RequestHandler->beforeRender($event);
  436. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  437. $view = $this->Controller->createView();
  438. $this->assertEquals('xml', $view->getLayoutPath());
  439. $this->assertEquals('xml', $view->subDir);
  440. }
  441. /**
  442. * test custom AjaxView class is loaded and correct.
  443. *
  444. * @return void
  445. * @triggers Controller.startup $this->Controller
  446. */
  447. public function testAjaxViewLoaded()
  448. {
  449. Router::extensions(['json', 'xml', 'ajax'], false);
  450. $this->Controller->request = $this->Controller->getRequest()->withParam('_ext', 'ajax');
  451. $event = new Event('Controller.startup', $this->Controller);
  452. $this->RequestHandler->initialize([]);
  453. $this->RequestHandler->startup($event);
  454. $event = new Event('Controller.beforeRender', $this->Controller);
  455. $this->RequestHandler->beforeRender($event);
  456. $this->assertEquals('Cake\View\AjaxView', $this->Controller->viewClass);
  457. $view = $this->Controller->createView();
  458. $this->assertEquals('ajax', $view->getLayout());
  459. }
  460. /**
  461. * test configured extension but no view class set.
  462. *
  463. * @return void
  464. * @triggers Controller.beforeRender $this->Controller
  465. */
  466. public function testNoViewClassExtension()
  467. {
  468. Router::extensions(['json', 'xml', 'ajax', 'csv'], false);
  469. $this->Controller->request = $this->Controller->getRequest()->withParam('_ext', 'csv');
  470. $event = new Event('Controller.startup', $this->Controller);
  471. $this->RequestHandler->initialize([]);
  472. $this->RequestHandler->startup($event);
  473. $this->Controller->getEventManager()->on('Controller.beforeRender', function () {
  474. return $this->Controller->response;
  475. });
  476. $this->Controller->render();
  477. $this->assertEquals('RequestHandlerTest' . DS . 'csv', $this->Controller->viewBuilder()->getTemplatePath());
  478. $this->assertEquals('csv', $this->Controller->viewBuilder()->getLayoutPath());
  479. }
  480. /**
  481. * testStartupCallback method
  482. *
  483. * @return void
  484. * @triggers Controller.beforeRender $this->Controller
  485. */
  486. public function testStartupCallback()
  487. {
  488. $event = new Event('Controller.beforeRender', $this->Controller);
  489. $_SERVER['REQUEST_METHOD'] = 'PUT';
  490. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  491. $this->Controller->request = new ServerRequest();
  492. $this->RequestHandler->beforeRender($event);
  493. $this->assertInternalType('array', $this->Controller->getRequest()->getData());
  494. $this->assertNotInternalType('object', $this->Controller->getRequest()->getData());
  495. }
  496. /**
  497. * testStartupCallback with charset.
  498. *
  499. * @return void
  500. * @triggers Controller.startup $this->Controller
  501. */
  502. public function testStartupCallbackCharset()
  503. {
  504. $event = new Event('Controller.startup', $this->Controller);
  505. $_SERVER['REQUEST_METHOD'] = 'PUT';
  506. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  507. $this->Controller->request = new ServerRequest();
  508. $this->RequestHandler->startup($event);
  509. $this->assertInternalType('array', $this->Controller->getRequest()->getData());
  510. $this->assertNotInternalType('object', $this->Controller->getRequest()->getData());
  511. }
  512. /**
  513. * Test that processing data results in an array.
  514. *
  515. * @return void
  516. * @triggers Controller.startup $this->Controller
  517. */
  518. public function testStartupProcessDataInvalid()
  519. {
  520. $this->Controller->request = new ServerRequest([
  521. 'environment' => [
  522. 'REQUEST_METHOD' => 'POST',
  523. 'CONTENT_TYPE' => 'application/json'
  524. ]
  525. ]);
  526. $event = new Event('Controller.startup', $this->Controller);
  527. $this->RequestHandler->startup($event);
  528. $this->assertEquals([], $this->Controller->getRequest()->getData());
  529. $stream = new Stream('php://memory', 'w');
  530. $stream->write('"invalid"');
  531. $this->Controller->request = $this->Controller->getRequest()->withBody($stream);
  532. $this->RequestHandler->startup($event);
  533. $this->assertEquals(['invalid'], $this->Controller->getRequest()->getData());
  534. }
  535. /**
  536. * Test that processing data results in an array.
  537. *
  538. * @return void
  539. * @triggers Controller.startup $this->Controller
  540. */
  541. public function testStartupProcessData()
  542. {
  543. $this->Controller->setRequest(new ServerRequest([
  544. 'environment' => [
  545. 'REQUEST_METHOD' => 'POST',
  546. 'CONTENT_TYPE' => 'application/json'
  547. ]
  548. ]));
  549. $stream = new Stream('php://memory', 'w');
  550. $stream->write('{"valid":true}');
  551. $this->Controller->setRequest($this->Controller->getRequest()->withBody($stream));
  552. $event = new Event('Controller.startup', $this->Controller);
  553. $this->RequestHandler->startup($event);
  554. $this->assertEquals(['valid' => true], $this->Controller->getRequest()->getData());
  555. }
  556. /**
  557. * Test that file handles are ignored as XML data.
  558. *
  559. * @return void
  560. * @triggers Controller.startup $this->Controller
  561. */
  562. public function testStartupIgnoreFileAsXml()
  563. {
  564. $this->Controller->setRequest(new ServerRequest([
  565. 'input' => '/dev/random',
  566. 'environment' => [
  567. 'REQUEST_METHOD' => 'POST',
  568. 'CONTENT_TYPE' => 'application/xml'
  569. ]
  570. ]));
  571. $event = new Event('Controller.startup', $this->Controller);
  572. $this->RequestHandler->startup($event);
  573. $this->assertEquals([], $this->Controller->getRequest()->getData());
  574. }
  575. /**
  576. * Test that input xml is parsed
  577. *
  578. * @return void
  579. */
  580. public function testStartupConvertXmlDataWrapper()
  581. {
  582. $xml = <<<XML
  583. <?xml version="1.0" encoding="utf-8"?>
  584. <data>
  585. <article id="1" title="first"></article>
  586. </data>
  587. XML;
  588. $this->Controller->setRequest((new ServerRequest(['input' => $xml]))
  589. ->withEnv('REQUEST_METHOD', 'POST')
  590. ->withEnv('CONTENT_TYPE', 'application/xml'));
  591. $event = new Event('Controller.startup', $this->Controller);
  592. $this->RequestHandler->startup($event);
  593. $expected = [
  594. 'data' => [
  595. 'article' => [
  596. '@id' => 1,
  597. '@title' => 'first'
  598. ]
  599. ]
  600. ];
  601. $this->assertEquals($expected, $this->Controller->getRequest()->getData());
  602. }
  603. /**
  604. * Test that input xml is parsed
  605. *
  606. * @return void
  607. */
  608. public function testStartupConvertXmlElements()
  609. {
  610. $xml = <<<XML
  611. <?xml version="1.0" encoding="utf-8"?>
  612. <article>
  613. <id>1</id>
  614. <title><![CDATA[first]]></title>
  615. </article>
  616. XML;
  617. $this->Controller->request = (new ServerRequest(['input' => $xml]))
  618. ->withEnv('REQUEST_METHOD', 'POST')
  619. ->withEnv('CONTENT_TYPE', 'application/xml');
  620. $event = new Event('Controller.startup', $this->Controller);
  621. $this->RequestHandler->startup($event);
  622. $expected = [
  623. 'article' => [
  624. 'id' => 1,
  625. 'title' => 'first'
  626. ]
  627. ];
  628. $this->assertEquals($expected, $this->Controller->getRequest()->getData());
  629. }
  630. /**
  631. * Test that input xml is parsed
  632. *
  633. * @return void
  634. */
  635. public function testStartupConvertXmlIgnoreEntities()
  636. {
  637. $xml = <<<XML
  638. <?xml version="1.0" encoding="UTF-8"?>
  639. <!DOCTYPE item [
  640. <!ENTITY item "item">
  641. <!ENTITY item1 "&item;&item;&item;&item;&item;&item;">
  642. <!ENTITY item2 "&item1;&item1;&item1;&item1;&item1;&item1;&item1;&item1;&item1;">
  643. <!ENTITY item3 "&item2;&item2;&item2;&item2;&item2;&item2;&item2;&item2;&item2;">
  644. <!ENTITY item4 "&item3;&item3;&item3;&item3;&item3;&item3;&item3;&item3;&item3;">
  645. <!ENTITY item5 "&item4;&item4;&item4;&item4;&item4;&item4;&item4;&item4;&item4;">
  646. <!ENTITY item6 "&item5;&item5;&item5;&item5;&item5;&item5;&item5;&item5;&item5;">
  647. <!ENTITY item7 "&item6;&item6;&item6;&item6;&item6;&item6;&item6;&item6;&item6;">
  648. <!ENTITY item8 "&item7;&item7;&item7;&item7;&item7;&item7;&item7;&item7;&item7;">
  649. ]>
  650. <item>
  651. <description>&item8;</description>
  652. </item>
  653. XML;
  654. $this->Controller->request = (new ServerRequest(['input' => $xml]))
  655. ->withEnv('REQUEST_METHOD', 'POST')
  656. ->withEnv('CONTENT_TYPE', 'application/xml');
  657. $event = new Event('Controller.startup', $this->Controller);
  658. $this->RequestHandler->startup($event);
  659. $this->assertEquals([], $this->Controller->getRequest()->getData());
  660. }
  661. /**
  662. * Test mapping a new type and having startup process it.
  663. *
  664. * @group deprecated
  665. * @return void
  666. * @triggers Controller.startup $this->Controller
  667. */
  668. public function testStartupCustomTypeProcess()
  669. {
  670. $this->deprecated(function () {
  671. $this->Controller->setRequest(new ServerRequest([
  672. 'input' => '"A","csv","string"',
  673. 'environment' => [
  674. 'REQUEST_METHOD' => 'POST',
  675. 'CONTENT_TYPE' => 'text/csv'
  676. ]
  677. ]));
  678. $this->RequestHandler->addInputType('csv', ['str_getcsv']);
  679. $event = new Event('Controller.startup', $this->Controller);
  680. $this->RequestHandler->startup($event);
  681. $expected = [
  682. 'A', 'csv', 'string'
  683. ];
  684. $this->assertEquals($expected, $this->Controller->getRequest()->getData());
  685. });
  686. }
  687. /**
  688. * Test that data isn't processed when parsed data already exists.
  689. *
  690. * @return void
  691. * @triggers Controller.startup $this->Controller
  692. */
  693. public function testStartupSkipDataProcess()
  694. {
  695. $this->Controller->setRequest(new ServerRequest([
  696. 'environment' => [
  697. 'REQUEST_METHOD' => 'POST',
  698. 'CONTENT_TYPE' => 'application/json'
  699. ]
  700. ]));
  701. $event = new Event('Controller.startup', $this->Controller);
  702. $this->RequestHandler->startup($event);
  703. $this->assertEquals([], $this->Controller->getRequest()->getData());
  704. $stream = new Stream('php://memory', 'w');
  705. $stream->write('{"new": "data"}');
  706. $this->Controller->setRequest($this->Controller->getRequest()
  707. ->withBody($stream)
  708. ->withParsedBody(['old' => 'news']));
  709. $this->RequestHandler->startup($event);
  710. $this->assertEquals(['old' => 'news'], $this->Controller->getRequest()->getData());
  711. }
  712. /**
  713. * testRenderAs method
  714. *
  715. * @return void
  716. */
  717. public function testRenderAs()
  718. {
  719. $this->RequestHandler->renderAs($this->Controller, 'rss');
  720. $this->Controller->viewBuilder()->setTemplatePath('request_handler_test\\rss');
  721. $this->RequestHandler->renderAs($this->Controller, 'js');
  722. $this->assertEquals('request_handler_test' . DS . 'js', $this->Controller->viewBuilder()->getTemplatePath());
  723. }
  724. /**
  725. * test that attachment headers work with renderAs
  726. *
  727. * @return void
  728. */
  729. public function testRenderAsWithAttachment()
  730. {
  731. $this->Controller->request = $this->request->withHeader('Accept', 'application/xml;q=1.0');
  732. $this->RequestHandler->renderAs($this->Controller, 'xml', ['attachment' => 'myfile.xml']);
  733. $this->assertEquals('Cake\View\XmlView', $this->Controller->viewClass);
  734. $this->assertEquals('application/xml', $this->Controller->getResponse()->getType());
  735. $this->assertEquals('UTF-8', $this->Controller->getResponse()->getCharset());
  736. $this->assertContains('myfile.xml', $this->Controller->getResponse()->getHeaderLine('Content-Disposition'));
  737. }
  738. /**
  739. * test that respondAs works as expected.
  740. *
  741. * @return void
  742. */
  743. public function testRespondAs()
  744. {
  745. $result = $this->RequestHandler->respondAs('json');
  746. $this->assertTrue($result);
  747. $this->assertEquals('application/json', $this->Controller->getResponse()->getType());
  748. $result = $this->RequestHandler->respondAs('text/xml');
  749. $this->assertTrue($result);
  750. $this->assertEquals('text/xml', $this->Controller->getResponse()->getType());
  751. }
  752. /**
  753. * test that attachment headers work with respondAs
  754. *
  755. * @return void
  756. */
  757. public function testRespondAsWithAttachment()
  758. {
  759. $result = $this->RequestHandler->respondAs('xml', ['attachment' => 'myfile.xml']);
  760. $this->assertTrue($result);
  761. $response = $this->Controller->getResponse();
  762. $this->assertContains('myfile.xml', $response->getHeaderLine('Content-Disposition'));
  763. $this->assertContains('application/xml', $response->getType());
  764. }
  765. /**
  766. * test that calling renderAs() more than once continues to work.
  767. *
  768. * @link #6466
  769. * @return void
  770. */
  771. public function testRenderAsCalledTwice()
  772. {
  773. $this->Controller->getEventManager()->on('Controller.beforeRender', function (\Cake\Event\Event $e) {
  774. return $e->getSubject()->response;
  775. });
  776. $this->Controller->render();
  777. $this->RequestHandler->renderAs($this->Controller, 'print');
  778. $this->assertEquals('RequestHandlerTest' . DS . 'print', $this->Controller->viewBuilder()->getTemplatePath());
  779. $this->assertEquals('print', $this->Controller->viewBuilder()->getLayoutPath());
  780. $this->RequestHandler->renderAs($this->Controller, 'js');
  781. $this->assertEquals('RequestHandlerTest' . DS . 'js', $this->Controller->viewBuilder()->getTemplatePath());
  782. $this->assertEquals('js', $this->Controller->viewBuilder()->getLayoutPath());
  783. }
  784. /**
  785. * testRequestContentTypes method
  786. *
  787. * @return void
  788. */
  789. public function testRequestContentTypes()
  790. {
  791. $this->Controller->request = $this->request->withEnv('REQUEST_METHOD', 'GET');
  792. $this->assertNull($this->RequestHandler->requestedWith());
  793. $this->Controller->request = $this->request->withEnv('REQUEST_METHOD', 'POST')
  794. ->withEnv('CONTENT_TYPE', 'application/json');
  795. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  796. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  797. $this->assertEquals('json', $result);
  798. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  799. $this->assertFalse($result);
  800. $this->Controller->request = $this->request
  801. ->withEnv('REQUEST_METHOD', 'PATCH')
  802. ->withEnv('CONTENT_TYPE', 'application/json');
  803. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  804. $this->Controller->request = $this->request
  805. ->withEnv('REQUEST_METHOD', 'DELETE')
  806. ->withEnv('CONTENT_TYPE', 'application/json');
  807. $this->assertEquals('json', $this->RequestHandler->requestedWith());
  808. $this->Controller->request = $this->request
  809. ->withEnv('REQUEST_METHOD', 'POST')
  810. ->withEnv('CONTENT_TYPE', 'application/json');
  811. $result = $this->RequestHandler->requestedWith(['json', 'xml']);
  812. $this->assertEquals('json', $result);
  813. $result = $this->RequestHandler->requestedWith(['rss', 'atom']);
  814. $this->assertFalse($result);
  815. $this->Controller->request = $this->request->withHeader(
  816. 'Accept',
  817. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  818. );
  819. $this->assertTrue($this->RequestHandler->isXml());
  820. $this->assertFalse($this->RequestHandler->isAtom());
  821. $this->assertFalse($this->RequestHandler->isRSS());
  822. $this->Controller->request = $this->request->withHeader(
  823. 'Accept',
  824. 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  825. );
  826. $this->assertTrue($this->RequestHandler->isAtom());
  827. $this->assertFalse($this->RequestHandler->isRSS());
  828. $this->Controller->request = $this->request->withHeader(
  829. 'Accept',
  830. 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  831. );
  832. $this->assertFalse($this->RequestHandler->isAtom());
  833. $this->assertTrue($this->RequestHandler->isRSS());
  834. $this->assertFalse($this->RequestHandler->isWap());
  835. $this->Controller->request = $this->request->withHeader(
  836. 'Accept',
  837. 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*'
  838. );
  839. $this->assertTrue($this->RequestHandler->isWap());
  840. }
  841. /**
  842. * testResponseContentType method
  843. *
  844. * @return void
  845. */
  846. public function testResponseContentType()
  847. {
  848. $this->assertEquals('html', $this->RequestHandler->responseType());
  849. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  850. $this->assertEquals('atom', $this->RequestHandler->responseType());
  851. }
  852. /**
  853. * testMobileDeviceDetection method
  854. *
  855. * @return void
  856. */
  857. public function testMobileDeviceDetection()
  858. {
  859. $request = $this->getMockBuilder('Cake\Http\ServerRequest')
  860. ->setMethods(['is'])
  861. ->getMock();
  862. $request->expects($this->once())->method('is')
  863. ->with('mobile')
  864. ->will($this->returnValue(true));
  865. $this->Controller->setRequest($request);
  866. $this->assertTrue($this->RequestHandler->isMobile());
  867. }
  868. /**
  869. * test that map alias converts aliases to content types.
  870. *
  871. * @return void
  872. */
  873. public function testMapAlias()
  874. {
  875. $result = $this->RequestHandler->mapAlias('xml');
  876. $this->assertEquals('application/xml', $result);
  877. $result = $this->RequestHandler->mapAlias('text/html');
  878. $this->assertNull($result);
  879. $result = $this->RequestHandler->mapAlias('wap');
  880. $this->assertEquals('text/vnd.wap.wml', $result);
  881. $result = $this->RequestHandler->mapAlias(['xml', 'js', 'json']);
  882. $expected = ['application/xml', 'application/javascript', 'application/json'];
  883. $this->assertEquals($expected, $result);
  884. }
  885. /**
  886. * test accepts() on the component
  887. *
  888. * @return void
  889. */
  890. public function testAccepts()
  891. {
  892. $this->Controller->setRequest($this->request->withHeader(
  893. 'Accept',
  894. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  895. ));
  896. $this->assertTrue($this->RequestHandler->accepts(['js', 'xml', 'html']));
  897. $this->assertFalse($this->RequestHandler->accepts(['gif', 'jpeg', 'foo']));
  898. $this->Controller->setRequest($this->request->withHeader('Accept', '*/*;q=0.5'));
  899. $this->assertFalse($this->RequestHandler->accepts('rss'));
  900. }
  901. /**
  902. * test accepts and prefers methods.
  903. *
  904. * @return void
  905. */
  906. public function testPrefers()
  907. {
  908. $this->Controller->request = $this->request->withHeader(
  909. 'Accept',
  910. 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*'
  911. );
  912. $this->assertNotEquals('rss', $this->RequestHandler->prefers());
  913. $this->RequestHandler->ext = 'rss';
  914. $this->assertEquals('rss', $this->RequestHandler->prefers());
  915. $this->assertFalse($this->RequestHandler->prefers('xml'));
  916. $this->assertEquals('xml', $this->RequestHandler->prefers(['js', 'xml', 'xhtml']));
  917. $this->assertFalse($this->RequestHandler->prefers(['red', 'blue']));
  918. $this->assertEquals('xhtml', $this->RequestHandler->prefers(['js', 'json', 'xhtml']));
  919. $this->assertTrue($this->RequestHandler->prefers(['rss']), 'Should return true if input matches ext.');
  920. $this->assertFalse($this->RequestHandler->prefers(['html']), 'No match with ext, return false.');
  921. $this->_init();
  922. $this->Controller->request = $this->request->withHeader(
  923. 'Accept',
  924. 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
  925. );
  926. $this->assertEquals('xml', $this->RequestHandler->prefers());
  927. $this->Controller->request = $this->request->withHeader('Accept', '*/*;q=0.5');
  928. $this->assertEquals('html', $this->RequestHandler->prefers());
  929. $this->assertFalse($this->RequestHandler->prefers('rss'));
  930. $this->Controller->request = $this->request->withEnv('HTTP_ACCEPT', null);
  931. $this->RequestHandler->ext = 'json';
  932. $this->assertFalse($this->RequestHandler->prefers('xml'));
  933. }
  934. /**
  935. * testAddInputTypeException method
  936. *
  937. * @group deprecated
  938. * @return void
  939. */
  940. public function testAddInputTypeException()
  941. {
  942. $this->expectException(\Cake\Core\Exception\Exception::class);
  943. $this->deprecated(function () {
  944. $this->RequestHandler->addInputType('csv', ['I am not callable']);
  945. });
  946. }
  947. /**
  948. * Test checkNotModified method
  949. *
  950. * @return void
  951. * @triggers Controller.beforeRender $this->Controller
  952. */
  953. public function testCheckNotModifiedByEtagStar()
  954. {
  955. $response = new Response();
  956. $response = $response->withEtag('something')
  957. ->withHeader('Content-Type', 'text/plain')
  958. ->withStringBody('keeper');
  959. $this->Controller->response = $response;
  960. $this->Controller->request = $this->request->withHeader('If-None-Match', '*');
  961. $event = new Event('Controller.beforeRender', $this->Controller);
  962. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  963. $this->assertFalse($requestHandler->beforeRender($event));
  964. $this->assertEquals(304, $this->Controller->getResponse()->getStatusCode());
  965. $this->assertEquals('', (string)$this->Controller->getResponse()->getBody());
  966. $this->assertFalse($this->Controller->getResponse()->hasHeader('Content-Type'), 'header should not be removed.');
  967. }
  968. /**
  969. * Test checkNotModified method
  970. *
  971. * @return void
  972. * @triggers Controller.beforeRender
  973. */
  974. public function testCheckNotModifiedByEtagExact()
  975. {
  976. $response = new Response();
  977. $response = $response->withEtag('something', true)
  978. ->withHeader('Content-Type', 'text/plain')
  979. ->withStringBody('keeper');
  980. $this->Controller->response = $response;
  981. $this->Controller->request = $this->request->withHeader('If-None-Match', 'W/"something", "other"');
  982. $event = new Event('Controller.beforeRender', $this->Controller);
  983. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  984. $this->assertFalse($requestHandler->beforeRender($event));
  985. $this->assertEquals(304, $this->Controller->getResponse()->getStatusCode());
  986. $this->assertEquals('', (string)$this->Controller->getResponse()->getBody());
  987. $this->assertFalse($this->Controller->getResponse()->hasHeader('Content-Type'));
  988. }
  989. /**
  990. * Test checkNotModified method
  991. *
  992. * @return void
  993. * @triggers Controller.beforeRender $this->Controller
  994. */
  995. public function testCheckNotModifiedByEtagAndTime()
  996. {
  997. $this->Controller->request = $this->request
  998. ->withHeader('If-None-Match', 'W/"something", "other"')
  999. ->withHeader('If-Modified-Since', '2012-01-01 00:00:00');
  1000. $response = new Response();
  1001. $response = $response->withEtag('something', true)
  1002. ->withHeader('Content-type', 'text/plain')
  1003. ->withStringBody('should be removed')
  1004. ->withModified('2012-01-01 00:00:00');
  1005. $this->Controller->response = $response;
  1006. $event = new Event('Controller.beforeRender', $this->Controller);
  1007. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  1008. $this->assertFalse($requestHandler->beforeRender($event));
  1009. $this->assertEquals(304, $this->Controller->getResponse()->getStatusCode());
  1010. $this->assertEquals('', (string)$this->Controller->getResponse()->getBody());
  1011. $this->assertFalse($this->Controller->getResponse()->hasHeader('Content-type'));
  1012. }
  1013. /**
  1014. * Test checkNotModified method
  1015. *
  1016. * @return void
  1017. * @triggers Controller.beforeRender $this->Controller
  1018. */
  1019. public function testCheckNotModifiedNoInfo()
  1020. {
  1021. $response = $this->getMockBuilder('Cake\Http\Response')
  1022. ->setMethods(['notModified', 'stop'])
  1023. ->getMock();
  1024. $response->expects($this->never())->method('notModified');
  1025. $this->Controller->response = $response;
  1026. $event = new Event('Controller.beforeRender', $this->Controller);
  1027. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  1028. $this->assertNull($requestHandler->beforeRender($event));
  1029. }
  1030. /**
  1031. * Test default options in construction
  1032. *
  1033. * @return void
  1034. */
  1035. public function testConstructDefaultOptions()
  1036. {
  1037. $requestHandler = new RequestHandlerComponent($this->Controller->components());
  1038. $viewClass = $requestHandler->getConfig('viewClassMap');
  1039. $expected = [
  1040. 'json' => 'Json',
  1041. 'xml' => 'Xml',
  1042. 'ajax' => 'Ajax',
  1043. ];
  1044. $this->assertEquals($expected, $viewClass);
  1045. $inputs = $requestHandler->getConfig('inputTypeMap');
  1046. $this->assertArrayHasKey('json', $inputs);
  1047. $this->assertArrayHasKey('xml', $inputs);
  1048. }
  1049. /**
  1050. * Test options in constructor replace defaults
  1051. *
  1052. * @return void
  1053. */
  1054. public function testConstructReplaceOptions()
  1055. {
  1056. $requestHandler = new RequestHandlerComponent(
  1057. $this->Controller->components(),
  1058. [
  1059. 'viewClassMap' => ['json' => 'Json'],
  1060. 'inputTypeMap' => ['json' => ['json_decode', true]]
  1061. ]
  1062. );
  1063. $viewClass = $requestHandler->getConfig('viewClassMap');
  1064. $expected = [
  1065. 'json' => 'Json',
  1066. ];
  1067. $this->assertEquals($expected, $viewClass);
  1068. $inputs = $requestHandler->getConfig('inputTypeMap');
  1069. $this->assertArrayHasKey('json', $inputs);
  1070. $this->assertCount(1, $inputs);
  1071. }
  1072. /**
  1073. * test beforeRender() doesn't override response type set in controller action
  1074. *
  1075. * @return void
  1076. */
  1077. public function testBeforeRender()
  1078. {
  1079. $this->Controller->set_response_type();
  1080. $event = new Event('Controller.beforeRender', $this->Controller);
  1081. $this->RequestHandler->beforeRender($event);
  1082. $this->assertEquals('text/plain', $this->Controller->getResponse()->getType());
  1083. }
  1084. /**
  1085. * tests beforeRender automatically uses renderAs when a supported extension is found
  1086. *
  1087. * @return void
  1088. */
  1089. public function testBeforeRenderAutoRenderAs()
  1090. {
  1091. $this->Controller->setRequest($this->request->withParam('_ext', 'csv'));
  1092. $this->RequestHandler->startup(new Event('Controller.startup', $this->Controller));
  1093. $event = new Event('Controller.beforeRender', $this->Controller);
  1094. $this->RequestHandler->beforeRender($event);
  1095. $this->assertEquals('text/csv', $this->Controller->getResponse()->getType());
  1096. }
  1097. }